diff --git a/app/renderer/src/contexts/CounterContext.tsx b/app/renderer/src/contexts/CounterContext.tsx index 9e9f9c96..61b51176 100644 --- a/app/renderer/src/contexts/CounterContext.tsx +++ b/app/renderer/src/contexts/CounterContext.tsx @@ -12,8 +12,9 @@ import sessionCompletedWav from "assets/audios/session-completed.wav"; import sixtySecondsLeftWav from "assets/audios/sixty-seconds-left.wav"; import specialBreakStartedWav from "assets/audios/special-break-started.wav"; import thirtySecondsLeftWav from "assets/audios/thirty-seconds-left.wav"; -import { useAppDispatch, useAppSelector } from "hooks/storeHooks"; +import { useAppDispatch, useAppSelector } from "hooks"; import { TimerStatus } from "store/timer/types"; +import { NotificationTypes } from "store/settings/types"; type CounterProps = { count: number; @@ -45,7 +46,7 @@ const CounterProvider: React.FC = ({ children }) => { icon: notificationIcon, mute: !settings.notificationSoundOn, }, - settings.notificationType !== "none" + settings.notificationType !== NotificationTypes.NONE ); const [shouldFullscreen, setShouldFullscreen] = useState(false); diff --git a/app/renderer/src/hooks/useNotification.ts b/app/renderer/src/hooks/useNotification.ts index db3e22f2..4f7c8be5 100644 --- a/app/renderer/src/hooks/useNotification.ts +++ b/app/renderer/src/hooks/useNotification.ts @@ -1,42 +1,49 @@ import bell from "assets/audios/notification-bell.wav"; -type OptionProps = { +type HookOptions = { mute?: boolean; } & NotificationOptions; export const useNotification = ( - constantOptions?: OptionProps, - notify?: boolean + commonOptions?: HookOptions, + showNotification?: boolean ) => { - return function ( + return async function sendNotification( title: string, options: NotificationOptions, audioSrc?: string ) { - const defaultOptions: NotificationOptions = { - ...constantOptions, + const userOptions = { + ...commonOptions, ...options, - silent: true, }; - // Making sure that notification sound the same - // in all Operating System - - if (!constantOptions?.mute) { - new Audio(bell).play().catch((e) => { - console.warn("There was a problem playing sound", e); - }); + // Making sure that notification sound the same in all Operating System + if (!userOptions.mute) { + await playSound(bell); if (audioSrc) { - setTimeout(() => { - new Audio(audioSrc).play().catch((e) => { - console.warn("There was a problem playing sound", e); - }); - }, 1500); + // Small delay to avoid sound overlapping + await wait(1500); + await playSound(audioSrc); } } - if (!notify) return; - return new window.Notification(title, defaultOptions); + if (showNotification) { + new Notification(title, { ...userOptions, silent: true }); + } }; }; + +async function playSound(audioSrc: string) { + try { + const audio = new Audio(audioSrc); + await audio.play(); + } catch (e) { + console.warn("There was a problem playing sound", e); + } +} + +async function wait(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +}