From 0fb17f626f41f8f48b259499d6b556f5b766c1f7 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Wed, 19 Jul 2023 16:09:24 -0700 Subject: [PATCH] fix: Make child loading indicator only appear on parent --- .../components/lemmy/comment/Comments.svelte | 49 +++++++++++-------- src/lib/components/lemmy/comment/comments.ts | 1 + src/lib/lemmy.ts | 2 +- src/routes/post/[instance]/[id]/+page.svelte | 2 +- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/lib/components/lemmy/comment/Comments.svelte b/src/lib/components/lemmy/comment/Comments.svelte index 36a845be..39edb46c 100644 --- a/src/lib/components/lemmy/comment/Comments.svelte +++ b/src/lib/components/lemmy/comment/Comments.svelte @@ -8,6 +8,7 @@ import { authData, getClient } from '$lib/lemmy.js' import type { Post } from 'lemmy-js-client' import { fly } from 'svelte/transition' + import { toast, ToastType } from '$lib/components/ui/toasts/toasts.js' export let nodes: CommentNodeI[] export let isParent: boolean @@ -33,26 +34,31 @@ ) return - loadingChildren = true + try { + parent.loading = true - const newComments = await getClient().getComments({ - auth: $authData?.token, - max_depth: 5, - parent_id: parent.comment_view.comment.id, - }) + const newComments = await getClient().getComments({ + auth: $authData?.token, + max_depth: 5, + parent_id: parent.comment_view.comment.id, + }) - if (newComments.comments.length == 0) { - loadingChildren = false - return + if (newComments.comments.length == 0) { + loadingChildren = false + return + } + + // this code sucks, but for some reason it works + // lemmy api is quite goofy + parent.children = buildCommentsTree(newComments.comments, true).find( + (c) => c.comment_view.comment.id == parent.comment_view.comment.id + )!.children + } catch (error) { + toast({ + content: 'Failed to fetch comments', + type: ToastType.error, + }) } - - // this code sucks, but for some reason it works - // lemmy api is quite goofy - parent.children = buildCommentsTree(newComments.comments, true).find( - (c) => c.comment_view.comment.id == parent.comment_view.comment.id - )!.children - - loadingChildren = false } @@ -77,9 +83,12 @@ class="my-2 min-w-[7rem] w-max h-8 border-l-2 border-slate-200 dark:border-zinc-900 ml-2.5 pl-0.5" >