fix: make dedicated $notifications store
This commit is contained in:
1 parent
19d4617dbf
commit
53bf2ebf78
10 files changed
+52
-51
No files matched your search
+16
-17
@@ -58,12 +58,12 @@ interface ProfileData {
|
||||
defaultInstance?: string
|
||||
}
|
||||
|
||||
interface PersonData extends MyUserInfo {
|
||||
notifications: {
|
||||
inbox: number
|
||||
reports: number
|
||||
applications: number
|
||||
}
|
||||
interface PersonData extends MyUserInfo {}
|
||||
|
||||
interface Notifications {
|
||||
inbox: number
|
||||
reports: number
|
||||
applications: number
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
if (profile?.jwt) {
|
||||
if (profile.user) {
|
||||
if (!profile.user) {
|
||||
site.set(undefined)
|
||||
|
||||
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) => {
|
||||
const serialized: ProfileData = {
|
||||
...pd,
|
||||
@@ -254,14 +260,7 @@ async function userFromJwt(
|
||||
if (!myUser) return undefined
|
||||
|
||||
return {
|
||||
user: {
|
||||
notifications: {
|
||||
applications: 0,
|
||||
inbox: 0,
|
||||
reports: 0,
|
||||
},
|
||||
...myUser,
|
||||
},
|
||||
user: myUser,
|
||||
site: site,
|
||||
}
|
||||
}
|
||||
@@ -373,11 +372,11 @@ async function checkInbox() {
|
||||
isAdmin(user)
|
||||
)
|
||||
|
||||
user.notifications = {
|
||||
notifications.set({
|
||||
inbox: notifs.unreads,
|
||||
applications: notifs.applications,
|
||||
reports: notifs.reports,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
setInterval(checkInbox, 4 * 60 * 1000)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<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 ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
|
||||
import {
|
||||
@@ -112,7 +112,7 @@
|
||||
class="relative"
|
||||
isSelectedFilter={(path) => path.startsWith('/admin')}
|
||||
>
|
||||
{#if ($profile?.user?.notifications.applications ?? 0) > 0}
|
||||
{#if ($notifications.applications ?? 0) > 0}
|
||||
<div
|
||||
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
|
||||
/>
|
||||
@@ -125,7 +125,7 @@
|
||||
label={$t('nav.moderation')}
|
||||
class="relative"
|
||||
>
|
||||
{#if ($profile?.user?.notifications.reports ?? 0) > 0}
|
||||
{#if ($notifications.reports ?? 0) > 0}
|
||||
<div
|
||||
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
|
||||
/>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/auth'
|
||||
import { notifications, profile } from '$lib/auth'
|
||||
|
||||
import {
|
||||
Badge,
|
||||
@@ -73,7 +73,7 @@
|
||||
alt={$profile.user.local_user_view.person.name}
|
||||
/>
|
||||
</div>
|
||||
{#if $profile.user.notifications.inbox > 0}
|
||||
{#if $notifications.inbox > 0}
|
||||
<div
|
||||
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">
|
||||
<Icon src={Inbox} mini width={16} slot="prefix" />
|
||||
{$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">
|
||||
{$profile.user.notifications.inbox}
|
||||
{$notifications.inbox}
|
||||
</Badge>
|
||||
{/if}
|
||||
</MenuButton>
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
UserCircle,
|
||||
UserGroup,
|
||||
} 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 SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||
import CommunityList from '$lib/components/ui/sidebar/CommunityList.svelte'
|
||||
@@ -70,12 +70,12 @@
|
||||
<SidebarButton icon={Inbox} href="/inbox">
|
||||
<span slot="label" class="flex items-center gap-2">
|
||||
{$t('profile.inbox')}
|
||||
{#if $profile?.user?.notifications.inbox}
|
||||
{#if $notifications.inbox}
|
||||
<Badge
|
||||
class="w-5 h-5 !p-0 grid place-items-center ml-auto"
|
||||
color="red-subtle"
|
||||
>
|
||||
{$profile?.user?.notifications.inbox}
|
||||
{$notifications.inbox}
|
||||
</Badge>
|
||||
{/if}
|
||||
</span>
|
||||
|
||||
Reference in new issue
Block a user