UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

91 lines (83 loc) 3.63 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useRef, useState } from 'react'; import { styled } from '@mui/material/styles'; import { Button, CardContent } from '@mui/material'; import PubSub from 'pubsub-js'; import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; import Widget from '../Widget'; import { useThemeProps } from '@mui/system'; import HiddenPlaceholder from '../../shared/HiddenPlaceholder'; import { PREFIX } from './constants'; const classes = { root: `${PREFIX}-root`, buttonLoadMore: `${PREFIX}-button-load-more` }; const Root = styled(Widget, { name: PREFIX, slot: 'Root' })(() => ({})); /** * > API documentation for the Community-JS Feed Updates Widget component. Learn about the available props and the CSS API. * * * This component allows the subscription to updates from PubSub channel (e.g. websocket) and tell the feed to updates if necessary. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/FeedUpdates) #### Import ```jsx import {FeedUpdatesWidget} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCFeedUpdatesWidget` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCFeedUpdatesWidget-root|Styles applied to the root element.| |buttonLoadMore|.SCFeedUpdatesWidget-button-load-more|Styles applied to the to load more button.| * * @param inProps */ export default function FeedUpdatesWidget(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { id = 'feed_updates', className = null, message = _jsx(FormattedMessage, { id: "ui.feedUpdatesWidget.message", defaultMessage: "ui.feedUpdatesWidget.message" }), subscriptionChannel, subscriptionChannelUpdatesCallback = (msg, data) => true, publicationChannel = null, onHeightChange } = props, rest = __rest(props, ["id", "className", "message", "subscriptionChannel", "subscriptionChannelUpdatesCallback", "publicationChannel", "onHeightChange"]); // STATE const [updates, setUpdates] = useState(false); // REFS const updatesSubscription = useRef(null); // Subscriber for pubsub callback const subscriber = (msg, data) => { if (subscriptionChannelUpdatesCallback(msg, data)) { setUpdates(true); } }; /** * On mount, fetches first page of notifications * On mount, subscribe to receive notification updates */ useEffect(() => { updatesSubscription.current = PubSub.subscribe(subscriptionChannel, subscriber); return () => { PubSub.unsubscribe(updatesSubscription.current); }; }, []); /** * Virtual Feed update */ useEffect(() => { onHeightChange && onHeightChange(); }, [updates]); if (!updates) { return _jsx(HiddenPlaceholder, {}); } // HANDLERS const handleClick = () => { PubSub.publishSync(publicationChannel, { refresh: true }); setUpdates(false); }; return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className) }, rest, { children: _jsxs(CardContent, { children: [message, publicationChannel && (_jsx(Button, Object.assign({ variant: "text", color: "inherit", onClick: handleClick, classes: { root: classes.buttonLoadMore } }, { children: _jsx(FormattedMessage, { id: "ui.feedUpdatesWidget.reload", defaultMessage: "ui.feedUpdatesWidget.reload" }) })))] }) }))); }