Skip to content

Commit

Permalink
refactor: reduce dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Apr 30, 2024
1 parent f025213 commit c893e2a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import { SnackbarProvider } from '@/contexts/snackbar';
import { sounds } from '@/data/sounds';

import type { Sound } from '@/data/types';
import { subscribe } from '@/lib/event';

export function App() {
const categories = useMemo(() => sounds.categories, []);

const favorites = useSoundStore(useShallow(state => state.getFavorites()));
const pause = useSoundStore(state => state.pause);

const favoriteSounds = useMemo(() => {
const favoriteSounds = categories
Expand Down Expand Up @@ -52,6 +54,16 @@ export function App() {
return () => document.removeEventListener('visibilitychange', onChange);
}, []);

useEffect(() => {
const unsubscribe = subscribe('fadeOut', (e: { duration: number }) => {
setTimeout(() => {
pause();
}, e.duration);
});

return unsubscribe;
}, [pause]);

const allCategories = useMemo(() => {
const favorites = [];

Expand Down
2 changes: 1 addition & 1 deletion src/components/modals/sleep-timer/sleep-timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function SleepTimerModal({ onClose, show }: SleepTimerModalProps) {
if (timeLeft === 0) {
setRunning(false);
// pause();
dispatch('fadeOut', { duration: 5000 });
dispatch('fadeOut', { duration: 1000 });

setTimeSpent(0);

Expand Down
9 changes: 3 additions & 6 deletions src/hooks/use-sound.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { useMemo, useEffect, useCallback, useState } from 'react';
import { Howl } from 'howler';

import { useSoundStore, useLoadingStore } from '@/store';
import { useLoadingStore } from '@/store';
import { subscribe } from '@/lib/event';
import { useSSR } from './use-ssr';

export function useSound(
src: string,
options: { loop?: boolean; volume?: number } = {},
) {
const pauseAll = useSoundStore(state => state.pause);

const [hasLoaded, setHasLoaded] = useState(false);
const isLoading = useLoadingStore(state => state.loaders[src]);
const setIsLoading = useLoadingStore(state => state.set);
Expand Down Expand Up @@ -70,12 +68,11 @@ export function useSound(
sound?.fade(sound.volume(), 0, duration);

setTimeout(() => {
pauseAll();

pause();
sound?.volume(options.volume || 0.5);
}, duration);
},
[options.volume, sound, pauseAll],
[options.volume, sound, pause],
);

useEffect(() => {
Expand Down

0 comments on commit c893e2a

Please sign in to comment.