-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor music player and add volume controls
- Loading branch information
Showing
5 changed files
with
207 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Suspense, useContext, useState } from 'react' | ||
import ReactPlayer from 'react-player' | ||
|
||
import { | ||
PauseIcon, | ||
PlayIcon, | ||
Volume1Icon, | ||
Volume2Icon, | ||
VolumeXIcon, | ||
} from 'lucide-react' | ||
import { Button } from '~/components/ui/button' | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from '~/components/ui/popover' | ||
import { Separator } from '~/components/ui/separator' | ||
import { Slider } from '~/components/ui/slider' | ||
import { MUSIC_STATIONS } from '~/lib/constants' | ||
import { AureliusContext, AureliusProviderData } from '~/lib/providers/aurelius' | ||
import { MusicChannels } from '~/lib/types' | ||
|
||
type MusicPlayerProps = { | ||
focusMode: boolean | ||
isMusicPlaying: boolean | ||
setIsMusicPlaying: (isPlaying: boolean) => void | ||
} | ||
|
||
const MusicPlayer = ({ | ||
focusMode, | ||
isMusicPlaying, | ||
setIsMusicPlaying, | ||
}: MusicPlayerProps) => { | ||
const { settings } = useContext<AureliusProviderData>(AureliusContext) | ||
|
||
const [volume, setVolume] = useState(50) | ||
|
||
return ( | ||
<div | ||
className={`p-4 flex items-center transition-opacity duration-100 hover:opacity-100 ${focusMode ? 'opacity-5' : 'opacity-100'}`} | ||
> | ||
<div className='flex items-center h-9 bg-background rounded-lg border border-border'> | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button className='w-9 h-9' size='icon' variant='ghost'> | ||
{volume === 0 ? ( | ||
<VolumeXIcon className='w-4 h-4' /> | ||
) : volume > 0 && volume <= 50 ? ( | ||
<Volume1Icon className='w-4 h-4' /> | ||
) : ( | ||
<Volume2Icon className='w-4 h-4' /> | ||
)} | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className='w-9 h-48 p-0 flex justify-center py-4'> | ||
<Slider | ||
max={100} | ||
min={0} | ||
onValueChange={(value) => setVolume(value[0])} | ||
orientation='vertical' | ||
value={[volume]} | ||
/> | ||
</PopoverContent> | ||
</Popover> | ||
<Separator orientation='vertical' /> | ||
{isMusicPlaying ? ( | ||
<Button | ||
className='w-9 h-9' | ||
onClick={() => setIsMusicPlaying?.(false)} | ||
size='icon' | ||
variant='ghost' | ||
> | ||
<PauseIcon className='w-4 h-4' /> | ||
</Button> | ||
) : ( | ||
<Button | ||
className='w-9 h-9' | ||
onClick={() => setIsMusicPlaying?.(true)} | ||
size='icon' | ||
variant='ghost' | ||
> | ||
<PlayIcon className='w-4 h-4' /> | ||
</Button> | ||
)} | ||
</div> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<ReactPlayer | ||
playing={isMusicPlaying} | ||
// @ts-ignore | ||
url={ | ||
settings?.youtubeLink || | ||
MUSIC_STATIONS[settings?.musicChannel as MusicChannels] | ||
} | ||
width='0' | ||
height='0' | ||
loop={true} | ||
volume={volume / 100} | ||
config={{ | ||
youtube: { | ||
playerVars: { | ||
control: 1, | ||
start: 1, | ||
}, | ||
}, | ||
}} | ||
/> | ||
</Suspense> | ||
</div> | ||
) | ||
} | ||
|
||
export default MusicPlayer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as React from 'react' | ||
|
||
import * as SliderPrimitive from '@radix-ui/react-slider' | ||
import { cn } from '~/lib/utils' | ||
|
||
const Slider = React.forwardRef< | ||
React.ElementRef<typeof SliderPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<SliderPrimitive.Root | ||
ref={ref} | ||
className={cn( | ||
'relative flex touch-none select-none', | ||
props.orientation === 'vertical' | ||
? 'h-full flex-col justify-center' | ||
: 'w-full items-center', | ||
className | ||
)} | ||
{...props} | ||
> | ||
<SliderPrimitive.Track | ||
className={cn( | ||
'relative grow overflow-hidden rounded-full bg-secondary', | ||
props.orientation === 'vertical' ? 'h-full w-2' : 'h-2 w-full' | ||
)} | ||
> | ||
<SliderPrimitive.Range | ||
className={cn( | ||
'absolute bg-primary', | ||
props.orientation === 'vertical' ? 'w-full' : 'h-full' | ||
)} | ||
/> | ||
</SliderPrimitive.Track> | ||
<SliderPrimitive.Thumb | ||
className={cn( | ||
'flex h-5 w-5 rounded-full border-2 border-primary ring-offset-background transition-colors disabled:pointer-events-none disabled:opacity-50', | ||
'bg-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2', | ||
props.orientation === 'vertical' ? '-ml-1.5' : '' | ||
)} | ||
/> | ||
</SliderPrimitive.Root> | ||
)) | ||
Slider.displayName = SliderPrimitive.Root.displayName | ||
|
||
export { Slider } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.