feat: Add notification for unreads

This commit is contained in:
Xyphyn committed 2023-07-06 11:11:25 -07:00
1 parent 28d9820b1e
commit 1a0881f9ce
13 files changed
+215 -105

No files matched your search

@@ -43,8 +43,15 @@
<FormattedNumber number={community_view.counts.comments} />
</span>
</div>
<h1 class="font-bold text-lg">
{community_view.community.name}
</h1>
<div>
<h1 class="font-bold text-lg">
{community_view.community.title}
</h1>
<span class="opacity-80 text-sm">
!{community_view.community.name}@{new URL(
community_view.community.actor_id
).hostname}
</span>
</div>
<Markdown source={community_view.community.description} />
</StickyCard>
+16 -9
View File
@@ -25,6 +25,7 @@
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { Color } from '$lib/ui/colors.js'
import { page } from '$app/stores'
import Avatar from '$lib/components/ui/Avatar.svelte'
let postRes: PostView
export { postRes as post }
@@ -49,16 +50,22 @@
<Card class="bg-white flex flex-col overflow-hidden w-full relative">
<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-xs items-center">
<CommunityLink avatarSize={20} avatar community={postRes.community} />
<span class="opacity-60">
<UserLink user={postRes.creator} />
</span>
<Avatar url={postRes.community.icon ?? ''} alt="" width={24} />
<div class="flex flex-col">
<CommunityLink community={postRes.community} />
<span class="opacity-60 flex flex-row gap-1">
<UserLink user={postRes.creator} />
<span>•</span>
<RelativeDate date={new Date(postRes.post.published)} />
</span>
</div>
<div class="ml-auto" />
<span class="opacity-50 capitalize">
<RelativeDate date={new Date(postRes.post.published)} />
</span>
<span class="opacity-50 capitalize" />
</span>
<a href="/post/{postRes.post.id}" class="font-bold">
<a
href="/post/{postRes.post.id}"
class="font-bold {postRes.read ? 'opacity-50' : ''}"
>
{postRes.post.name}
</a>
{#if postRes.post.url}
@@ -115,7 +122,7 @@
<FormattedNumber number={postRes.counts.comments} />
</span>
</a>
<Menu top absolute class="bottom-0 right-0 m-5 z-40">
<Menu top absolute class="bottom-0 right-0 m-5 z-10">
<Button slot="button">
<Icon src={EllipsisHorizontal} width={16} mini />
</Button>
@@ -120,14 +120,16 @@
class="flex flex-col gap-1 whitespace-pre-wrap
max-w-full"
>
<div class="max-w-full break-words">
<div class="max-w-full break-words text-sm leading-6">
<Markdown 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">
<div class="flex flex-row items-center">
<button
on:click={upvote}
class={node.comment_view.my_vote == 1 ? 'text-orange-500' : ''}
class="pr-1.5 {node.comment_view.my_vote == 1
? 'text-orange-500'
: ''}"
>
<Icon src={ArrowUp} width={16} mini />
</button>
@@ -136,7 +138,9 @@
</span>
<button
on:click={downvote}
class={node.comment_view.my_vote == -1 ? 'text-blue-500' : ''}
class="pl-1.5 {node.comment_view.my_vote == -1
? 'text-blue-500'
: ''}"
>
<Icon src={ArrowDown} width={16} mini />
</button>
@@ -1,5 +1,6 @@
<script lang="ts">
import MarkdownHeader from '$lib/components/markdown/MarkdownHeader.svelte'
import MarkdownHr from '$lib/components/markdown/MarkdownHr.svelte'
import MarkdownLink from '$lib/components/markdown/MarkdownLink.svelte'
import SvelteMarkdown from 'svelte-markdown'
@@ -11,5 +12,6 @@
renderers={{
link: MarkdownLink,
heading: MarkdownHeader,
hr: MarkdownHr,
}}
/>
@@ -0,0 +1 @@
<hr class="w-[90%] mx-auto my-4 opacity-80" />
+1 -1
View File
@@ -4,7 +4,7 @@
</script>
<div
class="bg-white shadow-lg border-slate-200
class="bg-white shadow-md border border-slate-100
dark:border-zinc-800 dark:bg-zinc-900 break-words
flex-col p-1 rounded-md {clazz}"
>
+91 -75
View File
@@ -26,84 +26,100 @@
</script>
<nav
class="flex top-0 z-20 flex-row gap-4 items-center mt-2 w-full h-16
max-w-5xl mx-auto px-6"
class="sticky bg-slate-50/70 dark:bg-zinc-950/70 backdrop-blur-3xl top-0
z-20 mt-2"
>
<div class="flex flex-row gap-2 items-center mr-auto">
<a href="/" class="flex flex-row items-center gap-2">
<Logo width={40} />
<div class="sm:flex flex-col hidden">
<span class="font-bold">Xylemmy</span>
<span
class="text-xs opacity-50
<div
class="flex z-20 flex-row gap-4 items-center w-full h-16 max-w-5xl mx-auto px-6"
>
<div class="flex flex-row gap-2 items-center mr-auto">
<a href="/" class="flex flex-row items-center gap-2">
<Logo width={40} />
<div class="sm:flex flex-col hidden">
<span class="font-bold">Xylemmy</span>
<span
class="text-xs opacity-50
transition-opacity"
>
{#if $authData?.instance}
{$authData.instance}
{:else}
{DEFAULT_INSTANCE_URL}
{/if}
</span>
</div>
</a>
</div>
<div class="flex flex-row gap-2">
<Link href="/communities">
<Icon src={GlobeAlt} width={16} />
<span class="hidden sm:inline ml-1">Communities</span>
</Link>
>
{#if $authData?.instance}
{$authData.instance}
{:else}
{DEFAULT_INSTANCE_URL}
{/if}
</span>
</div>
</a>
</div>
<div class="flex flex-row gap-2">
<Link href="/communities">
<Icon src={GlobeAlt} width={16} />
<span class="hidden sm:inline ml-1">Communities</span>
</Link>
<Menu>
<Button color={Color.accent} slot="button">
<Icon src={Plus} width={16} />
<span class="hidden sm:inline">Create</span>
</Button>
<span class="text-xs opacity-80 text-left mx-4 my-2">Create</span>
<MenuButton link href="/create/post">
<Icon src={PencilSquare} mini width={16} />
Post
</MenuButton>
</Menu>
</div>
<Menu>
<Button color={Color.accent} slot="button">
<Icon src={Plus} width={16} />
<span class="hidden sm:inline">Create</span>
</Button>
<span class="text-xs opacity-80 text-left mx-4 my-2">Create</span>
<MenuButton link href="/create/post">
<Icon src={PencilSquare} mini width={16} />
Post
</MenuButton>
<button
class="w-8 h-8 rounded-full ring-1 ring-slate-300 bg-slate-100
dark:bg-zinc-800 relative"
aria-label="Profile"
slot="button"
>
{#if $user}
<img
src={$user.person_view.person.avatar ??
'https://xylo.xylight.dev/img/logo-background.svg'}
alt=""
width={32}
height={32}
/>
{#if $user.unreads > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0"
/>
{/if}
{/if}
</button>
<span class="text-xs opacity-80 text-left mx-4 my-2">Profile</span>
{#if !$user}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} /> Log in
</MenuButton>
{:else}
<MenuButton link href="/u/{$user.person_view.person.name}">
<Icon src={UserCircle} mini width={16} /> Profile
</MenuButton>
<MenuButton link href="/inbox">
<div class="relative">
{#if $user.unreads > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/>
{/if}<Icon src={Inbox} mini width={16} />
</div>
Inbox
</MenuButton>
<MenuButton
on:click={() => {
localStorage.removeItem('user')
goto('/', {
invalidateAll: true,
})
}}
color={Color.dangerSecondary}
>
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton>
{/if}
</Menu>
</div>
<Menu>
<button
class="w-8 h-8 rounded-full ring-1 ring-slate-300 bg-slate-100
dark:bg-zinc-800"
aria-label="Profile"
slot="button"
>
{#if $user}
<img
src={$user.person_view.person.avatar ??
'https://xylo.xylight.dev/img/logo-background.svg'}
alt=""
width={32}
height={32}
/>
{/if}
</button>
<span class="text-xs opacity-80 text-left mx-4 my-2">Profile</span>
{#if !$user}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} /> Log in
</MenuButton>
{:else}
<MenuButton link href="/u/{$user.person_view.person.name}">
<Icon src={UserCircle} mini width={16} /> Profile
</MenuButton>
<MenuButton link href="/inbox">
<Icon src={Inbox} mini width={16} /> Inbox
</MenuButton>
<MenuButton
on:click={() => {
localStorage.removeItem('user')
goto('/', {
invalidateAll: true,
})
}}
color={Color.dangerSecondary}
>
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton>
{/if}
</Menu>
</nav>
+2 -2
View File
@@ -1,7 +1,7 @@
<div
class="sticky top-4 bg-white shadow-lg border-slate-200
class="sticky top-20 bg-white shadow-md border-slate-200
dark:border-zinc-800 dark:bg-zinc-900 break-words hidden lg:flex
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-2rem)] overflow-auto"
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-6rem)] overflow-auto"
>
<slot />
</div>
+20 -2
View File
@@ -31,8 +31,26 @@ export interface AuthData {
instance: string
}
export interface UserData {
unreads: number
}
export const authData = writable<AuthData | undefined>()
export const user = writable<GetPersonDetailsResponse>()
export const user = writable<GetPersonDetailsResponse & UserData>()
setInterval(async () => {
// check for unread messages
if (!get(authData)) return
const response = await getClient().getUnreadCount({
auth: get(authData)!.token,
})
const u = get(user)
u.unreads = response.mentions + response.private_messages + response.replies
user.set(u)
}, 60 * 1000)
if (typeof localStorage != 'undefined') {
if (localStorage.getItem('user')) {
@@ -52,7 +70,7 @@ authData.subscribe(async (data) => {
username: `${data?.username}@${data?.instance}`,
})
user.set(person)
user.set({ ...person, unreads: 0 })
if (typeof localStorage != 'undefined') {
localStorage.setItem('user', JSON.stringify(data))
+3 -1
View File
@@ -92,6 +92,8 @@
'My cat just invented a time machine, what do I do?',
'Unpopular opinion: world peace and global happiness would be beneficial to humanity',
'LPT: The smaller weights are easier to lift than the bigger weights in a gym',
'javascript bad',
'ELI5: What is 4 + 8?',
]
const placeholder =
@@ -123,7 +125,7 @@
<title>Create post</title>
</svelte:head>
<Card class="p-6 max-w-xl mx-auto">
<Card class="p-6 max-w-2xl mx-auto">
<form on:submit|preventDefault={submit} class="flex flex-col gap-4">
<h1 class="font-bold text-xl">Create Post</h1>
<div>
+42 -3
View File
@@ -1,14 +1,53 @@
<script lang="ts">
import type { CommentReplyView, PersonMentionView } from 'lemmy-js-client'
import InboxItem from './InboxItem.svelte'
import Button from '$lib/components/input/Button.svelte'
import { Check, Icon } from 'svelte-hero-icons'
import { authData, getClient } from '$lib/lemmy.js'
import { goto } from '$app/navigation'
import { page } from '$app/stores'
export let data
export let data: {
data: PersonMentionView[] | CommentReplyView[]
}
function isRead(item: PersonMentionView | CommentReplyView) {
console.log('running...')
if ('person_mention' in item) {
return (item as PersonMentionView).person_mention.read
}
if ('comment_reply' in item) {
return (item as CommentReplyView).comment_reply.read
}
return false
}
async function markAllAsRead() {
const response = await getClient().markAllAsRead({
auth: $authData!.token,
})
goto($page.url, {
invalidateAll: true,
})
return response.replies
}
</script>
<svelte:head>
<title>Inbox</title>
</svelte:head>
<h1 class="font-bold text-3xl">Inbox</h1>
<div class="flex flex-row justify-between">
<h1 class="font-bold text-3xl">Inbox</h1>
<Button on:click={markAllAsRead}>
<Icon src={Check} width={16} mini />
Mark all as read
</Button>
</div>
<div class="flex flex-col gap-4 list-none my-4">
{#if !data.data || (data.data?.length ?? 0) == 0}
<p class="text-center opacity-60 text-lg mx-4">
@@ -16,7 +55,7 @@
</p>
{:else}
{#each data.data as item}
<InboxItem {item} />
<InboxItem {item} read={isRead(item)} />
{/each}
{/if}
</div>
+8 -2
View File
@@ -2,15 +2,21 @@
import Link from '$lib/components/input/Link.svelte'
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Card from '$lib/components/ui/Card.svelte'
import type { CommentReplyView, PersonMentionView } from 'lemmy-js-client'
import type {
CommentReplyView,
PersonMentionView,
PrivateMessageView,
} from 'lemmy-js-client'
export let item: CommentReplyView | PersonMentionView
export let read: boolean
</script>
<Card class="flex flex-col bg-white rounded-md p-4 max-w-full">
<div class="flex flex-row items-center">
<div
class="text-sm max-w-[80ch] whitespace-nowrap text-ellipsis overflow-hidden"
class="text-sm max-w-[80ch] whitespace-nowrap text-ellipsis
overflow-hidden {read ? 'opacity-50' : ''}"
>
<span class="font-bold">
{item.creator.display_name ?? item.creator.name}
+11 -3
View File
@@ -3,13 +3,11 @@
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, isVideo } from '$lib/ui/image.js'
import { user } from '$lib/lemmy.js'
import { authData, getClient, 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'
import UserLink from '$lib/components/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
@@ -19,6 +17,16 @@
const postData = data.post.post_view
const post = postData.post
onMount(async () => {
if (!postData.read && $authData) {
getClient().markPostAsRead({
auth: $authData.token,
read: true,
post_id: post.id,
})
}
})
function getCommentParentId(comment?: Comment): number | undefined {
const split = comment?.path.split('.')
// remove the 0