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-20 04:40:55 +00:00
|
|
|
import GrowingTextarea from '../_shared/GrowingTextarea'
|
|
|
|
import { ReactionKind } from '../../graphql/types.gen'
|
|
|
|
import { useSession } from '../../context/session'
|
2022-11-26 16:51:08 +00:00
|
|
|
|
|
|
|
const ARTICLE_COMMENTS_PAGE_SIZE = 50
|
|
|
|
const MAX_COMMENT_LEVEL = 6
|
|
|
|
|
2023-01-20 04:40:55 +00:00
|
|
|
export const CommentsTree = (props: { shoutSlug: string; shoutId: number }) => {
|
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
|
|
|
|
|
|
|
const [loading, setLoading] = createSignal<boolean>(false)
|
|
|
|
const [errorMessage, setErrorMessage] = createSignal<string | null>(null)
|
|
|
|
const handleSubmitComment = async (value) => {
|
|
|
|
try {
|
|
|
|
setLoading(true)
|
|
|
|
await createReaction(
|
|
|
|
{
|
|
|
|
kind: ReactionKind.Comment,
|
|
|
|
body: value,
|
|
|
|
shout: props.shoutId
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: session().user.name,
|
|
|
|
userpic: session().user.userpic,
|
|
|
|
slug: session().user.slug
|
|
|
|
}
|
|
|
|
)
|
|
|
|
setLoading(false)
|
|
|
|
} catch (error) {
|
|
|
|
setErrorMessage(t('Something went wrong, please try again'))
|
|
|
|
console.error('[handleCreate reaction]:', error)
|
|
|
|
}
|
|
|
|
}
|
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)}
|
|
|
|
>
|
|
|
|
{(reaction) => <Comment reactions={reactions()} comment={reaction} />}
|
|
|
|
</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-20 04:40:55 +00:00
|
|
|
<GrowingTextarea
|
|
|
|
placeholder={t('Write comment')}
|
|
|
|
submitButtonText={t('Send')}
|
|
|
|
cancelButtonText={t('cancel')}
|
|
|
|
submit={(value) => handleSubmitComment(value)}
|
|
|
|
loading={loading()}
|
|
|
|
errorMessage={errorMessage()}
|
|
|
|
/>
|
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
|
|
|
)
|
|
|
|
}
|