Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2x 9x 5x 5x 15x 15x 5x 9x 9x 9x 5x 5x 4x 9x 5x | import type { ArticleComments } from "../../shared/types/ArticleComments";
import type { CommentThread } from "../../shared/types/CommentThread";
import type { WordpressComment } from "../adapters/wordpress";
import { toReaderComment } from "./readerComment";
/** Threads oldest first, each reply under its top-level comment however deep WordPress nested it. */
export const toArticleComments = (open: boolean, comments: WordpressComment[]): ArticleComments => {
const byId = new Map(comments.map((comment) => [comment.id, comment]));
const threads = new Map<number, CommentThread>();
const rootOf = (comment: WordpressComment): WordpressComment => {
const parent = byId.get(comment.parent);
return parent ? rootOf(parent) : comment;
};
for (const comment of comments) {
const root = rootOf(comment);
const thread = threads.get(root.id);
if (!thread) {
threads.set(comment.id, { ...toReaderComment(comment), replies: [] });
continue;
}
const replyTo = comment.parent === root.id ? undefined : byId.get(comment.parent);
thread.replies.push(toReaderComment(comment, replyTo?.author_name.trim()));
}
return { open, count: comments.length, threads: [...threads.values()] };
};
|