diff --git a/src/lib/components/lemmy/comment/Comments.svelte b/src/lib/components/lemmy/comment/Comments.svelte index 8f7df976..3ecfd3d8 100644 --- a/src/lib/components/lemmy/comment/Comments.svelte +++ b/src/lib/components/lemmy/comment/Comments.svelte @@ -9,8 +9,6 @@ import type { CommentView, Post } from 'lemmy-js-client' import { fly } from 'svelte/transition' import { toast, ToastType } from '$lib/components/ui/toasts/toasts.js' - import Modal from '$lib/components/ui/modal/Modal.svelte' - import TextArea from '$lib/components/input/TextArea.svelte' export let nodes: CommentNodeI[] export let isParent: boolean @@ -55,7 +53,7 @@ return } - const tree = buildCommentsTree(newComments.comments, false) + const tree = buildCommentsTree(newComments.comments, true, 1) // 0.18.2 -> 0.18.3 broke this // so i'm adding this check diff --git a/src/lib/components/lemmy/comment/comments.ts b/src/lib/components/lemmy/comment/comments.ts index 59303643..a3f64275 100644 --- a/src/lib/components/lemmy/comment/comments.ts +++ b/src/lib/components/lemmy/comment/comments.ts @@ -24,7 +24,8 @@ function getDepthFromComment(comment?: Comment): number | undefined { export function buildCommentsTree( comments: CommentView[], - parentComment: boolean + parentComment: boolean, + baseDepth: number = 0 ): CommentNodeI[] { const map = new Map() const depthOffset = !parentComment @@ -37,7 +38,7 @@ export function buildCommentsTree( const node: CommentNodeI = { comment_view, children: [], - depth, + depth: depth + baseDepth, } map.set(comment_view.comment.id, { ...node }) } @@ -73,6 +74,50 @@ export function buildCommentsTree( return tree } +export function searchCommentTree( + tree: CommentNodeI[], + id: number +): CommentNodeI | undefined { + for (const node of tree) { + if (node.comment_view.comment.id === id) { + return node + } + + for (const child of node.children) { + const res = searchCommentTree([child], id) + + if (res) { + return res + } + } + } + return undefined +} + +export function insertCommentIntoTree( + tree: CommentNodeI[], + cv: CommentView, + parentComment: boolean +) { + // Building a fake node to be used for later + const node: CommentNodeI = { + comment_view: cv, + children: [], + depth: 0, + } + + const parentId = getCommentParentId(cv.comment) + if (parentId) { + const parent_comment = searchCommentTree(tree, parentId) + if (parent_comment) { + node.depth = parent_comment.depth + 1 + parent_comment.children.unshift(node) + } + } else if (!parentComment) { + tree.unshift(node) + } +} + export async function buildCommentsTreeAsync( comments: CommentView[], parentComment: boolean