Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241<script lang="ts"> import { page } from '$app/state' import type { HydratedThread, PostRef, ThreadWithReplies } from '#lib/domain/thread.js' import { thread_path } from '#lib/domain/thread.js' import { board_slug } from '#lib/domain/board.js' import type { HydratedReply } from '#lib/domain/post.js' import Post from '#lib/components/Post.svelte' import DeletedPost from '#lib/components/moderation/DeletedPost.svelte' import { setContext, onMount, onDestroy, untrack, type Snippet } from 'svelte' import { post_lookup_key, post_visibility_key, seen_tracker_key, create_post_visibility, create_seen_tracker, type PostLookup } from '#lib/domain/post-context.js' import { last_seen, mark_seen } from '#lib/ui/thread-seen.js' import { AtUri } from '@atproto/syntax' import { SvelteMap } from 'svelte/reactivity' import { HoverPreview } from '#lib/ui/hover-preview.svelte.js'
let { thread, can_reply = false, // when true, track how far the reader has scrolled and mark unseen replies // with a divider. off for the truncated board catalog. track_unseen = false, children }: { thread: ThreadWithReplies can_reply?: boolean track_unseen?: boolean children?: Snippet } = $props()
const uri = $derived(new AtUri(thread.uri)) const omitted = $derived(thread.reply_count - thread.replies.length)
const posts_by_rkey = $derived.by(() => { const map = new SvelteMap<string, PostRef>() const add = (ref: PostRef) => map.set(new AtUri(ref.post.uri).rkey, ref) // a local post is just a ref whose home thread is this one const local = (post: HydratedThread | HydratedReply): PostRef => ({ post, thread_uri: thread.uri }) add(local(thread)) for (const reply of thread.replies) add(local(reply)) // referenced posts not shown inline, so their previews still resolve for (const reply of thread.linked ?? []) add(local(reply)) // posts in other threads involved in a cross-thread ref/backlink; already // PostRefs carrying their own home thread, so they add directly for (const f of thread.foreign ?? []) add(f) return map }) // context reads the derived lazily so previews see the latest posts const lookup_post: PostLookup = (rkey) => posts_by_rkey.get(rkey) const visibility = create_post_visibility() setContext(post_lookup_key, lookup_post) setContext(post_visibility_key, visibility)
// one floating preview for the whole thread, fed by delegated events from // every post's refs and backlinks const preview = new HoverPreview(() => ({ thread, lookup_post, visibility }))
// The divider is client-only: last_seen reads localStorage, which the server // can't, so gate on mount to keep SSR and first hydration in agreement. let mounted = $state(false) onMount(() => { mounted = true })
// Readers can switch read tracking off entirely; with it off we neither read // nor write the seen marks, so no divider, no unread counts, and nothing // accumulates in localStorage. const tracking = $derived(track_unseen && page.data.read_markers_enabled)
// Frozen so the divider holds still while the reader scrolls: the tracker // advances last_seen underneath us, and a live re-read would drag the marker // mid-view. It re-reads only when `thread` itself changes — a reload or live // update — which is exactly when a new divider position is actually warranted. const marker_after = $derived( tracking && mounted && thread ? untrack(() => last_seen(thread.uri)) : 0 )
// The unread backlog hidden below the preview fold — what you'd open the thread // to see. Own posts, and the shown replies (already flagged by the divider), // don't count. const unread = $derived( marker_after ? (thread.omitted_times?.filter((t) => t > marker_after).length ?? 0) : 0 )
// A single last-seen timestamp is only a trustworthy boundary once the backlog // is cleared — otherwise it can't distinguish read from skipped omitted replies. // So gate both marking-seen and the divider on this. Always true on the full // thread page, which omits nothing. const contiguous = $derived(unread === 0)
// don't let a preview that merely scrolled past mark its omitted backlog seen. if (untrack(() => track_unseen)) { const tracker = create_seen_tracker((time) => { if (tracking && contiguous) mark_seen(thread.uri, time) }) setContext(seen_tracker_key, tracker) onDestroy(() => tracker.destroy()) }
// Replies are oldest-first, so unseen ones sit at the bottom; the divider goes // above the first reply newer than the snapshot. Your own posts never count as // unseen, so posting a reply doesn't drop a marker above your own message. -1 // means nothing is unseen. const my_did = $derived(page.data.user?.did) const first_unseen = $derived( contiguous && marker_after ? thread.replies.findIndex( (r) => Date.parse(r.createdAt) > marker_after && r.authorDid !== my_did ) : -1 )</script>
<!-- svelte-ignore a11y_no_static_element_interactions, a11y_click_events_have_key_events --><div class="thread" onmouseover={preview.on_show} onmousemove={preview.on_move} onmouseout={preview.on_leave} onfocusin={preview.on_show} onfocusout={preview.on_leave} onclick={preview.on_activate}> {#snippet reply_list()} {#if omitted > 0} <p class="omitted"> ({omitted} {omitted === 1 ? 'reply' : 'replies'} omitted{#if unread > 0}, <span class="unread">{unread} unread</span>{/if}. <a href={thread_path(board_slug(thread.effectiveBoard) ?? '', uri.rkey)} >View full thread</a >) </p> {/if} {#each thread.replies as reply, i (reply.uri)} {#if i === first_unseen} <hr class="unseen-marker" aria-label="New posts below" /> {/if} <Post post={reply} {thread} {can_reply} variant="reply"></Post> {/each} {@render children?.()} {/snippet} {#if thread.moved} <p class="moved"> Thread moved from /{thread.moved.from}/{#if thread.moved.reason}: {thread .moved.reason}{/if} </p> {/if} {#if thread.deletedAt} <DeletedPost uri={thread.uri}>{@render reply_list()}</DeletedPost> {:else} <Post post={thread} {thread} {can_reply} variant="thread"> {@render reply_list()} </Post> {/if}</div>{#if preview.current} <div class="post-preview" popover="manual" bind:this={preview.el} inert style={preview.pos ? `left: ${preview.pos.x}px; top: ${preview.pos.y}px;` : 'left: 0; top: 0; visibility: hidden;'} > <Post post={preview.current.post} thread={preview.current.thread_uri === thread.uri ? thread : ({ uri: preview.current.thread_uri, // foreign preview: no durable handle, permalink falls back to the did handle: null } as HydratedThread)} in_preview={true} variant={preview.current.post.uri === preview.current.thread_uri ? 'thread' : 'reply'} /> </div>{/if}
<style> .omitted { color: var(--color-muted); margin-bottom: 0; } .omitted .unread { color: var(--color-danger); } /* a moderator's note, so it carries the admin colour rather than the muted one */ .moved { color: var(--color-danger); margin: 0 0 0.25em; font-size: 0.9em; font-weight: bold; } .post-preview { position: fixed; inset: auto; margin: 0; padding: 0; color: inherit; background: var(--color-post-bg); border: 1px solid var(--color-border); border-radius: 3px; animation: post-preview-in 100ms ease-out; } @keyframes post-preview-in { from { opacity: 0; transform: scale(0.97); } to { opacity: 1; transform: scale(1); } } .unseen-marker { background: var(--color-danger); margin: 0.5em 0; }</style>