fix: unreactive post comments

This commit is contained in:
James Xylight committed 2025-02-06 16:31:41 -08:00
1 parent dd07caf5c4
commit 9dc575668c
3 files changed
+24 -21

No files matched your search

+21 -15
View File
@@ -1,8 +1,5 @@
<script lang="ts"> <script lang="ts">
import { preventDefault } from 'svelte/legacy'
import { import {
ArrowUp,
Bookmark, Bookmark,
Icon, Icon,
Microphone, Microphone,
@@ -10,8 +7,6 @@
Pencil, Pencil,
Plus, Plus,
Trash, Trash,
PlusCircle,
MinusCircle,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import type { CommentNodeI } from './comments.svelte' import type { CommentNodeI } from './comments.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte' import RelativeDate from '$lib/components/util/RelativeDate.svelte'
@@ -31,6 +26,7 @@
import { fly, slide } from 'svelte/transition' import { fly, slide } from 'svelte/transition'
import { expoInOut, expoOut } from 'svelte/easing' import { expoInOut, expoOut } from 'svelte/easing'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte' import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import type { ClassValue } from 'svelte/elements'
interface Props { interface Props {
node: CommentNodeI node: CommentNodeI
@@ -40,8 +36,8 @@
meta?: boolean meta?: boolean
open?: boolean open?: boolean
replying?: boolean replying?: boolean
contentClass?: string contentClass?: ClassValue
class?: string class?: ClassValue
metaSuffix?: import('svelte').Snippet metaSuffix?: import('svelte').Snippet
children?: import('svelte').Snippet children?: import('svelte').Snippet
} }
@@ -57,11 +53,16 @@
class: clazz = '', class: clazz = '',
metaSuffix, metaSuffix,
children, children,
open = $bindable(true),
}: Props = $props() }: Props = $props()
// TODO this kinda sucks because you cant bind:, but we need to save the open state for the virtualizer
$effect(() => {
node.expanded = open
})
let editing = $state(false) let editing = $state(false)
let newComment = $state(node.comment_view.comment.content) let newComment = $state(node.comment_view.comment.content)
let editingLoad = $state(false) let editingLoad = $state(false)
async function save() { async function save() {
@@ -80,8 +81,7 @@
editing = false editing = false
} catch (err) { } catch (err) {
toast({ toast({
// @ts-ignore i hate this content: err as any,
content: err,
type: 'error', type: 'error',
}) })
} }
@@ -106,7 +106,13 @@
{#snippet customTitle()} {#snippet customTitle()}
<span>{$t('form.edit')}</span> <span>{$t('form.edit')}</span>
{/snippet} {/snippet}
<form onsubmit={preventDefault(save)} class="contents"> <form
onsubmit={(e) => {
e.preventDefault()
save()
}}
class="contents"
>
<CommentForm <CommentForm
postId={node.comment_view.comment.id} postId={node.comment_view.comment.id}
value={newComment} value={newComment}
@@ -136,7 +142,7 @@
> >
{#if meta} {#if meta}
<button <button
onclick={() => (node.expanded = !node.expanded)} onclick={() => (open = !open)}
class="flex flex-row cursor-pointer gap-2 items-center group text-[13px] flex-wrap w-full class="flex flex-row cursor-pointer gap-2 items-center group text-[13px] flex-wrap w-full
z-0 group relative" z-0 group relative"
> >
@@ -154,10 +160,10 @@
></FormattedNumber> ></FormattedNumber>
{/if} {/if}
<Icon <Icon
src={node.expanded ? Minus : Plus} src={open ? Minus : Plus}
size="16" size="16"
micro micro
class="transition-transform duration-[400ms] ease-out {node.expanded class="transition-transform duration-[400ms] ease-out {open
? '' ? ''
: 'rotate-90'} text-primary-900 dark:text-primary-100" : 'rotate-90'} text-primary-900 dark:text-primary-100"
/> />
@@ -212,7 +218,7 @@
</span> </span>
</button> </button>
{/if} {/if}
{#if node.expanded} {#if open}
<div <div
class="relative {contentClass}" class="relative {contentClass}"
transition:slide={{ duration: 400, easing: expoOut }} transition:slide={{ duration: 400, easing: expoOut }}
@@ -116,10 +116,10 @@
postId={post.id} postId={post.id}
node={nodes[index]} node={nodes[index]}
op={post.creator_id == node.comment_view.creator.id} op={post.creator_id == node.comment_view.creator.id}
contentClass="pl-2.5 {node.children.length > 0 || contentClass="{node.children.length > 0 ||
node.comment_view.counts.child_count > 0 node.comment_view.counts.child_count > 0
? 'border-l' ? 'border-l'
: ''} ml-2.5 border-slate-200 dark:border-zinc-800" : ''} ml-2.5 border-slate-200 dark:border-zinc-800 pl-2.5 "
> >
{#if node.children?.length > 0} {#if node.children?.length > 0}
<Comments {post} nodes={nodes[index].children} isParent={false} /> <Comments {post} nodes={nodes[index].children} isParent={false} />
@@ -44,10 +44,7 @@
let comments = $state(passedComments) let comments = $state(passedComments)
let commenting = $state(false) let commenting = $state(false)
let tree = $state(buildCommentsTree(comments.comments)) let tree = $derived(buildCommentsTree(comments.comments))
$effect(() => {
tree = buildCommentsTree(comments.comments)
})
</script> </script>
{#if $profile?.jwt} {#if $profile?.jwt}