mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-21 14:55:43 +00:00
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { create } from 'zustand';
|
|
import { persist } from 'zustand/middleware';
|
|
import { getPlaylists, createPlaylist as apiCreatePlaylist, SubsonicPlaylist } from '../api/subsonic';
|
|
|
|
interface PlaylistStore {
|
|
recentIds: string[];
|
|
playlists: SubsonicPlaylist[];
|
|
playlistsLoading: boolean;
|
|
touchPlaylist: (id: string) => void;
|
|
removeId: (id: string) => void;
|
|
fetchPlaylists: () => Promise<void>;
|
|
createPlaylist: (name: string, songIds?: string[]) => Promise<SubsonicPlaylist | null>;
|
|
addPlaylist: (playlist: SubsonicPlaylist) => void;
|
|
}
|
|
|
|
export const usePlaylistStore = create<PlaylistStore>()(
|
|
persist(
|
|
(set, get) => ({
|
|
recentIds: [],
|
|
playlists: [],
|
|
playlistsLoading: false,
|
|
touchPlaylist: (id) =>
|
|
set((s) => ({
|
|
recentIds: [id, ...s.recentIds.filter((x) => x !== id)].slice(0, 50),
|
|
})),
|
|
removeId: (id) =>
|
|
set((s) => ({ recentIds: s.recentIds.filter((x) => x !== id) })),
|
|
fetchPlaylists: async () => {
|
|
set({ playlistsLoading: true });
|
|
try {
|
|
const playlists = await getPlaylists();
|
|
set({ playlists, playlistsLoading: false });
|
|
} catch {
|
|
set({ playlistsLoading: false });
|
|
}
|
|
},
|
|
createPlaylist: async (name: string, songIds?: string[]) => {
|
|
try {
|
|
const playlist = await apiCreatePlaylist(name, songIds);
|
|
set((s) => ({
|
|
playlists: [...s.playlists, playlist],
|
|
recentIds: [playlist.id, ...s.recentIds.filter((x) => x !== playlist.id)].slice(0, 50),
|
|
}));
|
|
return playlist;
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
addPlaylist: (playlist) => {
|
|
set((s) => ({
|
|
playlists: [...s.playlists, playlist],
|
|
}));
|
|
},
|
|
}),
|
|
{ name: 'psysonic_playlists_recent' }
|
|
)
|
|
);
|