2022-12-06 16:03:55 +00:00
|
|
|
import { For, Show, createMemo, createSignal, onMount } from 'solid-js'
|
2022-11-26 16:51:08 +00:00
|
|
|
import Comment from './Comment'
|
|
|
|
import { t } from '../../utils/intl'
|
|
|
|
import styles from '../../styles/Article.module.scss'
|
2023-01-20 04:40:55 +00:00
|
|
|
import { createReaction, useReactionsStore } from '../../stores/zine/reactions'
|
2022-11-26 16:51:08 +00:00
|
|
|
import type { Reaction } from '../../graphql/types.gen'
|
2022-11-26 21:27:54 +00:00
|
|
|
import { clsx } from 'clsx'
|
2022-11-27 08:15:03 +00:00
|
|
|
import { byCreated, byStat } from '../../utils/sortby'
|
|
|
|
import { Loading } from '../Loading'
|
2023-01-26 06:59:43 +00:00
|
|
|
import { Author, ReactionKind } from '../../graphql/types.gen'
|
2023-01-20 04:40:55 +00:00
|
|
|
import { useSession } from '../../context/session'
|
2023-01-26 06:59:43 +00:00
|
|
|
import CommentEditor from '../_shared/CommentEditor'
|
|
|
|
import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient'
|
2022-11-26 16:51:08 +00:00
|
|
|
|
|
|
|
const ARTICLE_COMMENTS_PAGE_SIZE = 50
|
|
|
|
const MAX_COMMENT_LEVEL = 6
|
|
|
|
|
2023-01-26 06:59:43 +00:00
|
|
|
type Props = {
|
|
|
|
commentAuthors: Author[]
|
|
|
|
shoutSlug: string
|
|
|
|
shoutId: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CommentsTree = (props: Props) => {
|
2022-11-26 16:51:08 +00:00
|
|
|
const [getCommentsPage, setCommentsPage] = createSignal(0)
|
2022-11-27 08:15:03 +00:00
|
|
|
const [commentsOrder, setCommentsOrder] = createSignal<'rating' | 'createdAt'>('createdAt')
|
2022-11-26 16:51:08 +00:00
|
|
|
const [isCommentsLoading, setIsCommentsLoading] = createSignal(false)
|
|
|
|
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
|
2022-12-06 16:03:55 +00:00
|
|
|
const { sortedReactions, loadReactionsBy } = useReactionsStore()
|
2022-11-27 08:15:03 +00:00
|
|
|
const reactions = createMemo<Reaction[]>(() =>
|
2023-01-20 04:40:55 +00:00
|
|
|
sortedReactions().sort(commentsOrder() === 'rating' ? byStat('rating') : byCreated)
|
2022-11-27 08:15:03 +00:00
|
|
|
)
|
2023-01-20 04:40:55 +00:00
|
|
|
const { session } = useSession()
|
2022-11-26 16:51:08 +00:00
|
|
|
const loadMore = async () => {
|
|
|
|
try {
|
|
|
|
const page = getCommentsPage()
|
|
|
|
setIsCommentsLoading(true)
|
|
|
|
const { hasMore } = await loadReactionsBy({
|
2022-12-06 16:03:55 +00:00
|
|
|
by: { shout: props.shoutSlug, comment: true },
|
2022-11-26 16:51:08 +00:00
|
|
|
limit: ARTICLE_COMMENTS_PAGE_SIZE,
|
|
|
|
offset: page * ARTICLE_COMMENTS_PAGE_SIZE
|
|
|
|
})
|
|
|
|
setIsLoadMoreButtonVisible(hasMore)
|
|
|
|
} finally {
|
|
|
|
setIsCommentsLoading(false)
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 11:01:23 +00:00
|
|
|
const getCommentById = (cid: number) => reactions().find((r: Reaction) => r.id === cid)
|
2022-11-26 16:51:08 +00:00
|
|
|
const getCommentLevel = (c: Reaction, level = 0) => {
|
|
|
|
if (c && c.replyTo && level < MAX_COMMENT_LEVEL) {
|
|
|
|
return getCommentLevel(getCommentById(c.replyTo), level + 1)
|
|
|
|
}
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
onMount(async () => await loadMore())
|
2023-01-20 04:40:55 +00:00
|
|
|
|
2023-01-26 06:59:43 +00:00
|
|
|
const [submitted, setSubmitted] = createSignal<boolean>(false)
|
2023-01-20 04:40:55 +00:00
|
|
|
const handleSubmitComment = async (value) => {
|
|
|
|
try {
|
|
|
|
await createReaction(
|
|
|
|
{
|
|
|
|
kind: ReactionKind.Comment,
|
|
|
|
body: value,
|
|
|
|
shout: props.shoutId
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: session().user.name,
|
|
|
|
userpic: session().user.userpic,
|
|
|
|
slug: session().user.slug
|
|
|
|
}
|
|
|
|
)
|
2023-01-26 06:59:43 +00:00
|
|
|
setSubmitted(true)
|
2023-01-20 04:40:55 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('[handleCreate reaction]:', error)
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 06:59:43 +00:00
|
|
|
|
2022-11-26 16:51:08 +00:00
|
|
|
return (
|
2023-01-20 04:40:55 +00:00
|
|
|
<div>
|
2022-11-27 08:15:03 +00:00
|
|
|
<Show when={!isCommentsLoading()} fallback={<Loading />}>
|
2022-11-26 21:27:54 +00:00
|
|
|
<div class={styles.commentsHeaderWrapper}>
|
|
|
|
<h2 id="comments" class={styles.commentsHeader}>
|
|
|
|
{t('Comments')} {reactions().length.toString() || ''}
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
<ul class={clsx(styles.commentsViewSwitcher, 'view-switcher')}>
|
2022-11-27 08:15:03 +00:00
|
|
|
<li classList={{ selected: commentsOrder() === 'createdAt' || !commentsOrder() }}>
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
onClick={(ev) => {
|
|
|
|
ev.preventDefault()
|
|
|
|
setCommentsOrder('createdAt')
|
|
|
|
}}
|
|
|
|
>
|
2022-11-29 11:01:23 +00:00
|
|
|
{t('By time')}
|
2022-11-27 08:15:03 +00:00
|
|
|
</a>
|
2022-11-26 21:27:54 +00:00
|
|
|
</li>
|
2022-11-27 08:15:03 +00:00
|
|
|
<li classList={{ selected: commentsOrder() === 'rating' }}>
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
onClick={(ev) => {
|
|
|
|
ev.preventDefault()
|
|
|
|
setCommentsOrder('rating')
|
|
|
|
}}
|
|
|
|
>
|
2022-11-29 11:01:23 +00:00
|
|
|
{t('By rating')}
|
2022-11-27 08:15:03 +00:00
|
|
|
</a>
|
2022-11-26 21:27:54 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2023-01-20 04:40:55 +00:00
|
|
|
<ul class={styles.comments}>
|
|
|
|
<For
|
|
|
|
each={reactions()
|
|
|
|
.reverse()
|
|
|
|
.filter((r) => !r.replyTo)}
|
|
|
|
>
|
2023-01-26 06:59:43 +00:00
|
|
|
{(reaction) => (
|
|
|
|
<Comment
|
|
|
|
isArticleAuthor={Boolean(props.commentAuthors.some((a) => a.slug === session()?.user.slug))}
|
|
|
|
reactions={reactions()}
|
|
|
|
comment={reaction}
|
|
|
|
/>
|
|
|
|
)}
|
2023-01-20 04:40:55 +00:00
|
|
|
</For>
|
|
|
|
</ul>
|
2022-11-26 16:51:08 +00:00
|
|
|
<Show when={isLoadMoreButtonVisible()}>
|
2022-12-03 07:15:13 +00:00
|
|
|
<button onClick={loadMore}>{t('Load more')}</button>
|
2022-11-26 16:51:08 +00:00
|
|
|
</Show>
|
2023-01-26 06:59:43 +00:00
|
|
|
<ShowOnlyOnClient>
|
|
|
|
<CommentEditor
|
|
|
|
initialValue={t('Write a comment...')}
|
|
|
|
clear={submitted()}
|
|
|
|
onSubmit={(value) => handleSubmitComment(value)}
|
|
|
|
/>
|
|
|
|
</ShowOnlyOnClient>
|
2022-11-26 16:51:08 +00:00
|
|
|
</Show>
|
2023-01-20 04:40:55 +00:00
|
|
|
</div>
|
2022-11-26 16:51:08 +00:00
|
|
|
)
|
|
|
|
}
|