Enable browser notifications by default and always show them

Default prefs now start enabled with osa-announcements channel on.
Removed visibility check (tab focus) and self-filter so all alerts
trigger a browser notification regardless of who created them.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-13 13:48:07 -07:00
parent 57e8de0e7f
commit 5c67919c47

View File

@@ -1,6 +1,5 @@
import { useCallback, useRef, useSyncExternalStore } from "react";
import { useMqttRawSubscription } from "@/hooks/use-mqtt";
import { useAuthContext } from "@/contexts/auth-context";
// ── localStorage sync ────────────────────────────────────────────
@@ -12,7 +11,7 @@ interface NotificationPrefs {
channels: string[];
}
const DEFAULT_PREFS: NotificationPrefs = { enabled: false, channels: [] };
const DEFAULT_PREFS: NotificationPrefs = { enabled: true, channels: ["osa-announcements"] };
// Cache the snapshot so useSyncExternalStore gets a stable reference
let cachedRaw: string | null = null;
@@ -108,7 +107,6 @@ export function useNotificationPreference() {
export function useNotificationTrigger() {
const { enabled, channels, permissionState } = useNotificationPreference();
const { user } = useAuthContext();
const enabledRef = useRef(enabled);
enabledRef.current = enabled;
@@ -116,16 +114,11 @@ export function useNotificationTrigger() {
channelsRef.current = channels;
const permRef = useRef(permissionState);
permRef.current = permissionState;
const userRef = useRef(user);
userRef.current = user;
const handleMessage = useCallback(
(topic: string, payload: Record<string, unknown>) => {
if (!enabledRef.current || permRef.current !== "granted") return;
// Don't notify when tab is focused
if (document.visibilityState === "visible") return;
// Extract channel from topic: mgmt/notifications/{channel}
const parts = topic.split("/");
const channel = parts[parts.length - 1];
@@ -134,16 +127,6 @@ export function useNotificationTrigger() {
// Check if user is subscribed to this channel
if (!channelsRef.current.includes(channel)) return;
// Skip own announcements
if (
channel === "osa-announcements" &&
payload.created_by &&
userRef.current &&
payload.created_by === (userRef.current as { username?: string }).username
) {
return;
}
const title = (payload.title as string) || "MGMT Notification";
const body = (payload.body as string) || "";
const id = (payload.id as string) || String(Date.now());