mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 17:04:15 +00:00
feat: add auto pause to play button
This commit is contained in:
parent
22bb65de0d
commit
7c901b2bdc
2 changed files with 28 additions and 4 deletions
|
|
@ -1,14 +1,27 @@
|
||||||
|
import { useEffect } from 'react';
|
||||||
import { BiPause, BiPlay } from 'react-icons/bi/index';
|
import { BiPause, BiPlay } from 'react-icons/bi/index';
|
||||||
|
|
||||||
|
import { useSoundStore } from '@/store';
|
||||||
import { usePlay } from '@/contexts/play';
|
import { usePlay } from '@/contexts/play';
|
||||||
|
|
||||||
import styles from './play-button.module.css';
|
import styles from './play-button.module.css';
|
||||||
|
|
||||||
export function PlayButton() {
|
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 (
|
return (
|
||||||
<button className={styles.playButton} onClick={toggle}>
|
<button className={styles.playButton} onClick={handleClick}>
|
||||||
{isPlaying ? (
|
{isPlaying ? (
|
||||||
<>
|
<>
|
||||||
<span>
|
<span>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ export interface SoundState {
|
||||||
volume: number;
|
volume: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
noSelected: () => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createState: StateCreator<
|
export const createState: StateCreator<
|
||||||
|
|
@ -18,8 +19,18 @@ export const createState: StateCreator<
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
SoundState
|
SoundState
|
||||||
> = () => {
|
> = (set, get) => {
|
||||||
const state: SoundState = { sounds: {} };
|
const state: SoundState = {
|
||||||
|
noSelected() {
|
||||||
|
const { sounds } = get();
|
||||||
|
const keys = Object.keys(sounds);
|
||||||
|
|
||||||
|
return keys.every(key => !sounds[key].isSelected);
|
||||||
|
},
|
||||||
|
|
||||||
|
sounds: {},
|
||||||
|
};
|
||||||
|
|
||||||
const { categories } = sounds;
|
const { categories } = sounds;
|
||||||
|
|
||||||
categories.forEach(category => {
|
categories.forEach(category => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue