2026-06-13 23:53:39 -04:00
|
|
|
import { useEffect, useRef } from 'react';
|
2026-06-10 17:32:38 -04:00
|
|
|
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);
|
2026-06-13 23:53:39 -04:00
|
|
|
const prevHighlightsRef = useRef(0);
|
2026-06-10 17:32:38 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const invoke = tauriInvoke();
|
|
|
|
|
if (!invoke) return;
|
|
|
|
|
|
|
|
|
|
let totalHighlights = 0;
|
|
|
|
|
roomToUnread.forEach((unread) => {
|
|
|
|
|
totalHighlights += unread.highlight;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
invoke('set_badge_count', { count: totalHighlights }).catch(() => {});
|
2026-06-13 23:53:39 -04:00
|
|
|
// Mirror the unread state onto the tray icon (visible when minimized to tray).
|
|
|
|
|
invoke('set_tray_unread', { unread: totalHighlights > 0 }).catch(() => {});
|
|
|
|
|
|
|
|
|
|
// Flash the taskbar button when new mentions arrive and the window is not
|
|
|
|
|
// focused, then reset the baseline.
|
|
|
|
|
if (totalHighlights > prevHighlightsRef.current && !document.hasFocus()) {
|
|
|
|
|
invoke('flash_window').catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
prevHighlightsRef.current = totalHighlights;
|
2026-06-10 17:32:38 -04:00
|
|
|
}, [roomToUnread]);
|
|
|
|
|
}
|