Skip to content

Commit c44fc1e

Browse files
author
Pascal Wegner
committed
Add audio
1 parent 58c2472 commit c44fc1e

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

packages/pwa/public/BeepBreak.mp3

20.5 KB
Binary file not shown.
21 KB
Binary file not shown.

packages/pwa/public/BeepWork.mp3

20.5 KB
Binary file not shown.
21 KB
Binary file not shown.

packages/pwa/src/hooks/useAudio.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const useAudio = (url: string) => {
2+
/* istanbul ignore next */
3+
if (typeof window === undefined) {
4+
return { audio: undefined };
5+
}
6+
7+
const audio = new Audio(url);
8+
return { audio };
9+
};

packages/pwa/src/hooks/useBeep.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useAudio } from './useAudio';
2+
export const useBeep = () => {
3+
const { audio: beepBreak } = useAudio('/BeepBreak.mp3');
4+
const { audio: beepWork } = useAudio('/BeepWork.mp3');
5+
const { audio: beepBreakLong } = useAudio('/BeepBreakLong.mp3');
6+
const { audio: beepWorkLong } = useAudio('/BeepWorkLong.mp3');
7+
return {
8+
beepBreak,
9+
beepBreakLong,
10+
beepWork,
11+
beepWorkLong,
12+
};
13+
};

packages/pwa/src/pages/index.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
import { FormFields } from '../components/FormFields/FormFields';
2-
import { timerMachine, timerStates, TTimerStates } from '@interval-timer/core';
2+
import { timerMachine, timerStates } from '@interval-timer/core';
33
import { useMachine } from '@xstate/react';
44
import * as React from 'react';
55
import { Counter } from '../components/Counter/Counter';
6+
import { useBeep } from '../hooks/useBeep';
67

78
export default function Home() {
8-
const [state, send, service] = useMachine(timerMachine);
9+
const { beepBreak, beepBreakLong, beepWork, beepWorkLong } = useBeep();
10+
const [state, send, service] = useMachine(timerMachine, {
11+
actions: {
12+
initBreakEffect: () => {
13+
beepBreakLong.play();
14+
},
15+
initWorkEffect: () => {
16+
beepWorkLong.play();
17+
},
18+
countDownLastWorkEffect: () => {
19+
beepWork.pause();
20+
beepWork.currentTime = 0;
21+
beepWork.play();
22+
},
23+
countDownLastBreakEffect: () => {
24+
beepBreak.pause();
25+
beepBreak.currentTime = 0;
26+
beepBreak.play();
27+
},
28+
},
29+
});
930

1031
React.useEffect(() => {
1132
const intervalWorker = new Worker('/intervalWorker.js');

0 commit comments

Comments
 (0)