* wip * wip * wip * format * wip(mobile): lint and format * cleanup * nits * nits * idiomatic react * nit * format all root files
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { create } from 'zustand';
|
|
|
|
/**
|
|
* Single source of truth for "is stream playback paused." Each component that
|
|
* wants to pause playback registers a unique id via `useSuspendPlayback`; the
|
|
* label is for devtools only. Playback is paused while any id is registered.
|
|
*/
|
|
interface PlaybackPauseState {
|
|
activeIds: Record<string, string>;
|
|
composing: boolean;
|
|
add: (id: string, label: string) => void;
|
|
remove: (id: string) => void;
|
|
setComposing: (composing: boolean) => void;
|
|
}
|
|
|
|
export const usePlaybackPauseStore = create<PlaybackPauseState>((set) => ({
|
|
activeIds: {},
|
|
composing: false,
|
|
add: (id, label) =>
|
|
set((s) => ({ activeIds: { ...s.activeIds, [id]: label } })),
|
|
remove: (id) =>
|
|
set((s) => {
|
|
if (!(id in s.activeIds)) return s;
|
|
const { [id]: _, ...rest } = s.activeIds;
|
|
return { activeIds: rest };
|
|
}),
|
|
setComposing: (composing) => set({ composing }),
|
|
}));
|
|
|
|
export const selectIsPaused = (s: PlaybackPauseState) =>
|
|
Object.keys(s.activeIds).length > 0 || s.composing;
|
|
|
|
export const selectIsComposing = (s: PlaybackPauseState) => s.composing;
|