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

persist volume level for system #346

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
40 changes: 30 additions & 10 deletions renderer/src/components/AudioRecorder/components/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')}
>
<MinusIcon
className="w-4 h-4"
Expand All @@ -388,14 +408,14 @@ const Player = ({
max={1}
step={0.1}
value={volume}
onChange={(e) => handleVolumeChange('', Number(e.target.value), true)}
/>

<button
type="button"
className="rounded-md hover:bg-primary"
title="+"
onClick={() => setVolume(
volume > 0.9 ? volume : volume + 0.1,
)}
onClick={() => handleVolumeChange('inc')}
>
<PlusIcon
className="w-4 h-4"
Expand Down
1 change: 1 addition & 0 deletions renderer/src/layouts/editor/EditorSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function EditorSection({
const {
states: { scrollLock, selectedProjectMeta },
} = useContext(ProjectContext);

function removeResource() {
setOpenModal(true);
}
Expand Down
Loading