Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import type { ReactionGroup } from "$lib/components/reaction/reactions";
export interface CommentView { uri: string; cid?: string; rkey: string; authorDid: string; authorHandle: string; createdAt: string; // the appview's comment header shows "Edited <time>" in place of the created one editedAt?: string; body: string; bodyHtml: string | null; reactions?: ReactionGroup[]; deleted?: boolean;}
export interface CommentThread { self: CommentView; replies: CommentView[];}
export interface ThreadInput { comment: CommentView; replyTo: string | null; // lets us reply to a parent even after it's deleted replyToCid?: string;}
const byCreatedAt = (a: CommentView, b: CommentView) => a.createdAt.localeCompare(b.createdAt);
// stand-in for a deleted parentexport function deletedComment(uri: string, createdAt: string, cid?: string): CommentView { return { uri, cid, rkey: uri.split("/").pop() ?? "", authorDid: "", authorHandle: "", createdAt, body: "", bodyHtml: null, deleted: true };}
export function buildCommentThreads(inputs: ThreadInput[]): CommentThread[] { const threads = new Map<string, CommentThread>();
for (const { comment, replyTo } of inputs) { if (replyTo === null) { threads.set(comment.uri, { self: comment, replies: [] }); } }
for (const { comment, replyTo, replyToCid } of inputs) { if (replyTo === null) continue; let parent = threads.get(replyTo); if (!parent) { parent = { self: deletedComment(replyTo, comment.createdAt), replies: [] }; threads.set(replyTo, parent); } if (parent.self.deleted && replyToCid && !parent.self.cid) { parent.self.cid = replyToCid; } parent.replies.push(comment); }
const list = [...threads.values()].sort((a, b) => byCreatedAt(a.self, b.self)); for (const thread of list) thread.replies.sort(byCreatedAt); return list;}