@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
143 lines (136 loc) • 7.3 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 types_1 = require("@selfcommunity/types");
const index_1 = require("./index");
const system_1 = require("@mui/system");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_intl_1 = require("react-intl");
const notistack_1 = require("notistack");
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.CategoryTrendingUsersWidget,
componentProps: {},
column: 'right',
position: 0
},
{
type: 'widget',
component: react_ui_1.CategoryTrendingFeedWidget,
componentProps: {},
column: 'right',
position: 1
}
];
/**
* > API documentation for the Community-JS Category Feed Template. Learn about the available props and the CSS API.
*
*
* This component renders a specific category's feed.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-templates/Components/CategoryFeed)
#### Import
```jsx
import {CategoryFeed} from '@selfcommunity/react-templates';
```
#### Component Name
The name `SCCategoryFeedTemplate` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCCategoryFeedTemplate-root|Styles applied to the root element.|
*
* @param inProps
*/
function CategoryFeed(inProps) {
var _a;
// PROPS
const props = (0, system_1.useThemeProps)({
props: inProps,
name: constants_1.PREFIX
});
const { id = 'category_feed', className, category, categoryId, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props;
// CONTEXT
const scRoutingContext = (0, react_core_1.useSCRouting)();
const scUserContext = (0, react_core_1.useSCUser)();
const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
// REF
const feedRef = (0, react_1.useRef)();
// Hooks
const { scCategory } = (0, react_core_1.useSCFetchCategory)({ id: categoryId, category });
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 isPaymentsEnabled = (0, react_core_1.useSCPreferencesAndFeaturesEnabled)([react_core_1.SCPreferences.CONFIGURATIONS_PAYMENTS_ENABLED], [types_1.SCFeatureName.PAYMENTS]);
/**
* 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, categoriesId: [scCategory.id] });
}
return null;
}
// HANDLERS
const handleComposerSuccess = (feedObject) => {
// Not insert if the category does not match
if (feedObject.categories.findIndex((c) => c.id === scCategory.id) === -1) {
const messageId = feedObject.scheduled_at ? 'ui.composer.scheduled.success' : 'ui.composerIconButton.composer.success';
enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: messageId, defaultMessage: messageId }), {
action: () => ((0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ to: scRoutingContext.url(react_core_1.SCRoutes[`${feedObject.type.toUpperCase()}_ROUTE_NAME`], react_ui_1.ContributionUtils.getRouteData(feedObject)) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composerIconButton.composer.viewContribute", defaultMessage: "ui.composerIconButton.composer.viewContribute" }) }))),
variant: 'success',
autoHideDuration: 7000
});
return;
}
// 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 = (0, react_1.useMemo)(() => widgets.map((w) => {
if (scCategory) {
return Object.assign(Object.assign({}, w), { componentProps: Object.assign(Object.assign({}, w.componentProps), { categoryId: scCategory.id }) });
}
return w;
}), [widgets, scCategory]);
if (!scCategory) {
return (0, jsx_runtime_1.jsx)(index_1.CategoryFeedSkeleton, {});
}
else if (scCategory && isPaymentsEnabled && !scCategory.followed && !scCategory.payment_order && ((_a = scCategory.paywalls) === null || _a === void 0 ? void 0 : _a.length) > 0) {
return (0, jsx_runtime_1.jsx)(react_ui_1.HiddenPurchasableContent, {});
}
return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className), ref: feedRef, endpoint: Object.assign(Object.assign({}, api_services_1.Endpoints.CategoryFeed), { url: () => api_services_1.Endpoints.CategoryFeed.url({ id: scCategory.id }) }), 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: [((scCategory.content_only_staff && react_core_1.UserUtils.isStaff(scUserContext.user)) || !scCategory.content_only_staff) && ((0, jsx_runtime_1.jsx)(react_ui_1.InlineComposerWidget, { onSuccess: handleComposerSuccess, defaultValue: { categories: [scCategory] }, feedType: types_1.SCFeedTypologyType.CATEGORY })), renderAdvertising()] }), CustomAdvProps: { categoriesId: [scCategory.id] }, enabledCustomAdvPositions: [
types_1.SCCustomAdvPosition.POSITION_FEED_SIDEBAR,
types_1.SCCustomAdvPosition.POSITION_FEED,
types_1.SCCustomAdvPosition.POSITION_BELOW_TOPBAR
] }, FeedProps)));
}
exports.default = CategoryFeed;