Skip to content

Commit

Permalink
feat(ui): add toggle-muted function to global volume button
Browse files Browse the repository at this point in the history
resolve #506
  • Loading branch information
mateusfg7 committed Aug 7, 2023
1 parent ad967a9 commit e38df19
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/components/header/global-volume-controller/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react'
import { FiVolume2, FiVolume1, FiVolume } from 'react-icons/fi'
import { FiVolumeX, FiVolume2, FiVolume1, FiVolume } from 'react-icons/fi'

import { useGlobalVolumeStore } from '@/stores/global-volume-store'
import { useThemeStore } from '@/stores/theme-store'
Expand All @@ -15,11 +15,25 @@ export function GlobalVolumeController() {
const setGlobalVolume = useGlobalVolumeStore(state => state.setGlobalVolume)
const globalVolume = useGlobalVolumeStore(state => state.globalVolume)

const [currentVolume, setCurrentVolume] = useState(globalVolume)
const [isMuted, setIsMuted] = useState(false)

function handleVolume(value: number) {
setGlobalVolume(value / MAX_VALUE)
setRangeValue(value)
}

function toggleMuted() {
if (isMuted) {
setIsMuted(false)
handleVolume(currentVolume)
} else {
setIsMuted(true)
setCurrentVolume(globalVolume * MAX_VALUE)
handleVolume(0)
}
}

const theme = useThemeStore(set => set.theme)

return (
Expand Down Expand Up @@ -50,13 +64,14 @@ export function GlobalVolumeController() {
/>
</div>
<button
title="Toggle Global Volume Controller"
title="Enable/disable sound"
className={soundButton({ theme })}
onClick={() => setIsShowing(!isShowing)}
onClick={toggleMuted}
>
{globalVolume >= 0.5 && <FiVolume2 size={25} />}
{globalVolume >= 0.1 && globalVolume < 0.5 && <FiVolume1 size={25} />}
{globalVolume < 0.1 && <FiVolume size={25} />}
{globalVolume >= 0.25 && globalVolume < 0.5 && <FiVolume1 size={25} />}
{globalVolume > 0 && globalVolume <= 0.25 && <FiVolume size={25} />}
{globalVolume === 0 && <FiVolumeX size={25} />}
</button>
</div>
)
Expand Down

0 comments on commit e38df19

Please sign in to comment.