import { Show, createMemo, createSignal, onMount, For } from 'solid-js' import { Comment } from './Comment' import styles from '../../styles/Article.module.scss' import { clsx } from 'clsx' import { Loading } from '../_shared/Loading' import { Author, ReactionKind } from '../../graphql/types.gen' import { useSession } from '../../context/session' import CommentEditor from '../_shared/CommentEditor' import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient' import { Button } from '../_shared/Button' import { createStorage } from '@solid-primitives/storage' import { useReactions } from '../../context/reactions' import { byCreated } from '../../utils/sortby' import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated' import { useLocalize } from '../../context/localize' type CommentsOrder = 'createdAt' | 'rating' type Props = { commentAuthors: Author[] shoutSlug: string shoutId: number } export const CommentsTree = (props: Props) => { const [isCommentsLoading, setIsCommentsLoading] = createSignal(false) const [commentsOrder, setCommentsOrder] = createSignal('createdAt') const { reactionEntities, actions: { loadReactionsBy, createReaction } } = useReactions() const { t } = useLocalize() // TODO: server side? const [store, setStore] = createStorage({ api: typeof localStorage === 'undefined' ? {} : localStorage }) const [newReactionsCount, setNewReactionsCount] = createSignal(0) const comments = createMemo(() => Object.values(reactionEntities).filter((reaction) => reaction.kind === 'COMMENT') ) const sortedComments = createMemo(() => { let newSortedComments = [...comments()] newSortedComments = newSortedComments.sort(byCreated) if (commentsOrder() === 'rating') { newSortedComments = newSortedComments.sort((a, b) => { 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 }) } newSortedComments.reverse() return newSortedComments }) const updateNewReactionsCount = () => { const storeValue = Number(store[`${props.shoutSlug}`]) const setVal = () => setStore(`${props.shoutSlug}`, `${comments().length}`) if (!store[`${props.shoutSlug}`]) { setVal() } else if (storeValue < comments().length) { setNewReactionsCount(comments().length - storeValue) setVal() } } const { session } = useSession() onMount(async () => { try { setIsCommentsLoading(true) await loadReactionsBy({ by: { shout: props.shoutSlug } }) updateNewReactionsCount() } finally { setIsCommentsLoading(false) } }) const [submitted, setSubmitted] = createSignal(false) const handleSubmitComment = async (value) => { try { await createReaction({ kind: ReactionKind.Comment, body: value, shout: props.shoutId }) setSubmitted(true) } catch (error) { console.error('[handleCreate reaction]:', error) } } return (
}>

{t('Comments')} {comments().length.toString() || ''} 0}>  +{newReactionsCount()}

    !r.replyTo)}> {(reaction) => ( a.slug === session()?.user.slug))} comment={reaction} /> )}
{t('To write a comment, you must')}  {t('sign up')}  {t('or')}  {t('sign in')}
} > handleSubmitComment(value)} /> ) }