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

+120
View File
@@ -0,0 +1,120 @@
<script lang="ts">
import type {
CommentView,
GetPostResponse,
PostView,
Comment,
} from 'lemmy-js-client'
import {
ArrowDown,
ArrowUp,
ChatBubbleLeft,
ChatBubbleOvalLeft,
ChatBubbleOvalLeftEllipsis,
ChevronDown,
ChevronUp,
Icon,
} from 'svelte-hero-icons'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
import { authData, getClient } from '$lib/lemmy.js'
import PostVote from '$lib/components/lemmy/PostVote.svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
let postRes: PostView
export { postRes as post }
let vote = postRes.my_vote
async function upvote() {
if (!$authData) return
const upvoted = postRes.my_vote == 1
await getClient()
.likePost({
score: upvoted ? 0 : 1,
auth: $authData.token,
post_id: postRes.post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
const upvoted = postRes.my_vote == -1
await getClient()
.likePost({
score: upvoted ? 0 : -1,
auth: $authData.token,
post_id: postRes.post.id,
})
.catch((_) => undefined)
}
</script>
<div
class="bg-white dark:bg-zinc-900 border dark:border-zinc-800 border-slate-200
rounded-md flex flex-col overflow-hidden max-w-xl w-full"
>
<div class="flex flex-col gap-2 bg-white dark:bg-zinc-900 p-4 rounded-md">
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postRes.community} />
<div class="ml-auto" />
<span class="opacity-50">{postRes.creator.name}</span>
<span class="opacity-50">
<RelativeDate date={new Date(postRes.post.published)} />
</span>
</span>
<a href="/post/{postRes.post.id}" class="font-bold text-lg">
{postRes.post.name}
</a>
{#if postRes.post.url}
<a
href={postRes.post.url}
class="text-sky-400 max-w-[24ch] overflow-hidden
whitespace-nowrap text-ellipsis text-xs hover:underline"
>
{postRes.post.url}
</a>
{/if}
{#if isImage(postRes.post.url)}
<a href="/post/{postRes.post.id}" class="inline self-start">
<img
src={postRes.post.url}
alt={postRes.post.name}
class="rounded-md max-h-64 max-w-full"
loading="lazy"
/>
</a>
{/if}
{#if postRes.post.body}
<p class="text-sm max-h-24 line-clamp-3">
{postRes.post.body}
</p>
{/if}
</div>
<div class="flex flex-row gap-2 pb-4 px-4">
<PostVote
post={postRes.post}
vote={postRes.my_vote}
score={postRes.counts.score}
/>
<a
class="flex flex-row items-center gap-1 p-1.5 px-3 rounded-md
bg-slate-100 dark:bg-zinc-900 hover:bg-slate-200
dark:hover:bg-zinc-700 transition-colors border border-slate-200
dark:border-zinc-800"
href="/post/{postRes.post.id}"
>
<Icon src={ChatBubbleOvalLeft} mini width={16} height={16} />
<span class="text-sm">
<FormattedNumber number={postRes.counts.comments} />
</span>
</a>
</div>
</div>
+75
View File
@@ -0,0 +1,75 @@
<script lang="ts">
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { authData, getClient } from '$lib/lemmy'
import type { Post } from 'lemmy-js-client'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
export let post: Post
export let vote: number | undefined
export let score: number
async function upvote() {
if (!$authData) return
const upvoted = vote == 1
await getClient()
.likePost({
score: upvoted ? 0 : 1,
auth: $authData.token,
post_id: post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
const upvoted = vote == -1
await getClient()
.likePost({
score: upvoted ? 0 : -1,
auth: $authData.token,
post_id: post.id,
})
.catch((_) => undefined)
}
</script>
<div
class="flex flex-row items-center gap-1 rounded-md transition-colors
cursor-pointer border border-slate-200 dark:border-zinc-800
{// upvote
vote == 1
? 'bg-orange-600/20 text-orange-500'
: // downvote
vote == -1
? 'bg-blue-600/40 text-blue-400'
: // no vote
'bg-slate-100 dark:bg-zinc-900 hover:bg-slate-200 dark:hover:bg-zinc-700'}"
>
<button
aria-label="Upvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == 1 ? 0 : 1
upvote()
}}
>
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">
<FormattedNumber number={score} />
</span>
<button
aria-label="Downvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == -1 ? 0 : -1
downvote()
}}
>
<Icon src={ChevronDown} mini width={20} height={20} />
</button>
</div>
@@ -0,0 +1,158 @@
<script lang="ts">
import {
ArrowDown,
ArrowUp,
ChatBubbleOvalLeft,
Icon,
Minus,
Plus,
} from 'svelte-hero-icons'
import Avatar from '$lib/components/ui/Avatar.svelte'
import type { CommentNodeI } from './comments'
import SvelteMarkdown from 'svelte-markdown'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { getClient, authData } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import CommentForm from './CommentForm.svelte'
import { page } from '$app/stores'
export let node: CommentNodeI
export let postId: number
export let open = true
export let replying = false
async function upvote() {
if (!$authData) return
const upvoted = node.comment_view.my_vote == 1
node.comment_view.my_vote = Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : 1,
auth: $authData.token,
comment_id: node.comment_view.comment.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
const upvoted = node.comment_view.my_vote == -1
node.comment_view.my_vote = -Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : -1,
auth: $authData.token,
comment_id: node.comment_view.comment.id,
})
.catch((_) => undefined)
}
</script>
<li
class="py-3 {$page.url.hash == `#${node.comment_view.comment.id}`
? 'bg-slate-300 dark:bg-zinc-800'
: ''}"
id="#{node.comment_view.comment.id.toString()}"
>
<details bind:open class="flex flex-col gap-1">
<summary
class="flex flex-row cursor-pointer arrow gap-2 items-center group"
>
<Avatar
url={node.comment_view.creator.avatar ??
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
width={24}
/>
<span class="text-sm">{node.comment_view.creator.name}</span>
<span class="text-sm opacity-60 md:inline hidden">
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
1
? ''
: 's'}
</span>
<span class="text-sm opacity-60">
<RelativeDate date={new Date(node.comment_view.comment.published)} />
</span>
<span
class="ml-auto translate-x-1 opacity-0
group-hover:translate-x-0 group-hover:opacity-100 text-xs transition-all
flex flex-row items-center gap-1 hover:dark:bg-white/10 p-1 rounded-md
hover:bg-black/10 px-2"
>
{#if open}
<div class="inline">
<Icon src={Minus} width={16} height={16} mini />
</div>
Collapse
{:else}
<Icon src={Plus} width={16} height={16} mini />
Expand
{#if node.children.length > 0}
<span class="text-xs opacity-50">+{node.children.length}</span>
{/if}
{/if}
</span>
</summary>
<div class="mt-1">
<div
class="flex flex-col gap-1 whitespace-pre-wrap
max-w-full"
>
<div class="max-w-full break-words">
<SvelteMarkdown source={node.comment_view.comment.content} />
</div>
<div class="flex flex-row gap-2 items-center">
<div class="flex flex-row gap-2 items-center">
<button
on:click={upvote}
class={node.comment_view.my_vote == 1 ? 'text-orange-500' : ''}
>
<Icon src={ArrowUp} width={16} mini />
</button>
<span class="text-sm font-medium">
{node.comment_view.counts.score}
</span>
<button
on:click={downvote}
class={node.comment_view.my_vote == -1 ? 'text-blue-500' : ''}
>
<Icon src={ArrowDown} width={16} mini />
</button>
</div>
<button
class="flex flex-row gap-1 items-center transition-colors px-2 py-1
rounded-md {Color.secondary}"
on:click={() => (replying = !replying)}
>
<Icon src={ChatBubbleOvalLeft} width={16} height={16} mini />
<span class="text-xs">Reply</span>
</button>
</div>
</div>
</div>
{#if replying}
<div class="max-w-sm my-2">
<h1 class="font-bold text-sm mb-2">Reply</h1>
<CommentForm
{postId}
parentId={node.comment_view.comment.id}
on:comment={(e) => {
node.children = [e.detail, ...node.children]
replying = false
}}
/>
</div>
{/if}
<div class="bg-transparent dark:bg-transparent">
<slot />
</div>
</details>
</li>
@@ -0,0 +1,64 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import { Color } from '$lib/ui/colors.js'
import type { Comment, Post } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { createEventDispatcher } from 'svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
export let postId: number
export let parentId: number | undefined = undefined
const dispatch = createEventDispatcher()
let newComment = ''
let loading = false
async function submit() {
if (!$user || !$authData || newComment == '') return
loading = true
try {
const response = await getClient().createComment({
auth: $authData.token,
content: newComment,
post_id: postId,
parent_id: parentId,
})
dispatch('comment', response)
newComment = ''
addToast(
'Success',
'Your comment was added. You may need to refresh to see it.',
ToastType.success
)
} catch (err) {
addToast('Error', 'Failed to comment.', ToastType.error)
}
loading = false
}
</script>
<div class="max-w-lg flex flex-col gap-2">
<textarea
rows="3"
class="w-full bg-slate-100 dark:bg-zinc-900 border border-slate-200
dark:border-zinc-800 p-4 outline-none focus:border-black
focus:dark:border-white transition-colors rounded-md"
bind:value={newComment}
/>
<div class="w-24 ml-auto flex flex-row gap-2">
<Button
large
on:click={submit}
color={Color.accent}
{loading}
disabled={loading}
>
Submit
</Button>
</div>
</div>
@@ -0,0 +1,35 @@
<script lang="ts">
import Comment from './Comment.svelte'
import type { CommentNodeI } from './comments'
import type { Post } from 'lemmy-js-client'
import { page } from '$app/stores'
import { onMount } from 'svelte'
const maxComments = 250
export let nodes: CommentNodeI[]
export let isParent: boolean
export let post: Post
onMount(() => {
if (isParent && $page.url.hash) {
document.getElementById($page.url.hash)?.scrollIntoView({
behavior: 'smooth',
})
}
})
</script>
<ul
class={isParent
? ''
: 'ml-2.5 mt-2 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
>
{#each nodes.slice(0, maxComments) as node}
<Comment postId={post.id} {node} open={true}>
{#if node.children?.length > 0}
<svelte:self {post} nodes={node.children} isParent={false} />
{/if}
</Comment>
{/each}
</ul>
@@ -0,0 +1,7 @@
import type { CommentView } from 'lemmy-js-client'
export interface CommentNodeI {
comment_view: CommentView
children: Array<CommentNodeI>
depth: number
}
+1 -1
View File
@@ -6,7 +6,7 @@
<div
class="bg-white border border-slate-200
dark:border-zinc-800 dark:bg-zinc-900 break-words
flex-col gap-4 p-4 max-w-lg rounded-md {clazz}"
flex-col gap-4 p-4 rounded-md {clazz}"
>
<slot />
</div>
+4
View File
@@ -16,6 +16,7 @@
ArrowRightOnRectangle,
Cog6Tooth,
Icon,
Inbox,
} from 'svelte-hero-icons'
</script>
@@ -64,6 +65,9 @@
<Icon src={ArrowLeftOnRectangle} mini width={16} /> Log in
</MenuButton>
{:else}
<MenuButton link href="/inbox">
<Icon src={Inbox} mini width={16} /> Inbox
</MenuButton>
<MenuButton
on:click={() => {
localStorage.removeItem('user')
+1 -1
View File
@@ -48,7 +48,7 @@
{top ? 'bottom-[100%] origin-bottom-left' : 'top-[100%]'}
rounded-md py-2 min-w-[16rem] my-2 flex flex-col shadow-lg {itemsClass
? itemsClass
: 'bg-white dark:bg-zinc-900'}"
: 'bg-white dark:bg-zinc-900 border border-slate-200 dark:border-zinc-800'}"
>
{#if open}
<slot />
+1 -1
View File
@@ -6,7 +6,7 @@ import {
import { get, writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
export const DEFAULT_INSTANCE_URL = 'lemmy.world'
export const DEFAULT_INSTANCE_URL = 'lemm.ee'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export function getClient(instance?: string): LemmyHttp {
+22
View File
@@ -0,0 +1,22 @@
import type {
CommentReplyView,
PersonMentionView,
PrivateMessageView,
} from 'lemmy-js-client'
export type InboxItemView =
| PersonMentionView
| CommentReplyView
| PrivateMessageView
export function getInboxItemPublished(item: InboxItemView): string {
if ('comment_reply' in item) {
return item.comment_reply.published
}
if ('private_message' in item) {
return item.private_message.published
}
return item.person_mention.published
}