Something went wrong. Try again.
A Bluesky TUI Client. (Alpha)
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import type { Post } from '../core/models/post.js';import type { Thread, ThreadNode } from '../core/models/thread.js';
// A flattened, depth-annotated row used for rendering and selection in the// thread view: the focused post (depth 0) followed by its replies in// depth-first order.export interface ThreadRow { post: Post; depth: number;}
function walk(nodes: ThreadNode[], depth: number, acc: ThreadRow[]): void { for (const node of nodes) { acc.push({ post: node.post, depth }); walk(node.replies, depth + 1, acc); }}
export function flattenThreadReplies(thread: Thread): ThreadRow[] { const rows: ThreadRow[] = [{ post: thread.focus, depth: 0 }]; walk(thread.replies, 1, rows); return rows;}
/** Like flattenThreadReplies but prepends ancestors so the full chain is scrollable. */export function flattenThread(thread: Thread): ThreadRow[] { const rows: ThreadRow[] = thread.ancestors.map((post) => ({ post, depth: 0 })); rows.push({ post: thread.focus, depth: 0 }); walk(thread.replies, 1, rows); return rows;}
// Apply `fn` to every post in the thread (ancestors, focus, and the reply tree)// whose URI matches. Used to propagate optimistic interaction updates.export function mapThreadPosts(thread: Thread, uri: string, fn: (post: Post) => Post): Thread { const apply = (post: Post): Post => (post.uri === uri ? fn(post) : post); const mapNode = (node: ThreadNode): ThreadNode => ({ post: apply(node.post), replies: node.replies.map(mapNode), }); return { ancestors: thread.ancestors.map(apply), focus: apply(thread.focus), replies: thread.replies.map(mapNode), };}