feat: added new music player and fix some UI issues
This commit is contained in:
26 files changed
+1862
-1491
No files matched your search
@@ -19,7 +19,7 @@ type AudioPlaybackStore = {
|
||||
player: {
|
||||
currentTick: number;
|
||||
playing: boolean;
|
||||
ref: React.RefObject<HTMLAudioElement> | null;
|
||||
ref: React.RefObject<HTMLAudioElement | null> | null;
|
||||
volume: number;
|
||||
isMuted: boolean;
|
||||
};
|
||||
@@ -108,29 +108,28 @@ export const generateAudioStreamUrl = (
|
||||
return `${basePath}/Audio/${itemId}/universal?${urlParams}`;
|
||||
};
|
||||
|
||||
export const setAudioRef = (ref: React.RefObject<HTMLAudioElement>) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.ref = ref;
|
||||
useAudioPlayback.setState(state);
|
||||
// Player updates must replace `player` (not mutate it) so zustand notifies subscribers.
|
||||
const updatePlayer = (patch: Partial<AudioPlaybackStore["player"]>) => {
|
||||
useAudioPlayback.setState((state) => ({
|
||||
player: { ...state.player, ...patch },
|
||||
}));
|
||||
};
|
||||
|
||||
export const setAudioRef = (ref: React.RefObject<HTMLAudioElement | null>) => {
|
||||
updatePlayer({ ref });
|
||||
};
|
||||
|
||||
export const setProgress = (ticks: number) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.currentTick = ticks;
|
||||
useAudioPlayback.setState(state);
|
||||
updatePlayer({ currentTick: ticks });
|
||||
};
|
||||
|
||||
export const setVolume = (volume: number) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.volume = volume;
|
||||
useAudioPlayback.setState(state);
|
||||
updatePlayer({ volume });
|
||||
setPlayerVolume("audio", volume);
|
||||
};
|
||||
|
||||
export const setIsMuted = (isMuted: boolean) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.isMuted = isMuted;
|
||||
useAudioPlayback.setState(state);
|
||||
updatePlayer({ isMuted });
|
||||
setPlayerMuted("audio", isMuted);
|
||||
};
|
||||
|
||||
@@ -149,4 +148,4 @@ export const stopPlayback = () => {
|
||||
isMuted: current.player.isMuted,
|
||||
},
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -8,12 +8,16 @@ interface QueueStore {
|
||||
* This index number should be equal to index of the item in tracks array to allow seamless playback of items having broken IndexNumber field.
|
||||
*/
|
||||
currentItemIndex: number;
|
||||
repeatMode: RepeatMode;
|
||||
}
|
||||
|
||||
export type RepeatMode = "off" | "all" | "one";
|
||||
|
||||
const useQueue = createWithEqualityFn<QueueStore>(
|
||||
() => ({
|
||||
tracks: [],
|
||||
currentItemIndex: 0,
|
||||
repeatMode: "off",
|
||||
}),
|
||||
shallow,
|
||||
);
|
||||
@@ -22,7 +26,7 @@ export const setQueue = (
|
||||
tracks: BaseItemDto[] | undefined,
|
||||
currentItemIndex: number,
|
||||
) => {
|
||||
useQueue.setState(() => ({ tracks, currentItemIndex }));
|
||||
useQueue.setState({ tracks, currentItemIndex });
|
||||
};
|
||||
|
||||
export const setTrackIndex = (index: number) => {
|
||||
@@ -54,6 +58,20 @@ export const removeFromQueue = (index: number) => {
|
||||
});
|
||||
};
|
||||
|
||||
/** Inserts a track at `index`, keeping the current track pointer on the same item. */
|
||||
export const insertIntoQueue = (track: BaseItemDto, index: number) => {
|
||||
useQueue.setState((state) => {
|
||||
const tracks = [...(state.tracks ?? [])];
|
||||
const at = Math.min(Math.max(index, 0), tracks.length);
|
||||
tracks.splice(at, 0, track);
|
||||
const currentItemIndex =
|
||||
at <= state.currentItemIndex && tracks.length > 1
|
||||
? state.currentItemIndex + 1
|
||||
: state.currentItemIndex;
|
||||
return { ...state, tracks, currentItemIndex };
|
||||
});
|
||||
};
|
||||
|
||||
export const clearUpcoming = () => {
|
||||
useQueue.setState((state) => {
|
||||
if (!state.tracks) return state;
|
||||
@@ -84,6 +102,31 @@ export const shuffleUpcoming = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const nextRepeatMode: Record<RepeatMode, RepeatMode> = {
|
||||
off: "all",
|
||||
all: "one",
|
||||
one: "off",
|
||||
};
|
||||
|
||||
/** Cycles repeat mode: off → all → one → off */
|
||||
export const cycleRepeatMode = () => {
|
||||
useQueue.setState((state) => ({
|
||||
...state,
|
||||
repeatMode: nextRepeatMode[state.repeatMode],
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Index to advance to after the current track, honouring "repeat all".
|
||||
* Returns undefined at the end of the queue when not repeating.
|
||||
*/
|
||||
export const getNextTrackIndex = () => {
|
||||
const { tracks, currentItemIndex, repeatMode } = useQueue.getState();
|
||||
if (!tracks?.length) return undefined;
|
||||
if (currentItemIndex + 1 < tracks.length) return currentItemIndex + 1;
|
||||
return repeatMode === "all" ? 0 : undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets the Queue store
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user