fix: make dedicated $notifications store

This commit is contained in:
Xylight committed 2024-07-06 11:33:31 -07:00
1 parent 19d4617dbf
commit 53bf2ebf78
10 files changed
+52 -51

No files matched your search

+16 -17
View File
@@ -58,12 +58,12 @@ interface ProfileData {
defaultInstance?: string defaultInstance?: string
} }
interface PersonData extends MyUserInfo { interface PersonData extends MyUserInfo {}
notifications: {
inbox: number interface Notifications {
reports: number inbox: number
applications: number reports: number
} applications: number
} }
const getCookie = (key: string): string | undefined => { const getCookie = (key: string): string | undefined => {
@@ -99,7 +99,7 @@ export let profile = derived<Writable<ProfileData>, Profile>(
pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile()
if (profile?.jwt) { if (profile?.jwt) {
if (profile.user) { if (!profile.user) {
site.set(undefined) site.set(undefined)
userFromJwt(profile.jwt, profile.instance).then((res) => { userFromJwt(profile.jwt, profile.instance).then((res) => {
@@ -127,6 +127,12 @@ export let profile = derived<Writable<ProfileData>, Profile>(
} }
) )
export let notifications = writable<Notifications>({
applications: 0,
inbox: 0,
reports: 0,
})
profileData.subscribe(async (pd) => { profileData.subscribe(async (pd) => {
const serialized: ProfileData = { const serialized: ProfileData = {
...pd, ...pd,
@@ -254,14 +260,7 @@ async function userFromJwt(
if (!myUser) return undefined if (!myUser) return undefined
return { return {
user: { user: myUser,
notifications: {
applications: 0,
inbox: 0,
reports: 0,
},
...myUser,
},
site: site, site: site,
} }
} }
@@ -373,11 +372,11 @@ async function checkInbox() {
isAdmin(user) isAdmin(user)
) )
user.notifications = { notifications.set({
inbox: notifs.unreads, inbox: notifs.unreads,
applications: notifs.applications, applications: notifs.applications,
reports: notifs.reports, reports: notifs.reports,
} })
} }
setInterval(checkInbox, 4 * 60 * 1000) setInterval(checkInbox, 4 * 60 * 1000)
+3 -3
View File
@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { profile, profileData } from '$lib/auth.js' import { notifications, profile, profileData } from '$lib/auth.js'
import Link from '$lib/components/input/Link.svelte' import Link from '$lib/components/input/Link.svelte'
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte' import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
import { import {
@@ -112,7 +112,7 @@
class="relative" class="relative"
isSelectedFilter={(path) => path.startsWith('/admin')} isSelectedFilter={(path) => path.startsWith('/admin')}
> >
{#if ($profile?.user?.notifications.applications ?? 0) > 0} {#if ($notifications.applications ?? 0) > 0}
<div <div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1" class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/> />
@@ -125,7 +125,7 @@
label={$t('nav.moderation')} label={$t('nav.moderation')}
class="relative" class="relative"
> >
{#if ($profile?.user?.notifications.reports ?? 0) > 0} {#if ($notifications.reports ?? 0) > 0}
<div <div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1" class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/> />
+4 -4
View File
@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth' import { notifications, profile } from '$lib/auth'
import { import {
Badge, Badge,
@@ -73,7 +73,7 @@
alt={$profile.user.local_user_view.person.name} alt={$profile.user.local_user_view.person.name}
/> />
</div> </div>
{#if $profile.user.notifications.inbox > 0} {#if $notifications.inbox > 0}
<div <div
class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0 z-10" class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0 z-10"
/> />
@@ -104,9 +104,9 @@
<MenuButton link href="/inbox"> <MenuButton link href="/inbox">
<Icon src={Inbox} mini width={16} slot="prefix" /> <Icon src={Inbox} mini width={16} slot="prefix" />
{$t('profile.inbox')} {$t('profile.inbox')}
{#if $profile.user.notifications.inbox > 0} {#if $notifications.inbox > 0}
<Badge color="red-subtle" class="text-xs ml-auto font-bold !py-0.5"> <Badge color="red-subtle" class="text-xs ml-auto font-bold !py-0.5">
{$profile.user.notifications.inbox} {$notifications.inbox}
</Badge> </Badge>
{/if} {/if}
</MenuButton> </MenuButton>
+3 -3
View File
@@ -14,7 +14,7 @@
UserCircle, UserCircle,
UserGroup, UserGroup,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { profile, profileData } from '$lib/auth.js' import { notifications, profile, profileData } from '$lib/auth.js'
import { userSettings } from '$lib/settings.js' import { userSettings } from '$lib/settings.js'
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte' import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
import CommunityList from '$lib/components/ui/sidebar/CommunityList.svelte' import CommunityList from '$lib/components/ui/sidebar/CommunityList.svelte'
@@ -70,12 +70,12 @@
<SidebarButton icon={Inbox} href="/inbox"> <SidebarButton icon={Inbox} href="/inbox">
<span slot="label" class="flex items-center gap-2"> <span slot="label" class="flex items-center gap-2">
{$t('profile.inbox')} {$t('profile.inbox')}
{#if $profile?.user?.notifications.inbox} {#if $notifications.inbox}
<Badge <Badge
class="w-5 h-5 !p-0 grid place-items-center ml-auto" class="w-5 h-5 !p-0 grid place-items-center ml-auto"
color="red-subtle" color="red-subtle"
> >
{$profile?.user?.notifications.inbox} {$notifications.inbox}
</Badge> </Badge>
{/if} {/if}
</span> </span>
+3 -3
View File
@@ -1,16 +1,16 @@
<script> <script>
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { page } from '$app/stores' import { page } from '$app/stores'
import { profile } from '$lib/auth' import { notifications, profile } from '$lib/auth'
import MultiSelect from '$lib/components/input/Switch.svelte' import MultiSelect from '$lib/components/input/Switch.svelte'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import { Badge } from 'mono-svelte' import { Badge } from 'mono-svelte'
</script> </script>
{#if ($profile?.user?.notifications.applications ?? 0) > 0} {#if $notifications.applications > 0}
<span class="flex flex-row text-red-500 gap-2"> <span class="flex flex-row text-red-500 gap-2">
<Badge color="red-subtle" class="w-max"> <Badge color="red-subtle" class="w-max">
{$profile?.user?.notifications.applications} {$notifications.applications}
</Badge> unread applications </Badge> unread applications
</span> </span>
{/if} {/if}
@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth.js' import { notifications, profile } from '$lib/auth.js'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte' import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import SectionTitle from '$lib/components/ui/SectionTitle.svelte' import SectionTitle from '$lib/components/ui/SectionTitle.svelte'
import { Material, toast } from 'mono-svelte' import { Material, toast } from 'mono-svelte'
@@ -60,7 +60,7 @@
application.admin = $profile.user!.local_user_view.person application.admin = $profile.user!.local_user_view.person
application.registration_application.deny_reason = denyReason application.registration_application.deny_reason = denyReason
$profile.user!.notifications.applications -= 1 $notifications.applications -= 1
} catch (err) { } catch (err) {
toast({ toast({
content: err as any, content: err as any,
+2 -2
View File
@@ -13,7 +13,7 @@
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { page } from '$app/stores' import { page } from '$app/stores'
import Pageination from '$lib/components/ui/Pageination.svelte' import Pageination from '$lib/components/ui/Pageination.svelte'
import { profile } from '$lib/auth.js' import { notifications, profile } from '$lib/auth.js'
import Placeholder from '$lib/components/ui/Placeholder.svelte' import Placeholder from '$lib/components/ui/Placeholder.svelte'
import { fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import { searchParam } from '$lib/util.js' import { searchParam } from '$lib/util.js'
@@ -36,7 +36,7 @@
const response = await getClient().markAllAsRead() const response = await getClient().markAllAsRead()
$profile.user.notifications.inbox = 0 $notifications.inbox = 0
goto($page.url, { goto($page.url, {
invalidateAll: true, invalidateAll: true,
+2 -2
View File
@@ -2,7 +2,7 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte' import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte' import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import { profile } from '$lib/auth.js' import { notifications, profile } from '$lib/auth.js'
import { import {
ArrowUturnLeft, ArrowUturnLeft,
ArrowUturnUp, ArrowUturnUp,
@@ -58,7 +58,7 @@
} }
item.read = isRead item.read = isRead
if ($profile.user) $profile.user.notifications.inbox += isRead ? -1 : 1 if ($profile.user) $notifications.inbox += isRead ? -1 : 1
loading = false loading = false
} }
+2 -2
View File
@@ -10,7 +10,7 @@
PrivateMessageReportView, PrivateMessageReportView,
} from 'lemmy-js-client' } from 'lemmy-js-client'
import { Check, Icon } from 'svelte-hero-icons' import { Check, Icon } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js' import { notifications, profile } from '$lib/auth.js'
import { Button } from 'mono-svelte' import { Button } from 'mono-svelte'
import type { ReportView } from '$lib/lemmy/report.js' import type { ReportView } from '$lib/lemmy/report.js'
import PrivateMessage from '$lib/components/lemmy/inbox/PrivateMessage.svelte' import PrivateMessage from '$lib/components/lemmy/inbox/PrivateMessage.svelte'
@@ -77,7 +77,7 @@
} }
} }
$profile.user.notifications.reports += item.resolved ? 1 : -1 $notifications.reports += item.resolved ? 1 : -1
} catch (err) { } catch (err) {
toast({ toast({
content: err as any, content: err as any,
@@ -47,17 +47,19 @@
let post = data.post let post = data.post
// onMount(async () => { onMount(async () => {
// if ( if (
// !(post.post_view.read && $userSettings.markPostsAsRead) && !(post.post_view.read && $userSettings.markPostsAsRead) &&
// $profile?.jwt $profile?.jwt
// ) { ) {
// getClient().markPostAsRead({ getClient().markPostAsRead({
// read: $userSettings.markPostsAsRead, read: $userSettings.markPostsAsRead,
// post_id: post.post_view.post.id, post_ids: [post.post_view.post.id],
// }) // @ts-ignore
// } post_id: post.post_view.post.id,
// }) })
}
})
afterNavigate(async () => { afterNavigate(async () => {
// reactivity hack // reactivity hack
@@ -315,7 +317,7 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
{/each} {/each}
</div> </div>
{:then comments} {:then comments}
{#if $profile?.user} {#if $profile?.jwt}
<CommentForm <CommentForm
postId={post.post_view.post.id} postId={post.post_view.post.id}
on:comment={(comment) => on:comment={(comment) =>
@@ -325,7 +327,7 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
])} ])}
locked={(post.post_view.post.locked && locked={(post.post_view.post.locked &&
!( !(
$profile?.user.local_user_view.local_user.admin || $profile?.user?.local_user_view.local_user.admin ||
$profile?.user?.moderates $profile?.user?.moderates
.map((c) => c.community.id) .map((c) => c.community.id)
.includes(data.post.community_view.community.id) .includes(data.post.community_view.community.id)