UNPKG

@selfcommunity/react-templates

Version:

React Templates Components to integrate a Community created with SelfCommunity.

121 lines (114 loc) 4.55 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useMemo, useRef } from 'react'; import { styled } from '@mui/material'; import { Endpoints } from '@selfcommunity/api-services'; import { useSCFetchUser, useSCUser } from '@selfcommunity/react-core'; import { Feed, FeedObject, FeedObjectSkeleton, InlineComposerWidget, SCFeedObjectTemplateType, UserFollowedCategoriesWidget, UserFollowedUsersWidget, UserFollowersWidget, UserLiveStreamWidget } from '@selfcommunity/react-ui'; import { UserFeedSkeleton } 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: UserLiveStreamWidget, componentProps: {}, column: 'right', position: 0 }, { type: 'widget', component: UserFollowedCategoriesWidget, componentProps: {}, column: 'right', position: 1 }, { type: 'widget', component: UserFollowedUsersWidget, componentProps: {}, column: 'right', position: 2 }, { type: 'widget', component: UserFollowersWidget, componentProps: {}, column: 'right', position: 3 } ]; /** * > API documentation for the Community-JS User Feed Template. Learn about the available props and the CSS API. * * * This component renders a specific user's feed template. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-templates/Components/UserFeed) #### Import ```jsx import {UserFeed} from '@selfcommunity/react-templates'; ``` #### Component Name The name `SCUserFeedTemplate` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCUserFeedTemplate-root|Styles applied to the root element.| * * @param inProps */ export default function UserFeed(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { id = 'user_feed', className, userId, user, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props; // Context const scUserContext = useSCUser(); const { enqueueSnackbar } = useSnackbar(); // Hooks const { scUser } = useSCFetchUser({ id: userId, user }); // REF const feedRef = useRef(null); // HANDLERS const handleComposerSuccess = (feedObject) => { const messageId = feedObject.scheduled_at ? 'ui.composer.scheduled.success' : 'ui.inlineComposerWidget.success'; enqueueSnackbar(_jsx(FormattedMessage, { id: messageId, defaultMessage: messageId }), { variant: 'success', autoHideDuration: 3000 }); // 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) => { return Object.assign(Object.assign({}, w), { componentProps: Object.assign(Object.assign({}, w.componentProps), { userId: scUser ? scUser.id : userId }) }); }), [scUser, widgets]); if (scUser === null) { return _jsx(UserFeedSkeleton, {}); } return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className), ref: feedRef, endpoint: Object.assign(Object.assign({}, Endpoints.UserFeed), { url: () => Endpoints.UserFeed.url({ id: scUser.id }) }), widgets: _widgets, ItemComponent: FeedObject, itemPropsGenerator: (_scUser, item) => ({ feedObject: item[item.type], feedObjectType: item.type, feedObjectActivities: item.activities ? item.activities : null }), itemIdGenerator: (item) => item[item.type].id, ItemProps: FeedObjectProps, ItemSkeleton: FeedObjectSkeleton, ItemSkeletonProps: { template: SCFeedObjectTemplateType.PREVIEW } }, (scUserContext.user ? { HeaderComponent: _jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess }) } : {}), { FeedSidebarProps: FeedSidebarProps }, FeedProps))); }