UNPKG

@selfcommunity/react-ui

Version:

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

75 lines (74 loc) 5.98 kB
import { __rest } from "tslib"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import React, { useState } from 'react'; import { styled } from '@mui/material/styles'; import { Avatar, Stack, Typography } from '@mui/material'; import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core'; import { SCContributionType, SCNotificationTypologyType } from '@selfcommunity/types'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import Bullet from '../../../shared/Bullet'; import DateTimeAgo from '../../../shared/DateTimeAgo'; import { getContributionSnippet, getRouteData } from '../../../utils/contribution'; import classNames from 'classnames'; import { SCNotificationObjectTemplateType } from '../../../types'; import NotificationItem from '../../../shared/NotificationItem'; import VoteButton from '../../VoteButton'; import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar'; import UserAvatar from '../../../shared/UserAvatar'; import { PREFIX } from '../constants'; const messages = defineMessages({ comment: { id: 'ui.notification.comment.comment', defaultMessage: 'ui.notification.comment.comment' }, nestedComment: { id: 'ui.notification.comment.nestedComment', defaultMessage: 'ui.notification.comment.nestedComment' } }); const classes = { root: `${PREFIX}-comment-root`, avatar: `${PREFIX}-avatar`, username: `${PREFIX}-username`, voteButton: `${PREFIX}-vote-button`, contributionText: `${PREFIX}-contribution-text`, activeAt: `${PREFIX}-active-at`, bullet: `${PREFIX}-bullet` }; const Root = styled(NotificationItem, { name: PREFIX, slot: 'CommentRoot' })(() => ({})); /** * This component render the content of the notification of type comment/nested comment * @param props * @constructor */ export default function CommentNotification(props) { // PROPS const { notificationObject, onVote, id = `n_${props.notificationObject['sid']}`, template = SCNotificationObjectTemplateType.DETAIL, className } = props, rest = __rest(props, ["notificationObject", "onVote", "id", "template", "className"]); // STATE const [openAlert, setOpenAlert] = useState(false); // ROUTING const scRoutingContext = useSCRouting(); //INTL const intl = useIntl(); // HANDLERS const handleVote = (contribution) => { return onVote && onVote(contribution); }; /** * 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.comment.author.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.comment.author) }), { onClick: notificationObject.comment.author.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject.comment.author.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject.comment.author.username, variant: "circular", src: notificationObject.comment.author.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(_Fragment, { children: [_jsx(Link, Object.assign({}, (!notificationObject.comment.author.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.comment.author) }), { onClick: notificationObject.comment.author.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject.comment.author.username })), ' ', notificationObject.type === SCNotificationTypologyType.NESTED_COMMENT ? intl.formatMessage(messages.nestedComment, { b: (...chunks) => _jsx("strong", { children: chunks }) }) : intl.formatMessage(messages.comment, { b: (...chunks) => _jsx("strong", { children: chunks }) })] }), secondary: _jsxs(React.Fragment, { children: [_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes.COMMENT_ROUTE_NAME, getRouteData(notificationObject.comment)) }, { children: _jsx(Typography, Object.assign({ variant: "body2", className: classes.contributionText }, { children: getContributionSnippet(notificationObject.comment) })) })), (template === SCNotificationObjectTemplateType.DETAIL || template === SCNotificationObjectTemplateType.SNIPPET) && (_jsxs(Stack, Object.assign({ direction: "row", justifyContent: "flex-start", alignItems: "center", spacing: 1 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }), _jsx(Bullet, { className: classes.bullet }), _jsx(VoteButton, { className: classes.voteButton, variant: "text", size: "small", contributionId: notificationObject.comment.id, contributionType: SCContributionType.COMMENT, contribution: notificationObject.comment, onVote: handleVote })] })))] }), 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.COMMENT_ROUTE_NAME, getRouteData(notificationObject.comment)) }, { children: _jsx(FormattedMessage, { id: "ui.userToastNotifications.viewContribution", defaultMessage: 'ui.userToastNotifications.viewContribution' }) })) }))] }))) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] })); }