-
Notifications
You must be signed in to change notification settings - Fork 331
/
build-comments-tree.ts
58 lines (52 loc) · 1.71 KB
/
build-comments-tree.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { getCommentParentId, getDepthFromComment } from "@utils/app";
import { CommentView } from "lemmy-js-client";
import { CommentNodeI } from "../../interfaces";
export default function buildCommentsTree(
comments: CommentView[],
parentComment: boolean,
): CommentNodeI[] {
const map = new Map<number, CommentNodeI>();
const depthOffset = !parentComment
? 0
: getDepthFromComment(comments[0].comment) ?? 0;
for (const comment_view of comments) {
const depthI = getDepthFromComment(comment_view.comment) ?? 0;
const depth = depthI ? depthI - depthOffset : 0;
const node: CommentNodeI = {
comment_view,
children: [],
depth,
};
map.set(comment_view.comment.id, { ...node });
}
const tree: CommentNodeI[] = [];
// if its a parent comment fetch, then push the first comment to the top node.
if (parentComment) {
const cNode = map.get(comments[0].comment.id);
if (cNode) {
tree.push(cNode);
}
}
// This should not be sorted on the front end, in order to preserve the
// back end sorts. However, the parent ids must be sorted, so make sure
// When adding new comments to trees, that they're inserted right after
// their parent index. This is done in post.tsx
for (const comment_view of comments) {
const child = map.get(comment_view.comment.id);
if (child) {
const parent_id = getCommentParentId(comment_view.comment);
if (parent_id) {
const parent = map.get(parent_id);
// Necessary because blocked comment might not exist
if (parent) {
parent.children.push(child);
}
} else {
if (!parentComment) {
tree.push(child);
}
}
}
}
return tree;
}