mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 08:54:13 +00:00
refactor: remove seperate playing context
This commit is contained in:
parent
d7fd17ea8b
commit
daee7465bc
6 changed files with 48 additions and 70 deletions
|
|
@ -3,13 +3,14 @@ import { BiPause, BiPlay } from 'react-icons/bi/index';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
|
|
||||||
import { useSoundStore } from '@/store';
|
import { useSoundStore } from '@/store';
|
||||||
import { usePlay } from '@/contexts/play';
|
|
||||||
import { cn } from '@/helpers/styles';
|
import { cn } from '@/helpers/styles';
|
||||||
|
|
||||||
import styles from './play.module.css';
|
import styles from './play.module.css';
|
||||||
|
|
||||||
export function PlayButton() {
|
export function PlayButton() {
|
||||||
const { isPlaying, pause, toggle } = usePlay();
|
const isPlaying = useSoundStore(state => state.isPlaying);
|
||||||
|
const pause = useSoundStore(state => state.pause);
|
||||||
|
const toggle = useSoundStore(state => state.togglePlay);
|
||||||
const noSelected = useSoundStore(state => state.noSelected());
|
const noSelected = useSoundStore(state => state.noSelected());
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import { Container } from '@/components/container';
|
||||||
import { StoreConsumer } from '../store-consumer';
|
import { StoreConsumer } from '../store-consumer';
|
||||||
import { Category } from '@/components/category';
|
import { Category } from '@/components/category';
|
||||||
import { Buttons } from '@/components/buttons';
|
import { Buttons } from '@/components/buttons';
|
||||||
import { PlayProvider } from '@/contexts/play';
|
|
||||||
|
|
||||||
import { sounds } from '@/data/sounds';
|
import { sounds } from '@/data/sounds';
|
||||||
|
|
||||||
|
|
@ -34,35 +33,33 @@ export function Categories() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StoreConsumer>
|
<StoreConsumer>
|
||||||
<PlayProvider>
|
<Container>
|
||||||
<Container>
|
<Buttons />
|
||||||
<Buttons />
|
<div>
|
||||||
<div>
|
<AnimatePresence initial={false}>
|
||||||
<AnimatePresence initial={false}>
|
{!!favoriteSounds.length && (
|
||||||
{!!favoriteSounds.length && (
|
<Category
|
||||||
<Category
|
functional={false}
|
||||||
functional={false}
|
icon={<BiSolidHeart />}
|
||||||
icon={<BiSolidHeart />}
|
id="favorites"
|
||||||
id="favorites"
|
title="Favorites"
|
||||||
title="Favorites"
|
sounds={
|
||||||
sounds={
|
favoriteSounds as Array<{
|
||||||
favoriteSounds as Array<{
|
src: string;
|
||||||
src: string;
|
label: string;
|
||||||
label: string;
|
id: string;
|
||||||
id: string;
|
icon: React.ReactNode;
|
||||||
icon: React.ReactNode;
|
}>
|
||||||
}>
|
}
|
||||||
}
|
/>
|
||||||
/>
|
)}
|
||||||
)}
|
|
||||||
|
|
||||||
{categories.map(category => (
|
{categories.map(category => (
|
||||||
<Category {...category} key={category.id} />
|
<Category {...category} key={category.id} />
|
||||||
))}
|
))}
|
||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
</div>
|
</div>
|
||||||
</Container>
|
</Container>
|
||||||
</PlayProvider>
|
|
||||||
</StoreConsumer>
|
</StoreConsumer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import { Like } from './like';
|
||||||
|
|
||||||
import { useSound } from '@/hooks/use-sound';
|
import { useSound } from '@/hooks/use-sound';
|
||||||
import { useSoundStore } from '@/store';
|
import { useSoundStore } from '@/store';
|
||||||
import { usePlay } from '@/contexts/play';
|
|
||||||
import { cn } from '@/helpers/styles';
|
import { cn } from '@/helpers/styles';
|
||||||
|
|
||||||
import styles from './sound.module.css';
|
import styles from './sound.module.css';
|
||||||
|
|
@ -31,8 +30,8 @@ export function Sound({
|
||||||
src,
|
src,
|
||||||
unselectHidden,
|
unselectHidden,
|
||||||
}: SoundProps) {
|
}: SoundProps) {
|
||||||
const { isPlaying, play } = usePlay();
|
const isPlaying = useSoundStore(state => state.isPlaying);
|
||||||
|
const play = useSoundStore(state => state.play);
|
||||||
const select = useSoundStore(state => state.select);
|
const select = useSoundStore(state => state.select);
|
||||||
const unselect = useSoundStore(state => state.unselect);
|
const unselect = useSoundStore(state => state.unselect);
|
||||||
const setVolume = useSoundStore(state => state.setVolume);
|
const setVolume = useSoundStore(state => state.setVolume);
|
||||||
|
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
import { createContext, useContext, useState, useCallback } from 'react';
|
|
||||||
|
|
||||||
export const PlayContext = createContext({
|
|
||||||
isPlaying: false,
|
|
||||||
pause: () => {},
|
|
||||||
play: () => {},
|
|
||||||
toggle: () => {},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const usePlay = (): {
|
|
||||||
isPlaying: boolean;
|
|
||||||
pause: () => void;
|
|
||||||
play: () => void;
|
|
||||||
toggle: () => void;
|
|
||||||
} => useContext(PlayContext);
|
|
||||||
|
|
||||||
interface PlayProviderProps {
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function PlayProvider({ children }: PlayProviderProps) {
|
|
||||||
const [isPlaying, setIsPlaying] = useState(false);
|
|
||||||
|
|
||||||
const play = useCallback(() => setIsPlaying(true), []);
|
|
||||||
const pause = useCallback(() => setIsPlaying(false), []);
|
|
||||||
const toggle = useCallback(() => {
|
|
||||||
if (isPlaying) pause();
|
|
||||||
else play();
|
|
||||||
}, [isPlaying, play, pause]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<PlayContext.Provider value={{ isPlaying, pause, play, toggle }}>
|
|
||||||
{children}
|
|
||||||
</PlayContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
@ -9,6 +9,9 @@ export interface SoundActions {
|
||||||
unselectAll: (pushToHistory?: boolean) => void;
|
unselectAll: (pushToHistory?: boolean) => void;
|
||||||
restoreHistory: () => void;
|
restoreHistory: () => void;
|
||||||
toggleFavorite: (id: string) => void;
|
toggleFavorite: (id: string) => void;
|
||||||
|
pause: () => void;
|
||||||
|
play: () => void;
|
||||||
|
togglePlay: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createActions: StateCreator<
|
export const createActions: StateCreator<
|
||||||
|
|
@ -18,6 +21,14 @@ export const createActions: StateCreator<
|
||||||
SoundActions
|
SoundActions
|
||||||
> = (set, get) => {
|
> = (set, get) => {
|
||||||
return {
|
return {
|
||||||
|
pause() {
|
||||||
|
set({ isPlaying: false });
|
||||||
|
},
|
||||||
|
|
||||||
|
play() {
|
||||||
|
set({ isPlaying: true });
|
||||||
|
},
|
||||||
|
|
||||||
restoreHistory() {
|
restoreHistory() {
|
||||||
const history = get().history;
|
const history = get().history;
|
||||||
|
|
||||||
|
|
@ -57,6 +68,10 @@ export const createActions: StateCreator<
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
togglePlay() {
|
||||||
|
set({ isPlaying: !get().isPlaying });
|
||||||
|
},
|
||||||
|
|
||||||
unselect(id) {
|
unselect(id) {
|
||||||
set({
|
set({
|
||||||
sounds: {
|
sounds: {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import type { SoundActions } from './sound.actions';
|
||||||
import { sounds } from '@/data/sounds';
|
import { sounds } from '@/data/sounds';
|
||||||
|
|
||||||
export interface SoundState {
|
export interface SoundState {
|
||||||
|
isPlaying: boolean;
|
||||||
sounds: {
|
sounds: {
|
||||||
[id: string]: {
|
[id: string]: {
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
|
|
@ -38,6 +39,7 @@ export const createState: StateCreator<
|
||||||
return favorites;
|
return favorites;
|
||||||
},
|
},
|
||||||
history: null,
|
history: null,
|
||||||
|
isPlaying: false,
|
||||||
noSelected() {
|
noSelected() {
|
||||||
const { sounds } = get();
|
const { sounds } = get();
|
||||||
const keys = Object.keys(sounds);
|
const keys = Object.keys(sounds);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue