Skip to content

Commit

Permalink
refactor: remove seperate playing context
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Oct 19, 2023
1 parent d7fd17e commit daee746
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 70 deletions.
5 changes: 3 additions & 2 deletions src/components/buttons/play/play.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { BiPause, BiPlay } from 'react-icons/bi/index';
import { motion } from 'framer-motion';

import { useSoundStore } from '@/store';
import { usePlay } from '@/contexts/play';
import { cn } from '@/helpers/styles';

import styles from './play.module.css';

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 handleClick = () => {
Expand Down
55 changes: 26 additions & 29 deletions src/components/categories/categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Container } from '@/components/container';
import { StoreConsumer } from '../store-consumer';
import { Category } from '@/components/category';
import { Buttons } from '@/components/buttons';
import { PlayProvider } from '@/contexts/play';

import { sounds } from '@/data/sounds';

Expand All @@ -34,35 +33,33 @@ export function Categories() {

return (
<StoreConsumer>
<PlayProvider>
<Container>
<Buttons />
<div>
<AnimatePresence initial={false}>
{!!favoriteSounds.length && (
<Category
functional={false}
icon={<BiSolidHeart />}
id="favorites"
title="Favorites"
sounds={
favoriteSounds as Array<{
src: string;
label: string;
id: string;
icon: React.ReactNode;
}>
}
/>
)}
<Container>
<Buttons />
<div>
<AnimatePresence initial={false}>
{!!favoriteSounds.length && (
<Category
functional={false}
icon={<BiSolidHeart />}
id="favorites"
title="Favorites"
sounds={
favoriteSounds as Array<{
src: string;
label: string;
id: string;
icon: React.ReactNode;
}>
}
/>
)}

{categories.map(category => (
<Category {...category} key={category.id} />
))}
</AnimatePresence>
</div>
</Container>
</PlayProvider>
{categories.map(category => (
<Category {...category} key={category.id} />
))}
</AnimatePresence>
</div>
</Container>
</StoreConsumer>
);
}
5 changes: 2 additions & 3 deletions src/components/sound/sound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Like } from './like';

import { useSound } from '@/hooks/use-sound';
import { useSoundStore } from '@/store';
import { usePlay } from '@/contexts/play';
import { cn } from '@/helpers/styles';

import styles from './sound.module.css';
Expand All @@ -31,8 +30,8 @@ export function Sound({
src,
unselectHidden,
}: SoundProps) {
const { isPlaying, play } = usePlay();

const isPlaying = useSoundStore(state => state.isPlaying);
const play = useSoundStore(state => state.play);
const select = useSoundStore(state => state.select);
const unselect = useSoundStore(state => state.unselect);
const setVolume = useSoundStore(state => state.setVolume);
Expand Down
36 changes: 0 additions & 36 deletions src/contexts/play.tsx

This file was deleted.

15 changes: 15 additions & 0 deletions src/store/sound/sound.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface SoundActions {
unselectAll: (pushToHistory?: boolean) => void;
restoreHistory: () => void;
toggleFavorite: (id: string) => void;
pause: () => void;
play: () => void;
togglePlay: () => void;
}

export const createActions: StateCreator<
Expand All @@ -18,6 +21,14 @@ export const createActions: StateCreator<
SoundActions
> = (set, get) => {
return {
pause() {
set({ isPlaying: false });
},

play() {
set({ isPlaying: true });
},

restoreHistory() {
const history = get().history;

Expand Down Expand Up @@ -57,6 +68,10 @@ export const createActions: StateCreator<
});
},

togglePlay() {
set({ isPlaying: !get().isPlaying });
},

unselect(id) {
set({
sounds: {
Expand Down
2 changes: 2 additions & 0 deletions src/store/sound/sound.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SoundActions } from './sound.actions';
import { sounds } from '@/data/sounds';

export interface SoundState {
isPlaying: boolean;
sounds: {
[id: string]: {
isSelected: boolean;
Expand Down Expand Up @@ -38,6 +39,7 @@ export const createState: StateCreator<
return favorites;
},
history: null,
isPlaying: false,
noSelected() {
const { sounds } = get();
const keys = Object.keys(sounds);
Expand Down

0 comments on commit daee746

Please sign in to comment.