UNPKG

@selfcommunity/react-templates

Version:

React Templates Components to integrate a Community created with SelfCommunity.

136 lines (129 loc) 5.21 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { useContext, useRef } from 'react'; import { styled } from '@mui/material'; import { CategoriesSuggestionWidget, Feed, FeedObject, FeedObjectSkeleton, SCFeedObjectTemplateType, InlineComposerWidget, LoyaltyProgramWidget, UserSuggestionWidget, PlatformWidget, OnBoardingWidget } from '@selfcommunity/react-ui'; import { Endpoints } from '@selfcommunity/api-services'; import { SCUserContext, UserUtils } from '@selfcommunity/react-core'; import { useThemeProps } from '@mui/system'; import classNames from 'classnames'; import { SCCustomAdvPosition } from '@selfcommunity/types'; import { useSnackbar } from 'notistack'; import { FormattedMessage } from 'react-intl'; 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: PlatformWidget, componentProps: {}, column: 'right', position: 0 }, { type: 'widget', component: LoyaltyProgramWidget, componentProps: {}, column: 'right', position: 1 }, { type: 'widget', component: CategoriesSuggestionWidget, componentProps: {}, column: 'right', position: 2 }, { type: 'widget', component: UserSuggestionWidget, componentProps: {}, column: 'right', position: 3 } ]; /** * > API documentation for the Community-JS Main Feed Template. Learn about the available props and the CSS API. * * * This component renders the template for the main feed. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-templates/Components/MainFeed) #### Import ```jsx import {MainFeed} from '@selfcommunity/react-templates'; ``` #### Component Name The name `SCMainFeedTemplate` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCMainFeedTemplate-root|Styles applied to the root element.| * * @param inProps */ export default function MainFeed(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { id = 'main_feed', className, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props; //CONTEXT const scUserContext = useContext(SCUserContext); const { enqueueSnackbar } = useSnackbar(); // REF const feedRef = useRef(); // Ckeck user is authenticated if (!scUserContext.user) { return null; } // HANDLERS const handleComposerSuccess = (feedObject) => { enqueueSnackbar(_jsx(FormattedMessage, { id: "ui.inlineComposerWidget.success", defaultMessage: "ui.inlineComposerWidget.success" }), { variant: 'success', autoHideDuration: 3000 }); // Hydrate feedUnit const feedUnit = { type: feedObject.type, [feedObject.type]: feedObject, seen_by_id: [], has_boost: false }; feedRef && feedRef.current && feedRef.current.addFeedData(feedUnit, true); }; const handleAddGenerationContent = (feedObjects) => { if (feedRef && feedRef.current) { const currentFeedObjectIds = feedRef.current.getCurrentFeedObjectIds(); feedObjects.forEach((feedObject) => { if (!currentFeedObjectIds.includes(feedObject.id)) { const feedUnit = { type: feedObject.type, [feedObject.type]: feedObject, seen_by_id: [], has_boost: false }; feedRef.current.addFeedData(feedUnit, true); } }); } }; return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className), ref: feedRef, endpoint: Endpoints.MainFeed, 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: _jsxs(_Fragment, { children: [_jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess }), UserUtils.isAdmin(scUserContext.user) && _jsx(OnBoardingWidget, { onGeneratedContent: handleAddGenerationContent })] }), requireAuthentication: true, disablePaginationLinks: true, enabledCustomAdvPositions: [ SCCustomAdvPosition.POSITION_FEED_SIDEBAR, SCCustomAdvPosition.POSITION_FEED, SCCustomAdvPosition.POSITION_BELOW_TOPBAR ] }, FeedProps))); }