Skip to content

Commit

Permalink
fix: start audio recorder timer if already recording
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Jul 16, 2024
1 parent 6033466 commit c609f61
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ export const AudioRecordingInProgress = () => {

useEffect(() => {
if (!recorder?.mediaRecorder) return;

const { mediaRecorder } = recorder;

if (mediaRecorder.state === 'recording') {
startCounter();

Check warning on line 58 in src/components/MediaRecorder/AudioRecorder/AudioRecordingInProgress.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/MediaRecorder/AudioRecorder/AudioRecordingInProgress.tsx#L58

Added line #L58 was not covered by tests
}

mediaRecorder.addEventListener('start', startCounter);
mediaRecorder.addEventListener('resume', startCounter);
mediaRecorder.addEventListener('stop', stopCounter);
Expand Down
8 changes: 4 additions & 4 deletions src/components/MessageInput/hooks/useTimeElapsed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ export const useTimeElapsed = ({ startOnMount }: UseTimeElapsedParams = {}) => {
const updateInterval = useRef<ReturnType<typeof setInterval>>();

const startCounter = useCallback(() => {
if (updateInterval.current) return;
updateInterval.current = setInterval(() => {
setSecondsElapsed((prev) => prev + 1);
}, 1000);
}, []);

const stopCounter = useCallback(() => {
clearInterval(updateInterval.current);
updateInterval.current = undefined;

Check warning on line 21 in src/components/MessageInput/hooks/useTimeElapsed.ts

View check run for this annotation

Codecov / codecov/patch

src/components/MessageInput/hooks/useTimeElapsed.ts#L21

Added line #L21 was not covered by tests
}, []);

useEffect(() => {
if (!startOnMount) return;
updateInterval.current = setInterval(() => {
setSecondsElapsed((prev) => prev + 1);
}, 1000);
startCounter();

Check warning on line 26 in src/components/MessageInput/hooks/useTimeElapsed.ts

View check run for this annotation

Codecov / codecov/patch

src/components/MessageInput/hooks/useTimeElapsed.ts#L26

Added line #L26 was not covered by tests
return () => {
stopCounter();
};
}, [startOnMount, stopCounter]);
}, [startCounter, startOnMount, stopCounter]);

return {
secondsElapsed,
Expand Down

0 comments on commit c609f61

Please sign in to comment.