73 lines
1.5 KiB
TypeScript
73 lines
1.5 KiB
TypeScript
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
export interface PlayerState {
|
|
currentTrackId: string | null;
|
|
isPlaying: boolean;
|
|
position: number;
|
|
duration: number;
|
|
volume: number;
|
|
muted: boolean;
|
|
isQueueOpen: boolean;
|
|
}
|
|
|
|
export const playerInitialState: PlayerState = {
|
|
currentTrackId: null,
|
|
isPlaying: false,
|
|
position: 0,
|
|
duration: 0,
|
|
volume: 0.78,
|
|
muted: false,
|
|
isQueueOpen: false,
|
|
};
|
|
|
|
export const playerSlice = createSlice({
|
|
name: 'player',
|
|
initialState: playerInitialState,
|
|
reducers: {
|
|
play(state, action: PayloadAction<string>) {
|
|
state.currentTrackId = action.payload;
|
|
state.isPlaying = true;
|
|
state.position = 0;
|
|
},
|
|
pause(state) {
|
|
state.isPlaying = false;
|
|
},
|
|
resume(state) {
|
|
state.isPlaying = true;
|
|
},
|
|
stop(state) {
|
|
state.isPlaying = false;
|
|
state.currentTrackId = null;
|
|
state.position = 0;
|
|
},
|
|
setPosition(state, action: PayloadAction<number>) {
|
|
state.position = action.payload;
|
|
},
|
|
setDuration(state, action: PayloadAction<number>) {
|
|
state.duration = action.payload;
|
|
},
|
|
setVolume(state, action: PayloadAction<number>) {
|
|
state.volume = action.payload;
|
|
},
|
|
toggleMute(state) {
|
|
state.muted = !state.muted;
|
|
},
|
|
toggleQueue(state) {
|
|
state.isQueueOpen = !state.isQueueOpen;
|
|
},
|
|
},
|
|
});
|
|
|
|
export const {
|
|
play,
|
|
pause,
|
|
resume,
|
|
stop,
|
|
setPosition,
|
|
setDuration,
|
|
setVolume,
|
|
toggleMute,
|
|
toggleQueue,
|
|
} = playerSlice.actions;
|
|
export default playerSlice.reducer;
|