mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 08:54:13 +00:00
refactor: remove seperate favorite store
This commit is contained in:
parent
7e668e5b39
commit
d7fd17ea8b
8 changed files with 29 additions and 65 deletions
|
|
@ -3,7 +3,7 @@ import { useShallow } from 'zustand/react/shallow';
|
||||||
import { BiSolidHeart } from 'react-icons/bi/index';
|
import { BiSolidHeart } from 'react-icons/bi/index';
|
||||||
import { AnimatePresence } from 'framer-motion';
|
import { AnimatePresence } from 'framer-motion';
|
||||||
|
|
||||||
import { useFavoriteStore } from '@/store/favorite';
|
import { useSoundStore } from '@/store';
|
||||||
|
|
||||||
import { Container } from '@/components/container';
|
import { Container } from '@/components/container';
|
||||||
import { StoreConsumer } from '../store-consumer';
|
import { StoreConsumer } from '../store-consumer';
|
||||||
|
|
@ -16,7 +16,7 @@ import { sounds } from '@/data/sounds';
|
||||||
export function Categories() {
|
export function Categories() {
|
||||||
const categories = useMemo(() => sounds.categories, []);
|
const categories = useMemo(() => sounds.categories, []);
|
||||||
|
|
||||||
const favorites = useFavoriteStore(useShallow(state => state.favorites));
|
const favorites = useSoundStore(useShallow(state => state.getFavorites()));
|
||||||
|
|
||||||
const favoriteSounds = useMemo(() => {
|
const favoriteSounds = useMemo(() => {
|
||||||
const favoriteSounds = categories
|
const favoriteSounds = categories
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { BiHeart, BiSolidHeart } from 'react-icons/bi/index';
|
import { BiHeart, BiSolidHeart } from 'react-icons/bi/index';
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
import { AnimatePresence, motion } from 'framer-motion';
|
||||||
|
|
||||||
import { useFavoriteStore } from '@/store/favorite';
|
import { useSoundStore } from '@/store';
|
||||||
import { cn } from '@/helpers/styles';
|
import { cn } from '@/helpers/styles';
|
||||||
import { fade } from '@/lib/motion';
|
import { fade } from '@/lib/motion';
|
||||||
|
|
||||||
|
|
@ -12,8 +12,8 @@ interface LikeProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Like({ id }: LikeProps) {
|
export function Like({ id }: LikeProps) {
|
||||||
const isFavorite = useFavoriteStore(state => state.favorites.includes(id));
|
const isFavorite = useSoundStore(state => state.sounds[id].isFavorite);
|
||||||
const toggleFavorite = useFavoriteStore(state => state.toggleFavorite);
|
const toggleFavorite = useSoundStore(state => state.toggleFavorite);
|
||||||
|
|
||||||
const variants = fade();
|
const variants = fade();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
import { useSoundStore } from '@/store';
|
import { useSoundStore } from '@/store';
|
||||||
import { useFavoriteStore } from '@/store/favorite';
|
|
||||||
|
|
||||||
interface StoreConsumerProps {
|
interface StoreConsumerProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
|
|
@ -10,7 +9,6 @@ interface StoreConsumerProps {
|
||||||
export function StoreConsumer({ children }: StoreConsumerProps) {
|
export function StoreConsumer({ children }: StoreConsumerProps) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
useSoundStore.persist.rehydrate();
|
useSoundStore.persist.rehydrate();
|
||||||
useFavoriteStore.persist.rehydrate();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
|
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
import type { StateCreator } from 'zustand';
|
|
||||||
|
|
||||||
import type { FavoriteState } from './favorite.state';
|
|
||||||
|
|
||||||
export interface FavoriteActions {
|
|
||||||
toggleFavorite: (id: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createActions: StateCreator<
|
|
||||||
FavoriteActions & FavoriteState,
|
|
||||||
[],
|
|
||||||
[],
|
|
||||||
FavoriteActions
|
|
||||||
> = (set, get) => {
|
|
||||||
return {
|
|
||||||
toggleFavorite(id) {
|
|
||||||
if (get().favorites.includes(id)) {
|
|
||||||
set({ favorites: get().favorites.filter(_id => _id !== id) });
|
|
||||||
} else {
|
|
||||||
set({ favorites: [...get().favorites, id] });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
import type { StateCreator } from 'zustand';
|
|
||||||
|
|
||||||
import type { FavoriteActions } from './favorite.actions';
|
|
||||||
|
|
||||||
export interface FavoriteState {
|
|
||||||
favorites: Array<string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createState: StateCreator<
|
|
||||||
FavoriteState & FavoriteActions,
|
|
||||||
[],
|
|
||||||
[],
|
|
||||||
FavoriteState
|
|
||||||
> = () => ({ favorites: [] });
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
import { create } from 'zustand';
|
|
||||||
import { createJSONStorage, persist } from 'zustand/middleware';
|
|
||||||
|
|
||||||
import { type FavoriteState, createState } from './favorite.state';
|
|
||||||
import { type FavoriteActions, createActions } from './favorite.actions';
|
|
||||||
|
|
||||||
export const useFavoriteStore = create<FavoriteState & FavoriteActions>()(
|
|
||||||
persist(
|
|
||||||
(...a) => ({
|
|
||||||
...createState(...a),
|
|
||||||
...createActions(...a),
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
name: 'moodist-favorites',
|
|
||||||
skipHydration: true,
|
|
||||||
storage: createJSONStorage(() => localStorage),
|
|
||||||
version: 0,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
@ -8,6 +8,7 @@ export interface SoundActions {
|
||||||
setVolume: (id: string, volume: number) => void;
|
setVolume: (id: string, volume: number) => void;
|
||||||
unselectAll: (pushToHistory?: boolean) => void;
|
unselectAll: (pushToHistory?: boolean) => void;
|
||||||
restoreHistory: () => void;
|
restoreHistory: () => void;
|
||||||
|
toggleFavorite: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createActions: StateCreator<
|
export const createActions: StateCreator<
|
||||||
|
|
@ -44,6 +45,18 @@ export const createActions: StateCreator<
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
toggleFavorite(id) {
|
||||||
|
const sounds = get().sounds;
|
||||||
|
const sound = sounds[id];
|
||||||
|
|
||||||
|
set({
|
||||||
|
sounds: {
|
||||||
|
...sounds,
|
||||||
|
[id]: { ...sound, isFavorite: !sound.isFavorite },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
unselect(id) {
|
unselect(id) {
|
||||||
set({
|
set({
|
||||||
sounds: {
|
sounds: {
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,19 @@ export interface SoundState {
|
||||||
sounds: {
|
sounds: {
|
||||||
[id: string]: {
|
[id: string]: {
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
|
isFavorite: boolean;
|
||||||
volume: number;
|
volume: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
history: {
|
history: {
|
||||||
[id: string]: {
|
[id: string]: {
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
|
isFavorite: boolean;
|
||||||
volume: number;
|
volume: number;
|
||||||
};
|
};
|
||||||
} | null;
|
} | null;
|
||||||
noSelected: () => boolean;
|
noSelected: () => boolean;
|
||||||
|
getFavorites: () => Array<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createState: StateCreator<
|
export const createState: StateCreator<
|
||||||
|
|
@ -27,6 +30,13 @@ export const createState: StateCreator<
|
||||||
SoundState
|
SoundState
|
||||||
> = (set, get) => {
|
> = (set, get) => {
|
||||||
const state: SoundState = {
|
const state: SoundState = {
|
||||||
|
getFavorites() {
|
||||||
|
const { sounds } = get();
|
||||||
|
const ids = Object.keys(sounds);
|
||||||
|
const favorites = ids.filter(id => sounds[id].isFavorite);
|
||||||
|
|
||||||
|
return favorites;
|
||||||
|
},
|
||||||
history: null,
|
history: null,
|
||||||
noSelected() {
|
noSelected() {
|
||||||
const { sounds } = get();
|
const { sounds } = get();
|
||||||
|
|
@ -44,6 +54,7 @@ export const createState: StateCreator<
|
||||||
|
|
||||||
sounds.forEach(sound => {
|
sounds.forEach(sound => {
|
||||||
state.sounds[sound.id] = {
|
state.sounds[sound.id] = {
|
||||||
|
isFavorite: false,
|
||||||
isSelected: false,
|
isSelected: false,
|
||||||
volume: 0.5,
|
volume: 0.5,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue