@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
62 lines (61 loc) • 5.19 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Box, Stack, Typography } from '@mui/material';
import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { getRouteData, getContributionType, getContributionSnippet, getContribution } from '../../../utils/contribution';
import DateTimeAgo from '../../../shared/DateTimeAgo';
import classNames from 'classnames';
import { SCNotificationObjectTemplateType } from '../../../types';
import NotificationItem from '../../../shared/NotificationItem';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
quotedYouOn: {
id: 'ui.notification.mention.quotedYou',
defaultMessage: 'ui.notification.mention.quotedYou'
}
});
const classes = {
root: `${PREFIX}-mention-root`,
avatar: `${PREFIX}-avatar`,
username: `${PREFIX}-username`,
mentionText: `${PREFIX}-mention-text`,
activeAt: `${PREFIX}-active-at`,
contributionText: `${PREFIX}-contribution-text`
};
const Root = styled(NotificationItem, {
name: PREFIX,
slot: 'MentionRoot'
})(() => ({}));
/**
* This component render the content of the notification of type mention
* @constructor
* @param props
*/
export default function MentionNotification(props) {
// PROPS
const { notificationObject, id = `n_${props.notificationObject['sid']}`, className, template = SCNotificationObjectTemplateType.DETAIL } = props, rest = __rest(props, ["notificationObject", "id", "className", "template"]);
// CONTEXT
const scRoutingContext = useSCRouting();
// STATE
const [openAlert, setOpenAlert] = useState(false);
// CONST
const objectType = getContributionType(notificationObject);
const contribution = getContribution(notificationObject);
// INTL
const intl = useIntl();
/**
* Renders root object
*/
return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className, `${PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, image: _jsx(Link, Object.assign({}, (!notificationObject[objectType].author.deleted && {
to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject[objectType].author)
}), { onClick: notificationObject[objectType].author.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject[objectType].author.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject[objectType].author.username, variant: "circular", src: notificationObject[objectType].author.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(Typography, Object.assign({ component: "div", color: "inherit", className: classes.mentionText }, { children: [_jsx(Link, Object.assign({}, (!notificationObject[objectType].author.deleted && {
to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject[objectType].author)
}), { onClick: notificationObject[objectType].author.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject[objectType].author.username })), ' ', intl.formatMessage(messages.quotedYouOn, {
b: (...chunks) => _jsx("strong", { children: chunks })
}), ' '] })), secondary: _jsxs(React.Fragment, { children: [_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${objectType.toUpperCase()}_ROUTE_NAME`], getRouteData(notificationObject[objectType])) }, { children: _jsx(Typography, Object.assign({ component: 'span', variant: "body2", className: classes.contributionText }, { children: getContributionSnippet(notificationObject[objectType]) })) })), (template === SCNotificationObjectTemplateType.DETAIL || template === SCNotificationObjectTemplateType.SNIPPET) && (_jsx(Box, { children: _jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }) }))] }), footer: template === SCNotificationObjectTemplateType.TOAST && (_jsxs(Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at }), _jsx(Typography, Object.assign({ color: "primary", component: 'div' }, { children: _jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${contribution.type.toUpperCase()}_ROUTE_NAME`], getRouteData(contribution)) }, { children: _jsx(FormattedMessage, { id: "ui.userToastNotifications.viewContribution", defaultMessage: 'ui.userToastNotifications.viewContribution' }) })) }))] }))) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}