Spaces:
Build error
Build error
File size: 1,855 Bytes
b59aa07 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import { useCallback, useRef } from "react";
import notificationSound from "../assets/notification.mp3";
import { useSettings } from "./query/use-settings";
export const useNotification = () => {
const { data: settings } = useSettings();
const audioRef = useRef<HTMLAudioElement | undefined>(undefined);
// Initialize audio only in browser environment
if (typeof window !== "undefined" && !audioRef.current) {
audioRef.current = new Audio(notificationSound);
audioRef.current.volume = 0.5;
}
const notify = useCallback(
async (
title: string,
options?: NotificationOptions & { playSound?: boolean },
): Promise<Notification | undefined> => {
if (typeof window === "undefined") return undefined;
// Only play sound if:
// 1. Explicitly requested via playSound option
// 2. Sound notifications are enabled in settings
// 3. Audio is available
// 4. Not a settings-related notification
if (
options?.playSound === true && // Must be explicitly true
settings?.ENABLE_SOUND_NOTIFICATIONS &&
audioRef.current &&
!title.includes("BUTTON$") // Don't play for button/settings actions
) {
// Reset and play sound
audioRef.current.currentTime = 0;
audioRef.current.play().catch(() => {
// Ignore autoplay errors
});
}
if (Notification.permission === "default") {
await Notification.requestPermission();
}
if (Notification.permission === "granted") {
// Remove playSound from options before passing to Notification
const { playSound, ...notificationOptions } = options || {};
return new Notification(title, notificationOptions);
}
return undefined;
},
[settings?.ENABLE_SOUND_NOTIFICATIONS],
);
return { notify };
};
|