mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 10:10:30 +08:00
Set the dock badge in one place
The sidebar already follows the count, so the inbox and the poll don't need to set it too.
This commit is contained in:
@@ -10,9 +10,12 @@
|
||||
import { checkRadicleCLI } from "@app/lib/checkRadicleCLI.svelte";
|
||||
import { hints } from "@app/lib/hints";
|
||||
import { dynamicInterval } from "@app/lib/interval";
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import { modalStore, show } from "@app/lib/modal";
|
||||
import { notificationCount } from "@app/lib/notificationCount.svelte";
|
||||
import {
|
||||
badgeCount,
|
||||
notificationCount,
|
||||
refreshNotificationCount,
|
||||
} from "@app/lib/notificationCount.svelte";
|
||||
import * as router from "@app/lib/router";
|
||||
import type { SidebarData } from "@app/lib/router/definitions";
|
||||
import { ariaKeyShortcuts, useShortcuts } from "@app/lib/shortcuts.svelte";
|
||||
@@ -290,27 +293,14 @@
|
||||
firstLaunchStorage.value = false;
|
||||
}
|
||||
|
||||
await updateNotificationCount();
|
||||
dynamicInterval("notificationCount", updateNotificationCount, 3_000);
|
||||
await refreshNotificationCount();
|
||||
dynamicInterval("notificationCount", refreshNotificationCount, 3_000);
|
||||
});
|
||||
|
||||
async function updateNotificationCount() {
|
||||
notificationCount.value = await invoke<number>("notification_count");
|
||||
if (window.__TAURI_INTERNALS__ && $badgeCounter) {
|
||||
await getCurrentWindow().setBadgeCount(
|
||||
notificationCount.value === 0 ? undefined : notificationCount.value,
|
||||
);
|
||||
} else if (window.__TAURI_INTERNALS__) {
|
||||
await getCurrentWindow().setBadgeCount(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (window.__TAURI_INTERNALS__) {
|
||||
void getCurrentWindow().setBadgeCount(
|
||||
$badgeCounter && notificationCount.value > 0
|
||||
? notificationCount.value
|
||||
: undefined,
|
||||
badgeCount(notificationCount.value, $badgeCounter),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1 +1,14 @@
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
|
||||
export const notificationCount = $state({ value: 0 });
|
||||
|
||||
export async function refreshNotificationCount() {
|
||||
notificationCount.value = await invoke<number>("notification_count");
|
||||
}
|
||||
|
||||
export function badgeCount(
|
||||
count: number,
|
||||
enabled: boolean,
|
||||
): number | undefined {
|
||||
return enabled && count > 0 ? count : undefined;
|
||||
}
|
||||
|
||||
+7
-17
@@ -1,13 +1,13 @@
|
||||
<script lang="ts">
|
||||
import type { NotificationsByRepo } from "@bindings/cob/inbox/NotificationsByRepo";
|
||||
|
||||
import { getCurrentWindow } from "@tauri-apps/api/window";
|
||||
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import { notificationCount } from "@app/lib/notificationCount.svelte";
|
||||
import {
|
||||
notificationCount,
|
||||
refreshNotificationCount,
|
||||
} from "@app/lib/notificationCount.svelte";
|
||||
import type { SidebarData } from "@app/lib/router/definitions";
|
||||
|
||||
import { badgeCounter } from "@app/components/BadgeCounterSwitch.svelte";
|
||||
import InboxList from "@app/components/InboxList.svelte";
|
||||
import ScrollArea from "@app/components/ScrollArea.svelte";
|
||||
import Layout from "@app/views/repo/Layout.svelte";
|
||||
@@ -25,21 +25,11 @@
|
||||
notificationCount.value = sidebarData.notificationCount;
|
||||
});
|
||||
|
||||
async function updateCount() {
|
||||
const count = await invoke<number>("notification_count");
|
||||
notificationCount.value = count;
|
||||
if (window.__TAURI_INTERNALS__ && $badgeCounter) {
|
||||
await getCurrentWindow().setBadgeCount(count === 0 ? undefined : count);
|
||||
} else if (window.__TAURI_INTERNALS__) {
|
||||
await getCurrentWindow().setBadgeCount(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function clearAll() {
|
||||
try {
|
||||
await invoke("clear_notifications", { params: { type: "all" } });
|
||||
} finally {
|
||||
await updateCount();
|
||||
await refreshNotificationCount();
|
||||
await loadNotifications();
|
||||
}
|
||||
}
|
||||
@@ -50,7 +40,7 @@
|
||||
params: { type: "repo", content: rid },
|
||||
});
|
||||
} finally {
|
||||
await updateCount();
|
||||
await refreshNotificationCount();
|
||||
await loadNotifications();
|
||||
}
|
||||
}
|
||||
@@ -61,7 +51,7 @@
|
||||
params: { type: "ids", content: ids },
|
||||
});
|
||||
} finally {
|
||||
await updateCount();
|
||||
await refreshNotificationCount();
|
||||
await loadNotifications();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import { badgeCount } from "@app/lib/notificationCount.svelte";
|
||||
|
||||
test.each([
|
||||
[3, true, 3],
|
||||
[0, true, undefined],
|
||||
[3, false, undefined],
|
||||
])("%j notifications with the badge on %j show %j", (count, on, expected) => {
|
||||
expect(badgeCount(count, on)).toBe(expected);
|
||||
});
|
||||
Reference in New Issue
Block a user