import { For, Show } from 'solid-js' import { useSession } from '../../context/session' import Comment from './Comment' import { t } from '../../utils/intl' import { showModal } from '../../stores/ui' import styles from '../../styles/Article.module.scss' import { useReactionsStore } from '../../stores/zine/reactions' import { createMemo, createSignal, onMount } from 'solid-js' import type { Reaction } from '../../graphql/types.gen' import { clsx } from 'clsx' import { byCreated, byStat } from '../../utils/sortby' import { Loading } from '../Loading' const ARTICLE_COMMENTS_PAGE_SIZE = 50 const MAX_COMMENT_LEVEL = 6 export const CommentsTree = (props: { shout: string; reactions?: Reaction[] }) => { const [getCommentsPage, setCommentsPage] = createSignal(0) const [commentsOrder, setCommentsOrder] = createSignal<'rating' | 'createdAt'>('createdAt') const [isCommentsLoading, setIsCommentsLoading] = createSignal(false) const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false) const { session } = useSession() const { sortedReactions, loadReactionsBy } = useReactionsStore({ reactions: props.reactions }) const reactions = createMemo(() => sortedReactions() .sort(commentsOrder() === 'rating' ? byStat('rating') : byCreated) .filter((r) => r.shout.slug === props.shout) ) const loadMore = async () => { try { const page = getCommentsPage() setIsCommentsLoading(true) const { hasMore } = await loadReactionsBy({ by: { shout: props.shout, 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()) return ( <> }> {(reaction: Reaction) => ( )}
} >
) }