import type { ArticlePageSearchParams } from '../../Article/FullArticle' import { getPagePath, openPage } from '@nanostores/router' import { clsx } from 'clsx' import { createMemo, createSignal, onMount, Show } from 'solid-js' import { useLocalize } from '../../../context/localize' import { useNotifications } from '../../../context/notifications' import { Reaction } from '../../../graphql/schema/core.gen' import { Notification } from '../../../graphql/schema/notifier.gen' import { router, useRouter } from '../../../stores/router' import { GroupAvatar } from '../../_shared/GroupAvatar' import { TimeAgo } from '../../_shared/TimeAgo' import styles from './NotificationView.module.scss' type Props = { notification: Notification onClick: () => void dateTimeFormat: 'ago' | 'time' | 'date' class?: string } export const NotificationView = (props: Props) => { const { actions: { markNotificationAsRead, hideNotificationsPanel }, } = useNotifications() const { changeSearchParam } = useRouter() const { t, formatDate, formatTime } = useLocalize() const [data, setData] = createSignal(null) // NOTE: supports only SSMessage.entity == "reaction" onMount(() => { setTimeout(() => setData(JSON.parse(props.notification.payload))) }) const lastUser = createMemo(() => data().created_by) const handleLinkClick = (event: MouseEvent) => { event.stopPropagation() hideNotificationsPanel() } const content = createMemo(() => { if (!data()) { return null } let shoutTitle = '' let i = 0 const shoutTitleWords = data().shout.title.split(' ') while (shoutTitle.length <= 30 && i < shoutTitleWords.length) { shoutTitle += shoutTitleWords[i] + ' ' i++ } if (shoutTitle.length < data().shout.title.length) { shoutTitle = `${shoutTitle.trim()}...` if (shoutTitle[0] === '«') { shoutTitle += '»' } } switch (props.notification.action) { case 'create': { if (data()?.reply_to) { return ( <> {t('NotificationNewReplyText1', { commentsCount: 0, // FIXME: props.notification.occurrences, })}{' '} {shoutTitle} {' '} {t('NotificationNewReplyText2')}{' '} {lastUser().name} {' '} {t('NotificationNewReplyText3', { restUsersCount: 0, // FIXME: data().users.length - 1, })} ) } else { return ( <> {t('NotificationNewCommentText1', { commentsCount: 0, // FIXME: props.notification.occurrences, })}{' '} {shoutTitle} {' '} {t('NotificationNewCommentText2')}{' '} {lastUser().name} {' '} {t('NotificationNewCommentText3', { restUsersCount: 0, // FIXME: data().users.length - 1, })} ) } } default: { return <> } } }) const handleClick = () => { props.onClick() if (!props.notification.seen) { markNotificationAsRead(props.notification) } openPage(router, 'article', { slug: data().shout.slug }) // FIXME: // if (data().reactionIds) { // changeSearchParam({ commentId: data().reactionIds[0].toString() }) // } } const formattedDateTime = createMemo(() => { switch (props.dateTimeFormat) { case 'ago': { return } case 'time': { return formatTime(new Date(props.notification.created_at)) } case 'date': { return formatDate(new Date(props.notification.created_at), { month: 'numeric', year: '2-digit' }) } } }) return (
{content()}
{formattedDateTime()}
) }