@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
121 lines (114 loc) • 5.52 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo, useRef } from 'react';
import { styled } from '@mui/material';
import { CategoryTrendingFeedWidget, CategoryTrendingUsersWidget, ContributionUtils, Feed, FeedObject, FeedObjectSkeleton, InlineComposerWidget, SCFeedObjectTemplateType } from '@selfcommunity/react-ui';
import { Endpoints } from '@selfcommunity/api-services';
import { Link, SCRoutes, UserUtils, useSCFetchCategory, useSCRouting, useSCUser } from '@selfcommunity/react-core';
import { SCCustomAdvPosition, 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) {
// 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();
// Hooks
const { scCategory } = useSCFetchCategory({ id: categoryId, category });
// HANDLERS
const handleComposerSuccess = (feedObject) => {
// Not insert if the category does not match
if (feedObject.categories.findIndex((c) => c.id === scCategory.id) === -1) {
enqueueSnackbar(_jsx(FormattedMessage, { id: "ui.composerIconButton.composer.success", defaultMessage: "ui.composerIconButton.composer.success" }), {
action: (snackbarId) => (_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_by_id: [],
has_boost: false
};
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, {});
}
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_by_id.includes(scUser.id) : null
}), itemIdGenerator: (item) => item[item.type].id, ItemProps: FeedObjectProps, ItemSkeleton: FeedObjectSkeleton, ItemSkeletonProps: {
template: SCFeedObjectTemplateType.PREVIEW
}, FeedSidebarProps: FeedSidebarProps, HeaderComponent: _jsx(_Fragment, { children: ((scCategory.content_only_staff && UserUtils.isStaff(scUserContext.user)) || !scCategory.content_only_staff) && (_jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess, defaultValue: { categories: [scCategory] }, feedType: SCFeedTypologyType.CATEGORY })) }), CustomAdvProps: { categoriesId: [scCategory.id] }, enabledCustomAdvPositions: [
SCCustomAdvPosition.POSITION_FEED_SIDEBAR,
SCCustomAdvPosition.POSITION_FEED,
SCCustomAdvPosition.POSITION_BELOW_TOPBAR
] }, FeedProps)));
}