import { clsx } from 'clsx' import { For, Show, createMemo, createSignal, lazy, onMount } from 'solid-js' import { useLocalize } from '../../context/localize' import { useReactions } from '../../context/reactions' import { useSession } from '../../context/session' import { Author, Reaction, ReactionKind, ReactionSort } from '../../graphql/schema/core.gen' import { byCreated, byStat } from '../../utils/sortby' import { Button } from '../_shared/Button' import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated' import { Comment } from './Comment' import styles from './Article.module.scss' const SimplifiedEditor = lazy(() => import('../Editor/SimplifiedEditor')) type Props = { articleAuthors: Author[] shoutSlug: string shoutId: number } export const CommentsTree = (props: Props) => { const { author } = useSession() const { t } = useLocalize() const [commentsOrder, setCommentsOrder] = createSignal(ReactionSort.Newest) const [onlyNew, setOnlyNew] = createSignal(false) const [newReactions, setNewReactions] = createSignal([]) const [clearEditor, setClearEditor] = createSignal(false) const [clickedReplyId, setClickedReplyId] = createSignal() const { reactionEntities, createReaction, loadReactionsBy } = useReactions() const comments = createMemo(() => Object.values(reactionEntities).filter((reaction) => reaction.kind === 'COMMENT'), ) const sortedComments = createMemo(() => { let newSortedComments = [...comments()] newSortedComments = newSortedComments.sort(byCreated) if (onlyNew()) { return newReactions().sort(byCreated).reverse() } if (commentsOrder() === ReactionSort.Like) { newSortedComments = newSortedComments.sort(byStat('rating')) } return newSortedComments }) const dateFromLocalStorage = Number.parseInt(localStorage.getItem(`${props.shoutSlug}`)) const currentDate = new Date() const setCookie = () => localStorage.setItem(`${props.shoutSlug}`, `${currentDate}`) onMount(() => { if (!dateFromLocalStorage) { setCookie() } else if (currentDate.getTime() > dateFromLocalStorage) { const newComments = comments().filter((c) => { if (c.reply_to || c.created_by.slug === author()?.slug) { return } const created = c.created_at return created > dateFromLocalStorage }) setNewReactions(newComments) setCookie() } }) const [posting, setPosting] = createSignal(false) const handleSubmitComment = async (value: string) => { setPosting(true) try { await createReaction({ kind: ReactionKind.Comment, body: value, shout: props.shoutId, }) setClearEditor(true) await loadReactionsBy({ by: { shout: props.shoutSlug } }) } catch (error) { console.error('[handleCreate reaction]:', error) } setClearEditor(false) setPosting(false) } return ( <>

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

0}>
    0}>
    !r.reply_to)}> {(reaction) => ( a?.slug === reaction.created_by.slug), )} comment={reaction} clickedReply={(id) => setClickedReplyId(id)} clickedReplyId={clickedReplyId()} lastSeen={dateFromLocalStorage} /> )}
{t('To write a comment, you must')}{' '} {t('sign up')} {' '} {t('or')}  {t('sign in')} } > handleSubmitComment(value)} setClear={clearEditor()} isPosting={posting()} /> ) }