Skip to content

Commit

Permalink
fix: start audio recorder timer if already recording (#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela authored Jul 22, 2024
1 parent 6033466 commit 836917e
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();
}

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;
}, []);

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

return {
secondsElapsed,
Expand Down

0 comments on commit 836917e

Please sign in to comment.