webapp/src/components/Article/CommentsTree.tsx

187 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-02-07 12:48:45 +00:00
import { Show, createMemo, createSignal, onMount, For } from 'solid-js'
2023-02-17 09:21:02 +00:00
import { Comment } from './Comment'
2023-03-08 16:35:13 +00:00
import styles from './Article.module.scss'
2022-11-26 21:27:54 +00:00
import { clsx } from 'clsx'
2023-02-21 12:32:51 +00:00
import { Author, Reaction, ReactionKind } from '../../graphql/types.gen'
import { useSession } from '../../context/session'
import CommentEditor from '../_shared/CommentEditor'
2023-02-17 09:21:02 +00:00
import { Button } from '../_shared/Button'
import { useReactions } from '../../context/reactions'
import { byCreated } from '../../utils/sortby'
import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated'
import { useLocalize } from '../../context/localize'
2022-11-26 16:51:08 +00:00
2023-02-21 12:32:51 +00:00
type CommentsOrder = 'createdAt' | 'rating' | 'newOnly'
2022-11-26 16:51:08 +00:00
2023-03-04 17:26:28 +00:00
const sortCommentsByRating = (a: Reaction, b: Reaction): -1 | 0 | 1 => {
if (a.replyTo && b.replyTo) {
return 0
}
const x = (a?.stat && a.stat.rating) || 0
const y = (b?.stat && b.stat.rating) || 0
if (x > y) {
return 1
}
if (x < y) {
return -1
}
return 0
}
type Props = {
commentAuthors: Author[]
shoutSlug: string
shoutId: number
}
export const CommentsTree = (props: Props) => {
2023-02-17 09:21:02 +00:00
const [commentsOrder, setCommentsOrder] = createSignal<CommentsOrder>('createdAt')
const {
reactionEntities,
2023-02-28 17:13:14 +00:00
actions: { createReaction }
2023-02-17 09:21:02 +00:00
} = useReactions()
2023-02-11 10:49:31 +00:00
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2023-02-21 12:32:51 +00:00
const [newReactions, setNewReactions] = createSignal<Reaction[]>([])
2023-02-17 09:21:02 +00:00
const comments = createMemo(() =>
Object.values(reactionEntities).filter((reaction) => reaction.kind === 'COMMENT')
)
const sortedComments = createMemo(() => {
let newSortedComments = [...comments()]
newSortedComments = newSortedComments.sort(byCreated)
2023-03-04 17:26:28 +00:00
if (commentsOrder() === 'newOnly') {
return newReactions().reverse()
2023-02-17 09:21:02 +00:00
}
2023-03-04 17:26:28 +00:00
if (commentsOrder() === 'rating') {
newSortedComments = newSortedComments.sort(sortCommentsByRating)
2023-02-21 12:32:51 +00:00
}
2023-02-17 09:21:02 +00:00
newSortedComments.reverse()
return newSortedComments
})
2023-03-06 14:06:48 +00:00
const dateFromLocalStorage = new Date(localStorage.getItem(`${props.shoutSlug}`))
const currentDate = new Date()
const setCookie = () => localStorage.setItem(`${props.shoutSlug}`, `${currentDate}`)
onMount(() => {
if (!dateFromLocalStorage) {
2023-02-21 12:32:51 +00:00
setCookie()
2023-03-06 14:06:48 +00:00
} else if (currentDate > dateFromLocalStorage) {
2023-02-21 12:32:51 +00:00
const newComments = comments().filter((c) => {
2023-02-28 17:13:14 +00:00
if (c.replyTo) {
return
}
2023-03-06 14:06:48 +00:00
const created = new Date(c.createdAt)
return created > dateFromLocalStorage
2023-02-21 12:32:51 +00:00
})
setNewReactions(newComments)
setCookie()
2023-02-11 10:49:31 +00:00
}
2023-02-17 09:21:02 +00:00
})
2023-03-06 14:06:48 +00:00
const { session } = useSession()
const [submitted, setSubmitted] = createSignal<boolean>(false)
const handleSubmitComment = async (value) => {
try {
2023-02-17 09:21:02 +00:00
await createReaction({
kind: ReactionKind.Comment,
body: value,
shout: props.shoutId
})
setSubmitted(true)
} catch (error) {
console.error('[handleCreate reaction]:', error)
}
}
2022-11-26 16:51:08 +00:00
return (
2023-02-28 17:13:14 +00:00
<>
<div class={styles.commentsHeaderWrapper}>
<h2 class={styles.commentsHeader}>
2023-02-28 17:13:14 +00:00
{t('Comments')} {comments().length.toString() || ''}
2023-03-06 14:06:48 +00:00
<Show when={newReactions().length > 0}>
<span class={styles.newReactions}>&nbsp;+{newReactions().length}</span>
2023-02-28 17:13:14 +00:00
</Show>
</h2>
<ul class={clsx(styles.commentsViewSwitcher, 'view-switcher')}>
2023-03-06 14:06:48 +00:00
<Show when={newReactions().length > 0}>
2023-02-28 17:13:14 +00:00
<li classList={{ selected: commentsOrder() === 'newOnly' }}>
2023-02-07 12:48:45 +00:00
<Button
variant="inline"
2023-02-28 17:13:14 +00:00
value={t('New only')}
2023-02-07 12:48:45 +00:00
onClick={() => {
2023-02-28 17:13:14 +00:00
setCommentsOrder('newOnly')
}}
2023-04-04 00:09:56 +00:00
class={styles.commentsViewSwitcherButton}
2023-02-07 12:48:45 +00:00
/>
2022-11-26 21:27:54 +00:00
</li>
2023-02-28 17:13:14 +00:00
</Show>
<li classList={{ selected: commentsOrder() === 'createdAt' }}>
<Button
variant="inline"
value={t('By time')}
onClick={() => {
setCommentsOrder('createdAt')
}}
2023-04-04 00:09:56 +00:00
class={styles.commentsViewSwitcherButton}
2023-02-28 17:13:14 +00:00
/>
</li>
<li classList={{ selected: commentsOrder() === 'rating' }}>
<Button
variant="inline"
value={t('By rating')}
onClick={() => {
setCommentsOrder('rating')
}}
2023-04-04 00:09:56 +00:00
class={styles.commentsViewSwitcherButton}
2023-02-28 17:13:14 +00:00
/>
</li>
</ul>
2023-02-28 17:13:14 +00:00
</div>
<ul class={styles.comments}>
<For each={sortedComments().filter((r) => !r.replyTo)}>
{(reaction) => (
<Comment
sortedComments={sortedComments()}
isArticleAuthor={Boolean(props.commentAuthors.some((a) => a.slug === session()?.user.slug))}
comment={reaction}
2023-03-06 14:06:48 +00:00
lastSeen={dateFromLocalStorage}
2023-02-28 17:13:14 +00:00
/>
)}
</For>
</ul>
<ShowIfAuthenticated
fallback={
<div class={styles.signInMessage}>
2023-03-10 17:42:48 +00:00
{t('To write a comment, you must')}{' '}
2023-02-28 17:13:14 +00:00
<a href="?modal=auth&mode=register" class={styles.link}>
{t('sign up')}
2023-03-10 17:42:48 +00:00
</a>{' '}
{t('or')}&nbsp;
2023-02-28 17:13:14 +00:00
<a href="?modal=auth&mode=login" class={styles.link}>
{t('sign in')}
</a>
</div>
}
>
<CommentEditor
placeholder={t('Write a comment...')}
clear={submitted()}
onSubmit={(value) => handleSubmitComment(value)}
/>
</ShowIfAuthenticated>
</>
2022-11-26 16:51:08 +00:00
)
}