i forgot whatever i've been doing, i think i added an inbox

This commit is contained in:
Xyphyn committed 2023-07-04 18:45:15 -07:00
1 parent ef037349f0
commit 04704749b7
24 files changed
+307 -86

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
import { DEFAULT_INSTANCE_URL, authData } from '$lib/lemmy.js'
import { error, redirect } from '@sveltejs/kit'
import { get } from 'svelte/store'
export function load({ params, url }) {
if (Number(params.instance)) {
const split = url.pathname.split('/')
split.splice(2, 0, `${get(authData)?.instance ?? DEFAULT_INSTANCE_URL}`)
const newUrl = new URL(url)
newUrl.pathname = split.join('/')
throw redirect(300, newUrl.toString())
}
throw error(404, 'Not found')
}
@@ -0,0 +1,168 @@
<script lang="ts">
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import type { Comment, CommentView } from 'lemmy-js-client'
import type { CommentNodeI } from '$lib/components/lemmy/comment/comments.js'
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
import SvelteMarkdown from 'svelte-markdown'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
import { user } from '$lib/lemmy.js'
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
import PostVote from '$lib/components/lemmy/PostVote.svelte'
import { page } from '$app/stores'
import { onMount } from 'svelte'
export let data
const postData = data.post.post_view
const post = postData.post
function getCommentParentId(comment?: Comment): number | undefined {
const split = comment?.path.split('.')
// remove the 0
split?.shift()
return split && split.length > 1
? Number(split.at(split.length - 2))
: undefined
}
function getDepthFromComment(comment?: Comment): number | undefined {
const len = comment?.path.split('.').length
return len ? len - 2 : undefined
}
export function buildCommentsTree(
comments: CommentView[],
parentComment: boolean
): CommentNodeI[] {
const map = new Map<number, CommentNodeI>()
const depthOffset = !parentComment
? 0
: getDepthFromComment(comments[0].comment) ?? 0
for (const comment_view of comments) {
const depthI = getDepthFromComment(comment_view.comment) ?? 0
const depth = depthI ? depthI - depthOffset : 0
const node: CommentNodeI = {
comment_view,
children: [],
depth,
}
map.set(comment_view.comment.id, { ...node })
}
const tree: CommentNodeI[] = []
// if its a parent comment fetch, then push the first comment to the top node.
if (parentComment) {
const cNode = map.get(comments[0].comment.id)
if (cNode) {
tree.push(cNode)
}
}
for (const comment_view of comments) {
const child = map.get(comment_view.comment.id)
if (child) {
const parent_id = getCommentParentId(comment_view.comment)
if (parent_id) {
const parent = map.get(parent_id)
// Necessary because blocked comment might not exist
if (parent) {
parent.children.push(child)
}
} else {
if (!parentComment) {
tree.push(child)
}
}
}
}
return tree
}
</script>
<title>{data.post.post_view.post.name}</title>
<div class="flex flex-col gap-2">
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postData.community} />
<span class="opacity-50">{postData.creator.name}</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.url}
<a
href={post.url}
class="text-sky-400 max-w-[48ch] overflow-hidden
whitespace-nowrap text-ellipsis text-xs hover:underline"
>
{post.url}
</a>
{/if}
{#if isImage(post.url)}
<img
src={post.url}
alt={post.name}
class="rounded-md max-w-screen max-h-[80vh] mx-auto"
/>
{/if}
{#if post.body}
<p
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 p-2 text-sm rounded-md leading-5"
>
<SvelteMarkdown source={post.body} />
</p>
{/if}
<div class="w-max">
<PostVote
post={data.post.post_view.post}
score={data.post.post_view.counts.score}
vote={data.post.post_view.my_vote}
/>
</div>
</div>
<div class="mt-4 flex flex-col gap-2">
<div class="font-bold text-lg">
Comments <span class="text-sm font-normal ml-2 opacity-80">
{postData.counts.comments}
</span>
</div>
{#await data.streamed.comments}
<div class="flex flex-col gap-8">
{#each new Array(10) as comment}
<div class="flex flex-col gap-2">
<div
class="dark:bg-zinc-700 bg-slate-200 animate-pulse w-64 h-3 rounded-md"
/>
<div
class="dark:bg-zinc-700 bg-slate-200 animate-pulse w-128 h-4 rounded-md"
/>
</div>
{/each}
</div>
{:then comments}
{#if $user}
<CommentForm
postId={post.id}
on:comment={(comment) =>
(comments.comments = [...comment.detail, comments.comments])}
/>
{/if}
<MultiSelect
options={['hot', 'new', 'top']}
optionNames={['Hot', 'New', 'Top']}
selected="hot"
/>
<Comments
{post}
nodes={buildCommentsTree(comments.comments, false)}
isParent={true}
/>
{:catch}
<div class="bg-red-500/10 border border-red-500 rounded-md p-4">
Failed to load comments.
</div>
{/await}
</div>
+23
View File
@@ -0,0 +1,23 @@
import { authData, getClient } from '$lib/lemmy.js'
import { get } from 'svelte/store'
export async function load({ params }) {
return {
post: getClient(params.instance).getPost({
id: Number(params.id),
auth: get(authData)?.token,
}),
streamed: {
comments: getClient(params.instance).getComments({
post_id: Number(params.id),
type_: 'All',
limit: 250,
page: 1,
max_depth: 6,
saved_only: false,
sort: 'Hot',
auth: get(authData)?.token,
}),
},
}
}