UNPKG

@selfcommunity/react-templates

Version:

React Templates Components to integrate a Community created with SelfCommunity.

125 lines (118 loc) 5.74 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useMemo, useRef } from 'react'; import { styled } from '@mui/material'; import { ContributionUtils, Feed, FeedObject, FeedObjectSkeleton, GroupInfoWidget, GroupMembersWidget, InlineComposerWidget, SCFeedObjectTemplateType } from '@selfcommunity/react-ui'; import { Endpoints } from '@selfcommunity/api-services'; import { Link, SCRoutes, useSCFetchGroup, useSCRouting } from '@selfcommunity/react-core'; import { SCCustomAdvPosition, SCFeedTypologyType, SCGroupSubscriptionStatusType } from '@selfcommunity/types'; import { useThemeProps } from '@mui/system'; import classNames from 'classnames'; import { FormattedMessage } from 'react-intl'; import { useSnackbar } from 'notistack'; import { PREFIX } from './constants'; import GroupFeedSkeleton from './Skeleton'; const classes = { root: `${PREFIX}-root` }; const Root = styled(Feed, { name: PREFIX, slot: 'Root' })(() => ({})); // Widgets for feed const WIDGETS = [ { type: 'widget', component: GroupInfoWidget, componentProps: {}, column: 'right', position: 0 }, { type: 'widget', component: GroupMembersWidget, componentProps: {}, column: 'right', position: 1 } ]; /** * > API documentation for the Community-JS Group Feed Template. Learn about the available props and the CSS API. * * * This component renders a specific group's feed. #### Import ```jsx import {GroupFeed} from '@selfcommunity/react-templates'; ``` #### Component Name The name `SCGroupFeedTemplate` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCGroupFeedTemplate-root|Styles applied to the root element.| * * @param inProps */ export default function GroupFeed(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { id = 'group_feed', className, group, groupId, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props; // CONTEXT const scRoutingContext = useSCRouting(); const { enqueueSnackbar } = useSnackbar(); // REF const feedRef = useRef(); // Hooks const { scGroup, setSCGroup } = useSCFetchGroup({ id: groupId, group }); // HANDLERS const handleComposerSuccess = (feedObject) => { var _a; 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 }); if (((_a = feedObject.group) === null || _a === void 0 ? void 0 : _a.id) === scGroup.id) { // 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 (scGroup) { return Object.assign(Object.assign({}, w), { componentProps: Object.assign(Object.assign({}, w.componentProps), { groupId: scGroup.id }) }); } return w; }), [widgets, scGroup]); if (!scGroup) { return _jsx(GroupFeedSkeleton, {}); } else if (scGroup && scGroup.subscription_status !== SCGroupSubscriptionStatusType.SUBSCRIBED) { return _jsx(GroupInfoWidget, { className: classes.root, groupId: scGroup === null || scGroup === void 0 ? void 0 : scGroup.id }); } return (_jsx(Root, Object.assign({ className: classNames(classes.root, className), id: id, ref: feedRef, endpoint: Object.assign(Object.assign({}, Endpoints.GetGroupFeed), { url: () => Endpoints.GetGroupFeed.url({ id: scGroup.id }) }), widgets: _widgets, ItemComponent: FeedObject, itemPropsGenerator: (scUser, item) => { var _a; return ({ feedObject: item[item.type], feedObjectType: item.type, feedObjectActivities: item.activities ? item.activities : null, markRead: scUser ? !((_a = item === null || item === void 0 ? void 0 : item.seen_by_id) === null || _a === void 0 ? void 0 : _a.includes(scUser.id)) : null }); }, itemIdGenerator: (item) => item[item.type].id, ItemProps: FeedObjectProps, ItemSkeleton: FeedObjectSkeleton, ItemSkeletonProps: { template: SCFeedObjectTemplateType.PREVIEW }, FeedSidebarProps: FeedSidebarProps, HeaderComponent: scGroup && scGroup.subscription_status === SCGroupSubscriptionStatusType.SUBSCRIBED && (_jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess, defaultValue: { group: scGroup }, label: _jsx(FormattedMessage, { id: "templates.groupFeed.composer.label", defaultMessage: "templates.groupFeed.composer.label" }), feedType: SCFeedTypologyType.GROUP })), CustomAdvProps: { position: SCCustomAdvPosition.POSITION_FEED, groupsId: [scGroup.id] }, enabledCustomAdvPositions: [ SCCustomAdvPosition.POSITION_FEED_SIDEBAR, SCCustomAdvPosition.POSITION_FEED, SCCustomAdvPosition.POSITION_BELOW_TOPBAR ] }, FeedProps))); }