mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 12:30:12 +02:00
rework player store and add global player context
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
import { createContext, useCallback, useMemo } from 'react';
|
||||
import { Song } from 'src/main/features/core/lyrics/netease';
|
||||
|
||||
import { AddToQueueType } from '/@/renderer/store';
|
||||
import { LibraryItem, QueueSong } from '/@/shared/types/domain-types';
|
||||
|
||||
interface PlayerContext {
|
||||
addToQueueByData: (data: Song[], type: AddToQueueType) => void;
|
||||
addToQueueByFetch: (id: string[], itemType: LibraryItem, type: AddToQueueType) => void;
|
||||
clearQueue: () => void;
|
||||
clearSelected: (items: QueueSong[]) => void;
|
||||
decreaseVolume: (amount: number) => void;
|
||||
increaseVolume: (amount: number) => void;
|
||||
mediaNext: () => void;
|
||||
mediaPause: () => void;
|
||||
mediaPlay: (id: string) => void;
|
||||
mediaPrevious: () => void;
|
||||
mediaSeekToTimestamp: (timestamp: number) => void;
|
||||
mediaStepBackward: () => void;
|
||||
mediaStepForward: () => void;
|
||||
mediaToggleMute: () => void;
|
||||
mediaTogglePlayPause: () => void;
|
||||
moveSelectedTo: (items: QueueSong[], edge: 'bottom' | 'top', uniqueId: string) => void;
|
||||
moveSelectedToBottom: (items: QueueSong[]) => void;
|
||||
moveSelectedToNext: (items: QueueSong[]) => void;
|
||||
moveSelectedToTop: (items: QueueSong[]) => void;
|
||||
setVolume: (volume: number) => void;
|
||||
shuffle: () => void;
|
||||
shuffleAll: () => void;
|
||||
shuffleSelected: (items: QueueSong[]) => void;
|
||||
}
|
||||
|
||||
export const PlayerContext = createContext<PlayerContext>({
|
||||
addToQueueByData: () => {},
|
||||
addToQueueByFetch: () => {},
|
||||
clearQueue: () => {},
|
||||
clearSelected: () => {},
|
||||
decreaseVolume: () => {},
|
||||
increaseVolume: () => {},
|
||||
mediaNext: () => {},
|
||||
mediaPause: () => {},
|
||||
mediaPlay: () => {},
|
||||
mediaPrevious: () => {},
|
||||
mediaSeekToTimestamp: () => {},
|
||||
mediaStepBackward: () => {},
|
||||
mediaStepForward: () => {},
|
||||
mediaToggleMute: () => {},
|
||||
mediaTogglePlayPause: () => {},
|
||||
moveSelectedTo: () => {},
|
||||
moveSelectedToBottom: () => {},
|
||||
moveSelectedToNext: () => {},
|
||||
moveSelectedToTop: () => {},
|
||||
setVolume: () => {},
|
||||
shuffle: () => {},
|
||||
shuffleAll: () => {},
|
||||
shuffleSelected: () => {},
|
||||
});
|
||||
|
||||
export const PlayerProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const addToQueueByData = useCallback((data: Song[], type: AddToQueueType) => {}, []);
|
||||
|
||||
const addToQueueByFetch = useCallback(
|
||||
(id: string[], itemType: LibraryItem, type: AddToQueueType) => {},
|
||||
[],
|
||||
);
|
||||
const clearQueue = useCallback(() => {}, []);
|
||||
|
||||
const clearSelected = useCallback((items: QueueSong[]) => {}, []);
|
||||
|
||||
const decreaseVolume = useCallback((amount: number) => {}, []);
|
||||
|
||||
const increaseVolume = useCallback((amount: number) => {}, []);
|
||||
|
||||
const mediaNext = useCallback(() => {}, []);
|
||||
|
||||
const mediaPause = useCallback(() => {}, []);
|
||||
|
||||
const mediaPlay = useCallback((id: string) => {}, []);
|
||||
|
||||
const mediaPrevious = useCallback(() => {}, []);
|
||||
|
||||
const mediaSeekToTimestamp = useCallback((timestamp: number) => {}, []);
|
||||
|
||||
const mediaStepBackward = useCallback(() => {}, []);
|
||||
|
||||
const mediaStepForward = useCallback(() => {}, []);
|
||||
|
||||
const mediaToggleMute = useCallback(() => {}, []);
|
||||
|
||||
const mediaTogglePlayPause = useCallback(() => {}, []);
|
||||
|
||||
const moveSelectedTo = useCallback(
|
||||
(items: QueueSong[], edge: 'bottom' | 'top', uniqueId: string) => {},
|
||||
[],
|
||||
);
|
||||
|
||||
const moveSelectedToBottom = useCallback((items: QueueSong[]) => {}, []);
|
||||
|
||||
const moveSelectedToNext = useCallback((items: QueueSong[]) => {}, []);
|
||||
|
||||
const moveSelectedToTop = useCallback((items: QueueSong[]) => {}, []);
|
||||
|
||||
const setVolume = useCallback((volume: number) => {}, []);
|
||||
|
||||
const shuffle = useCallback(() => {}, []);
|
||||
|
||||
const shuffleAll = useCallback(() => {}, []);
|
||||
|
||||
const shuffleSelected = useCallback((items: QueueSong[]) => {}, []);
|
||||
|
||||
const contextValue: PlayerContext = useMemo(
|
||||
() => ({
|
||||
addToQueueByData,
|
||||
addToQueueByFetch,
|
||||
clearQueue,
|
||||
clearSelected,
|
||||
decreaseVolume,
|
||||
increaseVolume,
|
||||
mediaNext,
|
||||
mediaPause,
|
||||
mediaPlay,
|
||||
mediaPrevious,
|
||||
mediaSeekToTimestamp,
|
||||
mediaStepBackward,
|
||||
mediaStepForward,
|
||||
mediaToggleMute,
|
||||
mediaTogglePlayPause,
|
||||
moveSelectedTo,
|
||||
moveSelectedToBottom,
|
||||
moveSelectedToNext,
|
||||
moveSelectedToTop,
|
||||
setVolume,
|
||||
shuffle,
|
||||
shuffleAll,
|
||||
shuffleSelected,
|
||||
}),
|
||||
[
|
||||
addToQueueByData,
|
||||
addToQueueByFetch,
|
||||
clearQueue,
|
||||
clearSelected,
|
||||
decreaseVolume,
|
||||
increaseVolume,
|
||||
mediaNext,
|
||||
mediaPause,
|
||||
mediaPlay,
|
||||
mediaPrevious,
|
||||
mediaSeekToTimestamp,
|
||||
mediaStepBackward,
|
||||
mediaStepForward,
|
||||
mediaToggleMute,
|
||||
mediaTogglePlayPause,
|
||||
moveSelectedTo,
|
||||
moveSelectedToBottom,
|
||||
moveSelectedToNext,
|
||||
moveSelectedToTop,
|
||||
setVolume,
|
||||
shuffle,
|
||||
shuffleAll,
|
||||
shuffleSelected,
|
||||
],
|
||||
);
|
||||
|
||||
return <PlayerContext.Provider value={contextValue}>{children}</PlayerContext.Provider>;
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { StoreApi, UseBoundStore } from 'zustand';
|
||||
|
||||
type WithSelectors<S> = S extends { getState: () => infer T }
|
||||
? S & { use: { [K in keyof T]: () => T[K] } }
|
||||
: never;
|
||||
|
||||
export const createSelectors = <S extends UseBoundStore<StoreApi<object>>>(_store: S) => {
|
||||
const store = _store as WithSelectors<typeof _store>;
|
||||
store.use = {};
|
||||
for (const k of Object.keys(store.getState())) {
|
||||
(store.use as any)[k] = () => store((s) => s[k as keyof typeof s]);
|
||||
}
|
||||
|
||||
return store;
|
||||
};
|
||||
+1282
-1089
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
||||
export function shuffle<T>(array: T[]): T[] {
|
||||
// Create a copy of the array to avoid mutating the original
|
||||
const shuffled = [...array];
|
||||
|
||||
// Loop through the array from the last element to the first
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
// Generate a random index from 0 to i
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
// Swap elements at positions i and j
|
||||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
||||
}
|
||||
|
||||
return shuffled;
|
||||
}
|
||||
|
||||
export function shuffleInPlace<T>(array: T[]): T[] {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
Reference in New Issue
Block a user