fix: comments not updating

This commit is contained in:
James Xylight committed 2025-02-05 21:24:55 -08:00
1 parent 714cb37a6e
commit 9970c8af6b
3 files changed
+141 -103

No files matched your search

@@ -53,6 +53,7 @@
import { fly } from 'svelte/transition'
import { expoOut } from 'svelte/easing'
import Option from 'mono-svelte/forms/select/Option.svelte'
import CommentProvider from './CommentProvider.svelte'
let { data: loaded = $bindable() } = $props()
let data = $state(loaded)
@@ -395,105 +396,12 @@
{/each}
</div>
{:then comments}
{#if $profile?.jwt}
{#if !commenting}
<EndPlaceholder class="">
<Button color="primary" onclick={() => (commenting = true)}>
<Icon src={PlusCircle} size="16" micro />
{$t('routes.post.addComment')}
</Button>
{#snippet action()}
<div class="gap-2 flex items-center">
<Select
size="md"
bind:value={commentSort}
onchange={reloadComments}
>
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top">
{$t('filter.sort.top.label')}
</Option>
<Option icon={Star} value="New">{$t('filter.sort.new')}</Option>
<Option icon={Clock} value="Old">
{$t('filter.sort.old')}
</Option>
<Option icon={ArrowTrendingDown} value="Controversial">
{$t('filter.sort.controversial')}
</Option>
</Select>
<Button size="square-md" onclick={reloadComments}>
{#snippet prefix()}
<Icon src={ArrowPath} size="16" mini />
{/snippet}
</Button>
</div>
{/snippet}
</EndPlaceholder>
{:else}
<CommentForm
postId={data.post.post_view.post.id}
oncomment={(comment) => {
Promise.resolve(data.comments).then((r) => {
r.comments.push(comment.comment_view)
})
// comments.comments.push(comment.comment_view)
// reloadComments()
// toast({ content: $t('routes.post.commented'), type: 'success' })
}}
locked={(data.post.post_view.post.locked &&
!(
$profile?.user?.local_user_view.local_user.admin ||
$profile?.user?.moderates
.map((c) => c.community.id)
.includes(data.post.community_view.community.id)
)) ||
page.params.instance.toLowerCase() != $instance.toLowerCase()}
banned={data.post.community_view.banned_from_community}
onfocus={() => (commenting = true)}
tools={commenting}
preview={commenting}
placeholder={commenting ? undefined : $t('routes.post.addComment')}
rows={commenting ? 7 : 1}
oncancel={() => (commenting = false)}
/>
{/if}
{/if}
{#if commenting || !$profile.jwt}
<div class="gap-2 flex items-center">
<Select size="md" bind:value={commentSort} onchange={reloadComments}>
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top">
{$t('filter.sort.top.label')}
</Option>
<Option icon={Star} value="New">{$t('filter.sort.new')}</Option>
<Option icon={Clock} value="Old">
{$t('filter.sort.old')}
</Option>
<Option icon={ArrowTrendingDown} value="Controversial">
{$t('filter.sort.controversial')}
</Option>
</Select>
<Button size="square-md" onclick={reloadComments}>
{#snippet prefix()}
<Icon src={ArrowPath} size="16" mini />
{/snippet}
</Button>
</div>
{/if}
<CommentListVirtualizer
post={data.post.post_view.post}
nodes={buildCommentsTree(
comments.comments,
undefined,
(c) =>
!(
(settings.hidePosts.deleted && c.comment.deleted) ||
(settings.hidePosts.removed && c.comment.removed)
),
)}
scrollTo={data.thread.focus}
<CommentProvider
post={data.post}
{comments}
focus={data.thread.focus}
onupdate={reloadComments}
bind:sort={commentSort}
/>
{#if comments.comments.length == 0}
<Placeholder
@@ -0,0 +1,132 @@
<script lang="ts">
import { page } from '$app/state'
import { profile } from '$lib/auth.svelte'
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
import CommentListVirtualizer from '$lib/components/lemmy/comment/CommentListVirtualizer.svelte'
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.svelte'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import { instance } from '$lib/instance'
import { settings } from '$lib/settings.svelte'
import { t } from '$lib/translations'
import type {
CommentSortType,
GetCommentsResponse,
GetPostResponse,
PostView,
} from 'lemmy-js-client'
import { Button, Option, Select } from 'mono-svelte'
import {
ArrowPath,
ArrowTrendingDown,
Clock,
Fire,
Icon,
PlusCircle,
Star,
Trophy,
} from 'svelte-hero-icons'
interface Props {
post: GetPostResponse
comments: GetCommentsResponse
sort?: CommentSortType
onupdate?: () => void
focus?: string
}
let {
post,
comments: passedComments,
sort = $bindable(),
onupdate,
focus,
}: Props = $props()
let comments = $state(passedComments)
let commenting = $state(false)
let tree = $derived(buildCommentsTree(comments.comments))
</script>
{#if $profile?.jwt}
{#if !commenting}
<EndPlaceholder class="">
<Button color="primary" onclick={() => (commenting = true)}>
<Icon src={PlusCircle} size="16" micro />
{$t('routes.post.addComment')}
</Button>
{#snippet action()}
<div class="gap-2 flex items-center">
<Select size="md" bind:value={sort} onchange={onupdate}>
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top">
{$t('filter.sort.top.label')}
</Option>
<Option icon={Star} value="New">{$t('filter.sort.new')}</Option>
<Option icon={Clock} value="Old">
{$t('filter.sort.old')}
</Option>
<Option icon={ArrowTrendingDown} value="Controversial">
{$t('filter.sort.controversial')}
</Option>
</Select>
<Button size="square-md" onclick={onupdate}>
{#snippet prefix()}
<Icon src={ArrowPath} size="16" mini />
{/snippet}
</Button>
</div>
{/snippet}
</EndPlaceholder>
{:else}
<CommentForm
postId={post.post_view.post.id}
oncomment={(comment) => {
comments.comments = [comment.comment_view, ...comments.comments]
}}
locked={(post.post_view.post.locked &&
!(
$profile?.user?.local_user_view.local_user.admin ||
$profile?.user?.moderates
.map((c) => c.community.id)
.includes(post.community_view.community.id)
)) ||
page.params.instance.toLowerCase() != $instance.toLowerCase()}
banned={post.community_view.banned_from_community}
onfocus={() => (commenting = true)}
tools={commenting}
preview={commenting}
placeholder={commenting ? undefined : $t('routes.post.addComment')}
rows={commenting ? 7 : 1}
oncancel={() => (commenting = false)}
/>
{/if}
{/if}
{#if commenting || !$profile.jwt}
<div class="gap-2 flex items-center">
<Select size="md" bind:value={sort} onchange={onupdate}>
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top">
{$t('filter.sort.top.label')}
</Option>
<Option icon={Star} value="New">{$t('filter.sort.new')}</Option>
<Option icon={Clock} value="Old">
{$t('filter.sort.old')}
</Option>
<Option icon={ArrowTrendingDown} value="Controversial">
{$t('filter.sort.controversial')}
</Option>
</Select>
<Button size="square-md" onclick={onupdate}>
{#snippet prefix()}
<Icon src={ArrowPath} size="16" mini />
{/snippet}
</Button>
</div>
{/if}
<CommentListVirtualizer
post={post.post_view.post}
nodes={tree}
scrollTo={focus}
/>