Something went wrong. Try again.
Coves frontend - a photon fork
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300<script lang="ts"> import { browser } from '$app/environment' import { page } from '$app/state' import { goto } from '$app/navigation' import { mapSort } from '$lib/api/coves/sort' import { errorMessage } from '$lib/app/util/error' import { log } from '$lib/app/util/log' import type { AtUri, PostView, StrongRef, ThreadViewComment, } from '$lib/api/coves/types' import { profile } from '$lib/app/state/auth.svelte' import { t } from '$lib/app/state/i18n' import { settings } from '$lib/app/state/settings.svelte' import CommentForm from '$lib/feature/comment/CommentForm.svelte' import CommentListVirtualizer from '$lib/feature/comment/CommentListVirtualizer.svelte' import { buildCommentsTree, findTopLevelIndexByRkey, insertCommentIntoTree, retainCreatedComments, commentCreatedContext, type CommentCreated, } from '$lib/feature/comment/comments.svelte' import CommentTree from '$lib/feature/comment/CommentTree.svelte' import { postLink } from '$lib/feature/post' import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte' import { Button, Option, Select, toast } from '$lib/ui/kit' import { onMount, setContext, untrack } from 'svelte' import { SvelteSet } from 'svelte/reactivity' import { EMPTY_POST_STATS } from '$lib/feature/vote/subjects' import { Icon, CirclePlus, Flame, MessageSquare, RefreshCw, Star, Trophy, } from '$lib/ui/kit/icon' interface Props { post: PostView comments: ThreadViewComment[] sort?: string onupdate?: () => void focus?: string virtualize?: boolean showContext?: boolean singleThread?: boolean }
let { post = $bindable(), comments, sort = $bindable(), onupdate, focus, virtualize = true, showContext, singleThread, }: Props = $props() let commenting = $state(false) const activeSort = $derived( mapSort( sort ?? page.url.searchParams.get('sort') ?? settings.defaultSort.comments, ).sort, ) let selectedSort = $derived(activeSort)
async function changeSort(): Promise<void> { const nextSort = selectedSort const url = new URL(page.url) url.searchParams.set('sort', nextSort) url.searchParams.delete('cursor') // SvelteKit's shallow replaceState keeps page.url at the loaded URL, so a // consumed post-loading hint can still be present here after the address // bar has been cleaned. url.searchParams.delete('uri') try { // Cursors belong to a sort order. Navigation keeps the URL, loader cache, // and Back/Forward history on the same page as the displayed comments. await goto(url, { noScroll: true, keepFocus: true, state: page.state, }) settings.defaultSort.comments = nextSort } catch (err) { selectedSort = activeSort log.error('[comments] Failed to change sort', err) toast({ content: errorMessage(err), type: 'error' }) } }
const postRef: StrongRef = $derived({ uri: post.uri, cid: post.cid })
// `tree` has to stay $state, not $derived: insertCommentIntoTree() mutates it // in place for optimistic replies and CommentTree takes it via bind:nodes. // The effect rebuilds it whenever the loaded comments change; the untracked // seed is what the server-rendered first paint shows, before effects run. let tree = $state(untrack(() => buildCommentsTree(comments))) const createdComments = new SvelteSet<AtUri>() let treePostUri = untrack(() => post.uri) const commentCreated: CommentCreated = (comment, parent) => { if (comment.post.uri !== post.uri || createdComments.has(comment.uri)) return createdComments.add(comment.uri) if (parent) tree = retainCreatedComments(tree, [parent], createdComments) post.stats = { ...(post.stats ?? EMPTY_POST_STATS), commentCount: (post.stats?.commentCount ?? 0) + 1, } } setContext(commentCreatedContext, commentCreated) // The seed above already built the tree from these comments. Rebuilding it // on the effect's first run gave every node a new identity, so the whole // thread re-rendered straight after it had rendered. let treeComments = untrack(() => comments) $effect(() => { const loaded = comments const postUri = post.uri untrack(() => { if (loaded === treeComments && postUri === treePostUri) return treeComments = loaded if (treePostUri !== postUri) { createdComments.clear() treePostUri = postUri tree = buildCommentsTree(loaded) } else if (createdComments.size === 0) { // Nothing local to carry over: the retain pass would search the new // tree for every old node and keep none of them. tree = buildCommentsTree(loaded) } else { tree = retainCreatedComments( buildCommentsTree(loaded), tree, createdComments, ) } }) })
let virtualizer = $state<CommentListVirtualizer>()
/** * Points the comment list at the top-level row containing the comment with * the given rkey, mounting it when the list is virtualized. Returns * 'missing' when the rkey isn't anywhere in the loaded tree, 'pending' * while the virtualizer hasn't rendered yet (callers should retry), and * 'scrolled' once the row is mounted (or the list isn't virtualized, so * every row already is). */ export function scrollToComment( rkey: string, ): 'scrolled' | 'missing' | 'pending' { const index = findTopLevelIndexByRkey(tree, rkey) if (index === -1) return 'missing' if (!virtualize) return 'scrolled' return virtualizer?.scrollToRow(index) ? 'scrolled' : 'pending' }
onMount(() => { if (browser && !isNaN(Number(page.url.hash.slice(1)) || NaN)) // hack because virtual list needs to calc heights setTimeout(() => { document .getElementById(page.url.hash.slice(1)) ?.scrollIntoView({ behavior: 'instant', block: 'center' }) }, 100) })</script>
<!-- An expired session keeps the editor mounted: the draft lives in the form's local state, and the recovery prompt lets the reader log back in without leaving this page. Only a genuine guest loses the composer.-->{#if profile.sessionExpired || profile.current?.jwt} {#if !commenting} <EndPlaceholder border={false}> <Button color="primary" rounding="xl" onclick={() => (commenting = true)}> <Icon src={MessageSquare} size="16" /> {$t('routes.post.addComment')} </Button>
{#snippet action()} <div class="gap-2 flex items-center"> <Select size="md" bind:value={selectedSort} onchange={changeSort}> <Option icon={Flame} value="hot">{$t('filter.sort.hot')}</Option> <Option icon={Trophy} value="top"> {$t('filter.sort.top.label')} </Option> <Option icon={Star} value="new">{$t('filter.sort.new')}</Option> </Select> <Button size="custom" class="h-8.5 w-8.5" rounding="xl" onclick={onupdate} icon={RefreshCw} ></Button> </div> {/snippet} </EndPlaceholder> {:else} <CommentForm {postRef} oncomment={(cv) => { if (cv.post.uri !== post.uri) return insertCommentIntoTree(tree, cv, false) commentCreated(cv) }} onfocus={() => (commenting = true)} tools={commenting} preview={commenting} placeholder={commenting ? undefined : $t('routes.post.addComment')} rows={commenting ? 7 : 1} oncancel={() => (commenting = false)} /> {/if}{/if}
{#if commenting || !(profile.sessionExpired || profile.current.jwt)} <div class="gap-2 flex items-center"> <Select size="md" bind:value={selectedSort} onchange={changeSort}> <Option icon={Flame} value="hot">{$t('filter.sort.hot')}</Option> <Option icon={Trophy} value="top"> {$t('filter.sort.top.label')} </Option> <Option icon={Star} value="new">{$t('filter.sort.new')}</Option> </Select> <Button size="custom" class="h-8.5 w-8.5" rounding="xl" onclick={onupdate} icon={RefreshCw} ></Button> </div>{/if}
{#snippet allCommentsPlaceholder()} <EndPlaceholder alignment="center"> {#snippet action()} <Button href={postLink(post)} icon={CirclePlus} rounding="pill"> {$t('routes.post.thread.allComments')} </Button> {/snippet} </EndPlaceholder>{/snippet}
{#if singleThread && !showContext} {@render allCommentsPlaceholder()}{/if}
{#if showContext && tree[0]} <Button color="secondary" alignment="left" rounding="pill" href={postLink(post)} class="mt-2 -mb-2 -mx-2.5 w-max" > <Icon src={CirclePlus} size="16" /> {$t('routes.post.thread.allComments')} </Button> <div class="border-l h-4 -mb-5 ml-2.5 border-slate-200 dark:border-zinc-800" ></div>{/if}{#if virtualize} <CommentListVirtualizer bind:this={virtualizer} {post} {postRef} postAuthorDid={post.author.did} nodes={tree} scrollTo={focus} />{:else} <div class="divide-y divide-slate-200 dark:divide-zinc-800"> <div class="-mx-3 sm:-mx-6 px-3 sm:px-6"> <CommentTree bind:nodes={tree} {post} {postRef} postAuthorDid={post.author.did} /> </div> </div>{/if}
{#if singleThread && !showContext} {@render allCommentsPlaceholder()}{/if}