]> Untitled Git - lemmy-ui.git/blob - src/shared/utils/app/insert-comment-into-tree.ts
fix submodule error
[lemmy-ui.git] / src / shared / utils / app / insert-comment-into-tree.ts
1 import { getCommentParentId, searchCommentTree } from "@utils/app";
2 import { CommentView } from "lemmy-js-client";
3 import { CommentNodeI } from "../../interfaces";
4
5 export default function insertCommentIntoTree(
6   tree: CommentNodeI[],
7   cv: CommentView,
8   parentComment: boolean,
9 ) {
10   // Building a fake node to be used for later
11   const node: CommentNodeI = {
12     comment_view: cv,
13     children: [],
14     depth: 0,
15   };
16
17   const parentId = getCommentParentId(cv.comment);
18   if (parentId) {
19     const parent_comment = searchCommentTree(tree, parentId);
20     if (parent_comment) {
21       node.depth = parent_comment.depth + 1;
22       parent_comment.children.unshift(node);
23     }
24   } else if (!parentComment) {
25     tree.unshift(node);
26   }
27 }