@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
153 lines (146 loc) • 6.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const material_1 = require("@mui/material");
const react_ui_1 = require("@selfcommunity/react-ui");
const api_services_1 = require("@selfcommunity/api-services");
const react_core_1 = require("@selfcommunity/react-core");
const system_1 = require("@mui/system");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const types_1 = require("@selfcommunity/types");
const notistack_1 = require("notistack");
const react_intl_1 = require("react-intl");
const constants_1 = require("./constants");
const classes = {
root: `${constants_1.PREFIX}-root`
};
const Root = (0, material_1.styled)(react_ui_1.Feed, {
name: constants_1.PREFIX,
slot: 'Root'
})(() => ({}));
// Widgets for feed
const WIDGETS = [
{
type: 'widget',
component: react_ui_1.PlatformWidget,
componentProps: {},
column: 'right',
position: 0
},
{
type: 'widget',
component: react_ui_1.LoyaltyProgramWidget,
componentProps: {},
column: 'right',
position: 1
},
{
type: 'widget',
component: react_ui_1.CategoriesSuggestionWidget,
componentProps: {},
column: 'right',
position: 2
},
{
type: 'widget',
component: react_ui_1.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
*/
function MainFeed(inProps) {
// PROPS
const props = (0, system_1.useThemeProps)({
props: inProps,
name: constants_1.PREFIX
});
const { id = 'main_feed', className, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props;
//CONTEXT
const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
const isAdvertisingCustomAdvEnabled = (0, react_core_1.useSCPreferenceEnabled)(react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ENABLED);
const isAdvertisingCustomAdvOnlyForAnonUsersEnabled = (0, react_core_1.useSCPreferenceEnabled)(react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED);
const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
// REF
const feedRef = (0, react_1.useRef)();
/**
* Render advertising above the feed
*/
function renderAdvertising() {
if (isAdvertisingCustomAdvEnabled &&
((isAdvertisingCustomAdvOnlyForAnonUsersEnabled && scUserContext.user === null) || !isAdvertisingCustomAdvOnlyForAnonUsersEnabled)) {
return (0, jsx_runtime_1.jsx)(react_ui_1.CustomAdv, { position: types_1.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((0, jsx_runtime_1.jsx)(react_intl_1.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 ((0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className), ref: feedRef, endpoint: api_services_1.Endpoints.MainFeed, widgets: widgets, ItemComponent: react_ui_1.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: react_ui_1.FeedObjectSkeleton, ItemSkeletonProps: {
template: react_ui_1.SCFeedObjectTemplateType.PREVIEW
}, FeedSidebarProps: FeedSidebarProps, HeaderComponent: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_ui_1.InlineComposerWidget, { onSuccess: handleComposerSuccess }), react_core_1.UserUtils.isAdmin(scUserContext.user) && (0, jsx_runtime_1.jsx)(react_ui_1.OnBoardingWidget, { onGeneratedContent: handleAddGenerationContent }), renderAdvertising()] }), requireAuthentication: true, disablePaginationLinks: true, enabledCustomAdvPositions: [
types_1.SCCustomAdvPosition.POSITION_FEED_SIDEBAR,
types_1.SCCustomAdvPosition.POSITION_FEED,
types_1.SCCustomAdvPosition.POSITION_BELOW_TOPBAR
] }, FeedProps)));
}
exports.default = MainFeed;