Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(renderer): small changes on the useNotification hook for future text-speech implementation #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/renderer/src/contexts/CounterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 28 additions & 21 deletions app/renderer/src/hooks/useNotification.ts
Original file line number Diff line number Diff line change
@@ -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));
}
Loading