From cbb4c5c509dd1d88c32a89725763bd01b1d1f24b Mon Sep 17 00:00:00 2001 From: sijumoncy <72241997+sijumoncy@users.noreply.github.com> Date: Thu, 30 May 2024 15:23:15 +0530 Subject: [PATCH] persist volume level for system --- .../AudioRecorder/components/Player.js | 40 ++++++++++++++----- renderer/src/layouts/editor/EditorSection.js | 1 + 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/renderer/src/components/AudioRecorder/components/Player.js b/renderer/src/components/AudioRecorder/components/Player.js index 7bce950f3..ce63fb5b8 100644 --- a/renderer/src/components/AudioRecorder/components/Player.js +++ b/renderer/src/components/AudioRecorder/components/Player.js @@ -17,6 +17,8 @@ import { useCallback, useEffect, useState } from 'react'; import PlayIcon from '@/icons/basil/Outline/Media/Play.svg'; import PauseIcon from '@/icons/basil/Outline/Media/Pause.svg'; +const LS_AUDIO_VOLUME_KEY = 'audio-volume'; + const AudioWaveform = dynamic(() => import('./WaveForm'), { ssr: false }); const Player = ({ @@ -36,7 +38,8 @@ const Player = ({ location, }) => { const { t } = useTranslation(); - const [volume, setVolume] = useState(0.5); + // const [volume, setVolume] = useState(0.5); + const [volume, setVolume] = useState((Number(localStorage.getItem(LS_AUDIO_VOLUME_KEY)) && typeof Number(localStorage.getItem(LS_AUDIO_VOLUME_KEY) === 'number')) ? Number(localStorage.getItem(LS_AUDIO_VOLUME_KEY)) : 0.5); const [currentSpeed, setCurrentSpeed] = useState(1); const speed = [0.5, 1, 1.5, 2]; const path = require('path'); @@ -45,6 +48,23 @@ const Player = ({ // state to check stopwatch running or not const [isRunning, setIsRunning] = useState(false); + const handleVolumeChange = (action, value = 0.1, sliding = false) => { + // sliding the value will be tha actual value of slide + if (sliding) { + setVolume(value); + } else if (action === 'inc' && !sliding) { + // if not sliding the value will be the step value + setVolume((prev) => (prev > 0.9 ? prev : prev + value)); + } else if (action === 'dec' && !sliding) { + setVolume((prev) => (prev < 0.1 ? prev : prev - value)); + } + }; + + // volume + useEffect(() => { + localStorage.setItem(LS_AUDIO_VOLUME_KEY, volume); + }, [volume]); + useEffect(() => { let intervalId; if (isRunning) { @@ -137,10 +157,12 @@ const Player = ({ changeTake('take3'); break; case 187: // --> + (not in number area) - setVolume((prev) => (prev > 0.9 ? prev : prev + 0.1)); + // setVolume((prev) => (prev > 0.9 ? prev : prev + 0.1)); + handleVolumeChange('inc'); break; - case 189: // --> - (left to +) - setVolume((prev) => (prev < 0.1 ? prev : prev - 0.1)); + case 189: // --> - (left to +) + handleVolumeChange('dec'); + // setVolume((prev) => (prev < 0.1 ? prev : prev - 0.1)); break; default: @@ -372,9 +394,7 @@ const Player = ({ type="button" className="rounded-md hover:bg-primary" title="-" - onClick={() => setVolume( - volume < 0.1 ? volume : volume - 0.1, - )} + onClick={() => handleVolumeChange('dec')} > handleVolumeChange('', Number(e.target.value), true)} /> +