Files
photon-tbw/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte
T

161 lines
4.6 KiB
Svelte

<script lang="ts">
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 Comments from '$lib/components/lemmy/comment/Comments.svelte'
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.svelte'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import { t } from '$lib/i18n/translations'
import { settings } from '$lib/settings.svelte'
import type {
CommentSortType,
GetCommentsResponse,
GetPostResponse,
} 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
virtualize?: boolean
}
let {
post,
comments: passedComments,
sort = $bindable(),
onupdate,
focus,
virtualize = true,
}: Props = $props()
let commenting = $state(false)
let comments = $derived(passedComments)
let tree = $state(buildCommentsTree(comments.comments))
$effect(() => {
tree = buildCommentsTree(comments.comments)
})
</script>
{#if profile.data?.jwt}
{#if !commenting}
<EndPlaceholder>
<Button
color="primary"
rounding="xl"
disabled={(post.post_view.post.locked ||
post.post_view.banned_from_community) &&
!(
profile.data?.user?.local_user_view.local_user.admin ||
profile.data?.user?.moderates
.map(c => c.community.id)
.includes(post.community_view.community.id)
)}
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={settings.defaultSort.comments}
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="custom"
class="h-8.5 w-8.5"
rounding="xl"
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.push(comment.comment_view)
}}
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.data.jwt}
<div class="gap-2 flex items-center">
<Select
size="md"
bind:value={settings.defaultSort.comments}
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="custom" class="h-8.5 w-8.5" rounding="xl" onclick={onupdate}>
{#snippet prefix()}
<Icon src={ArrowPath} size="16" mini />
{/snippet}
</Button>
</div>
{/if}
{#if virtualize}
<CommentListVirtualizer
post={post.post_view.post}
nodes={tree}
scrollTo={focus}
/>
{:else}
<div class="divide-y divide-slate-200 dark:divide-zinc-800">
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
<Comments isParent={true} nodes={tree} post={post.post_view.post} />
</div>
</div>
{/if}