Files
cinny/src/app/hooks/useTauriNotificationBadge.ts
T

26 lines
945 B
TypeScript
Raw Normal View History

import { useEffect } from 'react';
import { useAtomValue } from 'jotai';
import { roomToUnreadAtom } from '../state/room/roomToUnread';
// Tauri v2 injects __TAURI_INTERNALS__ into the webview at runtime.
// We use it directly so cinny doesn't need @tauri-apps/api as a dependency.
type TauriInternals = { invoke: (cmd: string, args?: Record<string, unknown>) => Promise<unknown> };
const tauriInvoke = (): TauriInternals['invoke'] | undefined =>
(window as unknown as { __TAURI_INTERNALS__?: TauriInternals }).__TAURI_INTERNALS__?.invoke;
export function useTauriNotificationBadge() {
const roomToUnread = useAtomValue(roomToUnreadAtom);
useEffect(() => {
const invoke = tauriInvoke();
if (!invoke) return;
let totalHighlights = 0;
roomToUnread.forEach((unread) => {
totalHighlights += unread.highlight;
});
invoke('set_badge_count', { count: totalHighlights }).catch(() => {});
}, [roomToUnread]);
}