Spaces:
Sleeping
Sleeping
Download frontend/src/lib/store/playerStore.ts from kishankrish19/Chat: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/frontend/src/lib/store/playerStore.ts
- Command line
-
hf download hf://spaces/kishankrish19/Chat/frontend/src/lib/store/playerStore.ts
-
curl -L -o playerStore.ts https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/frontend/src/lib/store/playerStore.ts
6.11 kB
| import { create } from "zustand"; | |
| import { persist } from "zustand/middleware"; | |
| import { api, Track } from "../api/apiClient"; | |
| export type { Track } from "../api/apiClient"; | |
| interface Player { | |
| requestId: number; | |
| currentTrack: Track | null; | |
| queue: Track[]; | |
| queueIndex: number; | |
| isPlaying: boolean; | |
| positionMs: number; | |
| durationMs: number; | |
| volume: number; | |
| isMuted: boolean; | |
| shuffle: boolean; | |
| repeatMode: "off" | "track" | "context"; | |
| isQueueOpen: boolean; | |
| autoplay: boolean; | |
| adventure: number; | |
| isAutoplay: boolean; | |
| notice: string; | |
| loading: boolean; | |
| start: (track: Track, list?: Track[], position?: number) => void; | |
| setIsPlaying: (v: boolean) => void; | |
| setPositionMs: (v: number) => void; | |
| setDurationMs: (v: number) => void; | |
| setVolume: (v: number) => void; | |
| toggleMute: () => void; | |
| toggleShuffle: () => void; | |
| toggleRepeat: () => void; | |
| toggleQueueOpen: () => void; | |
| addToQueue: (t: Track) => void; | |
| removeFromQueue: (i: number) => void; | |
| next: (ended?: boolean) => Promise<void>; | |
| previous: () => void; | |
| setPreferences: (p: { autoplay?: boolean; adventure?: number }) => void; | |
| setNotice: (s: string) => void; | |
| setLoading: (v: boolean) => void; | |
| clear: () => void; | |
| } | |
| export const usePlayerStore = create<Player>()( | |
| persist( | |
| (set, get) => ({ | |
| requestId: 0, | |
| currentTrack: null, | |
| queue: [], | |
| queueIndex: 0, | |
| isPlaying: false, | |
| positionMs: 0, | |
| durationMs: 0, | |
| volume: 0.75, | |
| isMuted: false, | |
| shuffle: false, | |
| repeatMode: "off", | |
| isQueueOpen: false, | |
| autoplay: true, | |
| adventure: 0.3, | |
| isAutoplay: false, | |
| notice: "", | |
| loading: false, | |
| start: (track, list = [track], position = 0) => | |
| set({ | |
| requestId: get().requestId + 1, | |
| currentTrack: track, | |
| queue: list.filter((t) => t.status === "ready"), | |
| queueIndex: Math.max( | |
| 0, | |
| list | |
| .filter((t) => t.status === "ready") | |
| .findIndex((t) => t.id === track.id), | |
| ), | |
| isPlaying: true, | |
| positionMs: position, | |
| durationMs: track.duration_ms, | |
| isAutoplay: false, | |
| notice: "", | |
| }), | |
| setIsPlaying: (isPlaying) => set({ isPlaying }), | |
| setPositionMs: (positionMs) => set({ positionMs }), | |
| setDurationMs: (durationMs) => set({ durationMs }), | |
| setVolume: (volume) => set({ volume, isMuted: false }), | |
| toggleMute: () => set((s) => ({ isMuted: !s.isMuted })), | |
| toggleShuffle: () => set((s) => ({ shuffle: !s.shuffle })), | |
| toggleRepeat: () => | |
| set((s) => ({ | |
| repeatMode: | |
| s.repeatMode === "off" | |
| ? "context" | |
| : s.repeatMode === "context" | |
| ? "track" | |
| : "off", | |
| })), | |
| toggleQueueOpen: () => set((s) => ({ isQueueOpen: !s.isQueueOpen })), | |
| addToQueue: (t) => set((s) => ({ queue: [...s.queue, t] })), | |
| removeFromQueue: (i) => | |
| set((s) => ({ | |
| queue: s.queue.filter((_, n) => n !== i), | |
| queueIndex: i < s.queueIndex ? s.queueIndex - 1 : s.queueIndex, | |
| })), | |
| next: async (ended = false) => { | |
| const s = get(); | |
| let index = s.queueIndex + 1; | |
| if (s.shuffle && s.queue.length > 1) { | |
| const options = s.queue | |
| .map((_, i) => i) | |
| .filter((i) => i !== s.queueIndex); | |
| index = options[Math.floor(Math.random() * options.length)]; | |
| } | |
| if (index >= s.queue.length && s.repeatMode === "context") index = 0; | |
| if (s.queue[index]) { | |
| set({ | |
| requestId: get().requestId + 1, | |
| currentTrack: s.queue[index], | |
| queueIndex: index, | |
| positionMs: 0, | |
| isPlaying: true, | |
| notice: "", | |
| isAutoplay: false, | |
| }); | |
| return; | |
| } | |
| if (s.autoplay && s.currentTrack) { | |
| try { | |
| const candidates = await api.next( | |
| s.currentTrack.id, | |
| s.adventure, | |
| s.queue.map((t) => t.id), | |
| ); | |
| if (get().currentTrack?.id !== s.currentTrack.id) return; | |
| if (candidates.length) { | |
| set({ | |
| requestId: get().requestId + 1, | |
| queue: [...s.queue, ...candidates], | |
| queueIndex: s.queue.length, | |
| currentTrack: candidates[0], | |
| positionMs: 0, | |
| isPlaying: true, | |
| isAutoplay: true, | |
| notice: "", | |
| }); | |
| return; | |
| } | |
| } catch { | |
| set({ | |
| notice: | |
| "Suggestions are unavailable. Choose another song from your collection.", | |
| }); | |
| } | |
| } | |
| set({ | |
| isPlaying: false, | |
| notice: ended | |
| ? "You reached the end. A little quiet, or another song?" | |
| : "No more songs in this queue.", | |
| }); | |
| }, | |
| previous: () => { | |
| const s = get(); | |
| const index = Math.max(0, s.queueIndex - 1); | |
| if (s.queue[index]) | |
| set({ | |
| requestId: get().requestId + 1, | |
| currentTrack: s.queue[index], | |
| queueIndex: index, | |
| positionMs: 0, | |
| isPlaying: true, | |
| isAutoplay: false, | |
| }); | |
| }, | |
| setPreferences: (p) => set(p), | |
| setNotice: (notice) => set({ notice }), | |
| setLoading: (loading) => set({ loading }), | |
| clear: () => | |
| set({ | |
| currentTrack: null, | |
| queue: [], | |
| queueIndex: 0, | |
| isPlaying: false, | |
| positionMs: 0, | |
| notice: "", | |
| }), | |
| }), | |
| { | |
| name: "stillwave-player-v1", | |
| partialize: (s) => ({ | |
| currentTrack: s.currentTrack, | |
| queue: s.queue, | |
| queueIndex: s.queueIndex, | |
| volume: s.volume, | |
| isMuted: s.isMuted, | |
| shuffle: s.shuffle, | |
| repeatMode: s.repeatMode, | |
| autoplay: s.autoplay, | |
| adventure: s.adventure, | |
| positionMs: s.positionMs, | |
| }), | |
| }, | |
| ), | |
| ); | |