@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
139 lines (132 loc) • 6.85 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useRef } from 'react';
import { styled } from '@mui/material';
import { CategoryTrendingFeedWidget, CategoryTrendingUsersWidget, ContributionUtils, Feed, FeedObject, FeedObjectSkeleton, HiddenPurchasableContent, InlineComposerWidget, SCFeedObjectTemplateType, CustomAdv } from '@selfcommunity/react-ui';
import { Endpoints } from '@selfcommunity/api-services';
import { Link, SCPreferences, SCRoutes, UserUtils, useSCFetchCategory, useSCPreferenceEnabled, useSCPreferencesAndFeaturesEnabled, useSCRouting, useSCUser } from '@selfcommunity/react-core';
import { SCCustomAdvPosition, SCFeatureName, SCFeedTypologyType } from '@selfcommunity/types';
import { CategoryFeedSkeleton } from './index';
import { useThemeProps } from '@mui/system';
import classNames from 'classnames';
import { FormattedMessage } from 'react-intl';
import { useSnackbar } from 'notistack';
import { PREFIX } from './constants';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Feed, {
name: PREFIX,
slot: 'Root'
})(() => ({}));
// Widgets for feed
const WIDGETS = [
{
type: 'widget',
component: CategoryTrendingUsersWidget,
componentProps: {},
column: 'right',
position: 0
},
{
type: 'widget',
component: CategoryTrendingFeedWidget,
componentProps: {},
column: 'right',
position: 1
}
];
/**
* > API documentation for the Community-JS Category Feed Template. Learn about the available props and the CSS API.
*
*
* This component renders a specific category's feed.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-templates/Components/CategoryFeed)
#### Import
```jsx
import {CategoryFeed} from '@selfcommunity/react-templates';
```
#### Component Name
The name `SCCategoryFeedTemplate` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCCategoryFeedTemplate-root|Styles applied to the root element.|
*
* @param inProps
*/
export default function CategoryFeed(inProps) {
var _a;
// PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { id = 'category_feed', className, category, categoryId, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props;
// CONTEXT
const scRoutingContext = useSCRouting();
const scUserContext = useSCUser();
const { enqueueSnackbar } = useSnackbar();
// REF
const feedRef = useRef(null);
// Hooks
const { scCategory } = useSCFetchCategory({ id: categoryId, category });
const isAdvertisingCustomAdvEnabled = useSCPreferenceEnabled(SCPreferences.ADVERTISING_CUSTOM_ADV_ENABLED);
const isAdvertisingCustomAdvOnlyForAnonUsersEnabled = useSCPreferenceEnabled(SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED);
const isPaymentsEnabled = useSCPreferencesAndFeaturesEnabled([SCPreferences.CONFIGURATIONS_PAYMENTS_ENABLED], [SCFeatureName.PAYMENTS]);
/**
* Render advertising above the feed
*/
function renderAdvertising() {
if (isAdvertisingCustomAdvEnabled &&
((isAdvertisingCustomAdvOnlyForAnonUsersEnabled && scUserContext.user === null) || !isAdvertisingCustomAdvOnlyForAnonUsersEnabled)) {
return _jsx(CustomAdv, { position: SCCustomAdvPosition.POSITION_ABOVE_FEED_CATEGORY, categoriesId: [scCategory.id] });
}
return null;
}
// HANDLERS
const handleComposerSuccess = (feedObject) => {
// Not insert if the category does not match
if (feedObject.categories.findIndex((c) => c.id === scCategory.id) === -1) {
const messageId = feedObject.scheduled_at ? 'ui.composer.scheduled.success' : 'ui.composerIconButton.composer.success';
enqueueSnackbar(_jsx(FormattedMessage, { id: messageId, defaultMessage: messageId }), {
action: () => (_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${feedObject.type.toUpperCase()}_ROUTE_NAME`], ContributionUtils.getRouteData(feedObject)) }, { children: _jsx(FormattedMessage, { id: "ui.composerIconButton.composer.viewContribute", defaultMessage: "ui.composerIconButton.composer.viewContribute" }) }))),
variant: 'success',
autoHideDuration: 7000
});
return;
}
// Hydrate feedUnit
const feedUnit = {
type: feedObject.type,
[feedObject.type]: feedObject,
seen: false,
has_boost: false
};
!feedObject.draft && feedRef && feedRef.current && feedRef.current.addFeedData(feedUnit, true);
};
// WIDGETS
const _widgets = useMemo(() => widgets.map((w) => {
if (scCategory) {
return Object.assign(Object.assign({}, w), { componentProps: Object.assign(Object.assign({}, w.componentProps), { categoryId: scCategory.id }) });
}
return w;
}), [widgets, scCategory]);
if (!scCategory) {
return _jsx(CategoryFeedSkeleton, {});
}
else if (scCategory && isPaymentsEnabled && !scCategory.followed && !scCategory.payment_order && ((_a = scCategory.paywalls) === null || _a === void 0 ? void 0 : _a.length) > 0) {
return _jsx(HiddenPurchasableContent, {});
}
return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className), ref: feedRef, endpoint: Object.assign(Object.assign({}, Endpoints.CategoryFeed), { url: () => Endpoints.CategoryFeed.url({ id: scCategory.id }) }), widgets: _widgets, ItemComponent: FeedObject, itemPropsGenerator: (scUser, item) => ({
feedObject: item[item.type],
feedObjectType: item.type,
feedObjectActivities: item.activities ? item.activities : null,
markRead: scUser ? !item.seen : null
}), itemIdGenerator: (item) => item[item.type].id, ItemProps: FeedObjectProps, ItemSkeleton: FeedObjectSkeleton, ItemSkeletonProps: {
template: SCFeedObjectTemplateType.PREVIEW
}, FeedSidebarProps: FeedSidebarProps, HeaderComponent: _jsxs(_Fragment, { children: [((scCategory.content_only_staff && UserUtils.isStaff(scUserContext.user)) || !scCategory.content_only_staff) && (_jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess, defaultValue: { categories: [scCategory] }, feedType: SCFeedTypologyType.CATEGORY })), renderAdvertising()] }), CustomAdvProps: { categoriesId: [scCategory.id] }, enabledCustomAdvPositions: [
SCCustomAdvPosition.POSITION_FEED_SIDEBAR,
SCCustomAdvPosition.POSITION_FEED,
SCCustomAdvPosition.POSITION_BELOW_TOPBAR
] }, FeedProps)));
}