2023-10-14 11:39:24 +00:00
|
|
|
import { clsx } from 'clsx'
|
2023-10-20 16:21:40 +00:00
|
|
|
import type { Author, Notification } from '../../../graphql/types.gen'
|
2023-10-14 11:39:24 +00:00
|
|
|
import { createMemo, createSignal, onMount, Show } from 'solid-js'
|
|
|
|
import { NotificationType } from '../../../graphql/types.gen'
|
2023-10-16 17:24:33 +00:00
|
|
|
import { getPagePath, openPage } from '@nanostores/router'
|
|
|
|
import { router, useRouter } from '../../../stores/router'
|
2023-10-14 11:39:24 +00:00
|
|
|
import { useNotifications } from '../../../context/notifications'
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
2023-10-16 17:24:33 +00:00
|
|
|
import type { ArticlePageSearchParams } from '../../Article/FullArticle'
|
2023-10-18 10:56:41 +00:00
|
|
|
import { TimeAgo } from '../../_shared/TimeAgo'
|
|
|
|
import styles from './NotificationView.module.scss'
|
2023-10-20 16:21:40 +00:00
|
|
|
import { GroupAvatar } from '../../_shared/GroupAvatar'
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
notification: Notification
|
|
|
|
onClick: () => void
|
2023-10-18 10:56:41 +00:00
|
|
|
dateTimeFormat: 'ago' | 'time' | 'date'
|
2023-10-14 11:39:24 +00:00
|
|
|
class?: string
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:21:40 +00:00
|
|
|
export type NotificationUser = {
|
|
|
|
id: number
|
|
|
|
name: string
|
|
|
|
slug: string
|
|
|
|
userpic: string
|
|
|
|
}
|
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
type NotificationData = {
|
|
|
|
shout: {
|
|
|
|
slug: string
|
|
|
|
title: string
|
|
|
|
}
|
2023-10-20 16:21:40 +00:00
|
|
|
users: NotificationUser[]
|
2023-10-16 17:24:33 +00:00
|
|
|
reactionIds: number[]
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const NotificationView = (props: Props) => {
|
|
|
|
const {
|
2023-10-16 17:24:33 +00:00
|
|
|
actions: { markNotificationAsRead, hideNotificationsPanel }
|
2023-10-14 11:39:24 +00:00
|
|
|
} = useNotifications()
|
|
|
|
|
2023-10-16 17:24:33 +00:00
|
|
|
const { changeSearchParam } = useRouter<ArticlePageSearchParams>()
|
|
|
|
|
2023-10-18 10:56:41 +00:00
|
|
|
const { t, formatDate, formatTime } = useLocalize()
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
const [data, setData] = createSignal<NotificationData>(null)
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
setTimeout(() => setData(JSON.parse(props.notification.data)))
|
|
|
|
})
|
|
|
|
|
|
|
|
const lastUser = createMemo(() => {
|
|
|
|
if (!data()) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return data().users[data().users.length - 1]
|
|
|
|
})
|
|
|
|
|
2023-10-16 17:24:33 +00:00
|
|
|
const handleLinkClick = (event: MouseEvent) => {
|
|
|
|
event.stopPropagation()
|
|
|
|
hideNotificationsPanel()
|
|
|
|
}
|
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
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) {
|
2023-10-16 17:24:33 +00:00
|
|
|
shoutTitle = `${shoutTitle.trim()}...`
|
|
|
|
|
|
|
|
if (shoutTitle[0] === '«') {
|
|
|
|
shoutTitle += '»'
|
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (props.notification.type) {
|
|
|
|
case NotificationType.NewComment: {
|
2023-10-16 17:24:33 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{t('NotificationNewCommentText1', {
|
|
|
|
commentsCount: props.notification.occurrences
|
|
|
|
})}{' '}
|
|
|
|
<a href={getPagePath(router, 'article', { slug: data().shout.slug })} onClick={handleLinkClick}>
|
|
|
|
{shoutTitle}
|
|
|
|
</a>{' '}
|
|
|
|
{t('NotificationNewCommentText2')}{' '}
|
|
|
|
<a href={getPagePath(router, 'author', { slug: lastUser().slug })} onClick={handleLinkClick}>
|
|
|
|
{lastUser().name}
|
|
|
|
</a>{' '}
|
|
|
|
{t('NotificationNewCommentText3', {
|
|
|
|
restUsersCount: data().users.length - 1
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
)
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
case NotificationType.NewReply: {
|
2023-10-16 17:24:33 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{t('NotificationNewReplyText1', {
|
|
|
|
commentsCount: props.notification.occurrences
|
|
|
|
})}{' '}
|
|
|
|
<a href={getPagePath(router, 'article', { slug: data().shout.slug })} onClick={handleLinkClick}>
|
|
|
|
{shoutTitle}
|
|
|
|
</a>{' '}
|
|
|
|
{t('NotificationNewReplyText2')}{' '}
|
|
|
|
<a href={getPagePath(router, 'author', { slug: lastUser().slug })} onClick={handleLinkClick}>
|
|
|
|
{lastUser().name}
|
|
|
|
</a>{' '}
|
|
|
|
{t('NotificationNewReplyText3', {
|
|
|
|
restUsersCount: data().users.length - 1
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
)
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const handleClick = () => {
|
2023-10-16 17:24:33 +00:00
|
|
|
props.onClick()
|
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
if (!props.notification.seen) {
|
|
|
|
markNotificationAsRead(props.notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
openPage(router, 'article', { slug: data().shout.slug })
|
|
|
|
|
2023-10-16 17:24:33 +00:00
|
|
|
if (data().reactionIds) {
|
|
|
|
changeSearchParam({ commentId: data().reactionIds[0].toString() })
|
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 10:56:41 +00:00
|
|
|
const formattedDateTime = createMemo(() => {
|
|
|
|
switch (props.dateTimeFormat) {
|
|
|
|
case 'ago': {
|
|
|
|
return <TimeAgo date={props.notification.createdAt} />
|
|
|
|
}
|
|
|
|
case 'time': {
|
|
|
|
return formatTime(new Date(props.notification.createdAt))
|
|
|
|
}
|
|
|
|
case 'date': {
|
|
|
|
return formatDate(new Date(props.notification.createdAt), { month: 'numeric', year: '2-digit' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
return (
|
|
|
|
<Show when={data()}>
|
|
|
|
<div
|
|
|
|
class={clsx(styles.NotificationView, props.class, {
|
|
|
|
[styles.seen]: props.notification.seen
|
|
|
|
})}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
2023-10-20 16:21:40 +00:00
|
|
|
<div class={styles.userpic}>
|
|
|
|
<GroupAvatar authors={data().users} />
|
|
|
|
</div>
|
2023-10-14 11:39:24 +00:00
|
|
|
<div>{content()}</div>
|
2023-10-18 10:56:41 +00:00
|
|
|
<div class={styles.timeContainer}>{formattedDateTime()}</div>
|
2023-10-14 11:39:24 +00:00
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
)
|
|
|
|
}
|