import { For, Show, createMemo, createSignal, onMount } from 'solid-js' import Comment from './Comment' import { t } from '../../utils/intl' import styles from '../../styles/Article.module.scss' import { createReaction, useReactionsStore } from '../../stores/zine/reactions' import type { Reaction } from '../../graphql/types.gen' import { clsx } from 'clsx' import { byCreated, byStat } from '../../utils/sortby' import { Loading } from '../Loading' import { Author, ReactionKind } from '../../graphql/types.gen' import { useSession } from '../../context/session' import CommentEditor from '../_shared/CommentEditor' import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient' const ARTICLE_COMMENTS_PAGE_SIZE = 50 const MAX_COMMENT_LEVEL = 6 type Props = { commentAuthors: Author[] shoutSlug: string shoutId: number } export const CommentsTree = (props: Props) => { const [getCommentsPage, setCommentsPage] = createSignal(0) const [commentsOrder, setCommentsOrder] = createSignal<'rating' | 'createdAt'>('createdAt') const [isCommentsLoading, setIsCommentsLoading] = createSignal(false) const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false) const { sortedReactions, loadReactionsBy } = useReactionsStore() const reactions = createMemo(() => sortedReactions().sort(commentsOrder() === 'rating' ? byStat('rating') : byCreated) ) const { session } = useSession() const loadMore = async () => { try { const page = getCommentsPage() setIsCommentsLoading(true) const { hasMore } = await loadReactionsBy({ by: { shout: props.shoutSlug, comment: true }, limit: ARTICLE_COMMENTS_PAGE_SIZE, offset: page * ARTICLE_COMMENTS_PAGE_SIZE }) setIsLoadMoreButtonVisible(hasMore) } finally { setIsCommentsLoading(false) } } const getCommentById = (cid: number) => reactions().find((r: Reaction) => r.id === cid) 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()) const [submitted, setSubmitted] = createSignal(false) 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 } ) setSubmitted(true) } catch (error) { console.error('[handleCreate reaction]:', error) } } return (
}>
    !r.replyTo)} > {(reaction) => ( a.slug === session()?.user.slug))} reactions={reactions()} comment={reaction} /> )}
handleSubmitComment(value)} />
) }