mirror of
https://github.com/remvze/moodist.git
synced 2025-12-18 17:34:17 +00:00
refactor: reduce dependency
This commit is contained in:
parent
f025213ef2
commit
c893e2a6ad
3 changed files with 16 additions and 7 deletions
|
|
@ -16,11 +16,13 @@ import { SnackbarProvider } from '@/contexts/snackbar';
|
||||||
import { sounds } from '@/data/sounds';
|
import { sounds } from '@/data/sounds';
|
||||||
|
|
||||||
import type { Sound } from '@/data/types';
|
import type { Sound } from '@/data/types';
|
||||||
|
import { subscribe } from '@/lib/event';
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
const categories = useMemo(() => sounds.categories, []);
|
const categories = useMemo(() => sounds.categories, []);
|
||||||
|
|
||||||
const favorites = useSoundStore(useShallow(state => state.getFavorites()));
|
const favorites = useSoundStore(useShallow(state => state.getFavorites()));
|
||||||
|
const pause = useSoundStore(state => state.pause);
|
||||||
|
|
||||||
const favoriteSounds = useMemo(() => {
|
const favoriteSounds = useMemo(() => {
|
||||||
const favoriteSounds = categories
|
const favoriteSounds = categories
|
||||||
|
|
@ -52,6 +54,16 @@ export function App() {
|
||||||
return () => document.removeEventListener('visibilitychange', onChange);
|
return () => document.removeEventListener('visibilitychange', onChange);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = subscribe('fadeOut', (e: { duration: number }) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
pause();
|
||||||
|
}, e.duration);
|
||||||
|
});
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
}, [pause]);
|
||||||
|
|
||||||
const allCategories = useMemo(() => {
|
const allCategories = useMemo(() => {
|
||||||
const favorites = [];
|
const favorites = [];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ export function SleepTimerModal({ onClose, show }: SleepTimerModalProps) {
|
||||||
if (timeLeft === 0) {
|
if (timeLeft === 0) {
|
||||||
setRunning(false);
|
setRunning(false);
|
||||||
// pause();
|
// pause();
|
||||||
dispatch('fadeOut', { duration: 5000 });
|
dispatch('fadeOut', { duration: 1000 });
|
||||||
|
|
||||||
setTimeSpent(0);
|
setTimeSpent(0);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { useMemo, useEffect, useCallback, useState } from 'react';
|
import { useMemo, useEffect, useCallback, useState } from 'react';
|
||||||
import { Howl } from 'howler';
|
import { Howl } from 'howler';
|
||||||
|
|
||||||
import { useSoundStore, useLoadingStore } from '@/store';
|
import { useLoadingStore } from '@/store';
|
||||||
import { subscribe } from '@/lib/event';
|
import { subscribe } from '@/lib/event';
|
||||||
import { useSSR } from './use-ssr';
|
import { useSSR } from './use-ssr';
|
||||||
|
|
||||||
|
|
@ -9,8 +9,6 @@ export function useSound(
|
||||||
src: string,
|
src: string,
|
||||||
options: { loop?: boolean; volume?: number } = {},
|
options: { loop?: boolean; volume?: number } = {},
|
||||||
) {
|
) {
|
||||||
const pauseAll = useSoundStore(state => state.pause);
|
|
||||||
|
|
||||||
const [hasLoaded, setHasLoaded] = useState(false);
|
const [hasLoaded, setHasLoaded] = useState(false);
|
||||||
const isLoading = useLoadingStore(state => state.loaders[src]);
|
const isLoading = useLoadingStore(state => state.loaders[src]);
|
||||||
const setIsLoading = useLoadingStore(state => state.set);
|
const setIsLoading = useLoadingStore(state => state.set);
|
||||||
|
|
@ -70,12 +68,11 @@ export function useSound(
|
||||||
sound?.fade(sound.volume(), 0, duration);
|
sound?.fade(sound.volume(), 0, duration);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
pauseAll();
|
pause();
|
||||||
|
|
||||||
sound?.volume(options.volume || 0.5);
|
sound?.volume(options.volume || 0.5);
|
||||||
}, duration);
|
}, duration);
|
||||||
},
|
},
|
||||||
[options.volume, sound, pauseAll],
|
[options.volume, sound, pause],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue