mirror of
https://github.com/remvze/moodist.git
synced 2025-10-17 03:50:30 -04:00
feat: add auto pause to play button
This commit is contained in:
parent
22bb65de0d
commit
7c901b2bdc
@ -1,14 +1,27 @@
|
||||
import { useEffect } from 'react';
|
||||
import { BiPause, BiPlay } from 'react-icons/bi/index';
|
||||
|
||||
import { useSoundStore } from '@/store';
|
||||
import { usePlay } from '@/contexts/play';
|
||||
|
||||
import styles from './play-button.module.css';
|
||||
|
||||
export function PlayButton() {
|
||||
const { isPlaying, toggle } = usePlay();
|
||||
const { isPlaying, pause, toggle } = usePlay();
|
||||
const noSelected = useSoundStore(state => state.noSelected());
|
||||
|
||||
const handleClick = () => {
|
||||
if (noSelected) return pause();
|
||||
|
||||
toggle();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (isPlaying && noSelected) pause();
|
||||
}, [isPlaying, pause, noSelected]);
|
||||
|
||||
return (
|
||||
<button className={styles.playButton} onClick={toggle}>
|
||||
<button className={styles.playButton} onClick={handleClick}>
|
||||
{isPlaying ? (
|
||||
<>
|
||||
<span>
|
||||
|
@ -11,6 +11,7 @@ export interface SoundState {
|
||||
volume: number;
|
||||
};
|
||||
};
|
||||
noSelected: () => boolean;
|
||||
}
|
||||
|
||||
export const createState: StateCreator<
|
||||
@ -18,8 +19,18 @@ export const createState: StateCreator<
|
||||
[],
|
||||
[],
|
||||
SoundState
|
||||
> = () => {
|
||||
const state: SoundState = { sounds: {} };
|
||||
> = (set, get) => {
|
||||
const state: SoundState = {
|
||||
noSelected() {
|
||||
const { sounds } = get();
|
||||
const keys = Object.keys(sounds);
|
||||
|
||||
return keys.every(key => !sounds[key].isSelected);
|
||||
},
|
||||
|
||||
sounds: {},
|
||||
};
|
||||
|
||||
const { categories } = sounds;
|
||||
|
||||
categories.forEach(category => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user