summaryrefslogtreecommitdiff
path: root/apps/web/src/voiceover/voiceover-store.ts
blob: e4024c1e9aec22b2b6a4916db97bc5f209c8344e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Voice-over recording UI state.
 * Transient by nature — a recording never survives a reload, so unlike most
 * stores here this one is intentionally NOT persisted (same pattern as
 * editor-store / properties-store).
 */

import { create } from "zustand";
import { persist } from "zustand/middleware";
import type { MediaTime } from "@/wasm";
import {
	DEFAULT_VOICE_OVER_ENHANCEMENTS,
	type VoiceOverEnhancementSettings,
} from "@/voiceover/enhancements";

export type VoiceOverStatus = "idle" | "recording" | "processing";

interface VoiceOverStore {
	status: VoiceOverStatus;
	startedAtMs: number | null;
	playheadStartTime: MediaTime | null;
	startRecording: ({ playheadTime }: { playheadTime: MediaTime }) => void;
	processRecording: () => void;
	stopRecording: () => void;
}

interface VoiceOverPreferencesStore {
	selectedDeviceId: string | null;
	enhancements: VoiceOverEnhancementSettings;
	setSelectedDeviceId: ({ deviceId }: { deviceId: string | null }) => void;
	setEnhancementEnabled: ({
		id,
		enabled,
	}: {
		id: keyof VoiceOverEnhancementSettings;
		enabled: boolean;
	}) => void;
}

export const useVoiceOverStore = create<VoiceOverStore>()((set) => ({
	status: "idle",
	startedAtMs: null,
	playheadStartTime: null,

	startRecording: ({ playheadTime }) => {
		set({
			status: "recording",
			startedAtMs: Date.now(),
			playheadStartTime: playheadTime,
		});
	},

	processRecording: () => {
		set({
			status: "processing",
			startedAtMs: null,
		});
	},

	stopRecording: () => {
		set({
			status: "idle",
			startedAtMs: null,
			playheadStartTime: null,
		});
	},
}));

export const useVoiceOverPreferencesStore = create<VoiceOverPreferencesStore>()(
	persist(
		(set) => ({
			selectedDeviceId: null,
			enhancements: DEFAULT_VOICE_OVER_ENHANCEMENTS,

			setSelectedDeviceId: ({ deviceId }) => {
				set({ selectedDeviceId: deviceId });
			},

			setEnhancementEnabled: ({ id, enabled }) => {
				set((state) => ({
					enhancements: {
						...state.enhancements,
						[id]: enabled,
					},
				}));
			},
		}),
		{
			name: "voiceover-preferences-store",
			partialize: (state) => ({
				selectedDeviceId: state.selectedDeviceId,
				enhancements: state.enhancements,
			}),
		},
	),
);