@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
149 lines (142 loc) • 6.02 kB
JavaScript
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, CustomAdv } from '@selfcommunity/react-ui';
import { Endpoints } from '@selfcommunity/api-services';
import { SCPreferences, SCUserContext, UserUtils, useSCPreferenceEnabled } 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 isAdvertisingCustomAdvEnabled = useSCPreferenceEnabled(SCPreferences.ADVERTISING_CUSTOM_ADV_ENABLED);
const isAdvertisingCustomAdvOnlyForAnonUsersEnabled = useSCPreferenceEnabled(SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED);
const { enqueueSnackbar } = useSnackbar();
// REF
const feedRef = useRef();
/**
* Render advertising above the feed
*/
function renderAdvertising() {
if (isAdvertisingCustomAdvEnabled &&
((isAdvertisingCustomAdvOnlyForAnonUsersEnabled && scUserContext.user === null) || !isAdvertisingCustomAdvOnlyForAnonUsersEnabled)) {
return _jsx(CustomAdv, { position: SCCustomAdvPosition.POSITION_ABOVE_FEED });
}
return null;
}
// Ckeck user is authenticated
if (!scUserContext.user) {
return 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);
};
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: false,
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 : 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 }), renderAdvertising()] }), requireAuthentication: true, disablePaginationLinks: true, enabledCustomAdvPositions: [
SCCustomAdvPosition.POSITION_FEED_SIDEBAR,
SCCustomAdvPosition.POSITION_FEED,
SCCustomAdvPosition.POSITION_BELOW_TOPBAR
] }, FeedProps)));
}