summaryrefslogtreecommitdiff
path: root/apps/web/src/stores/playback-store.ts
blob: 3ef1d95c5345559f92f4605971f782f88a951ffe (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { create } from "zustand";
import type { PlaybackState, PlaybackControls } from "@/types/playback";

interface PlaybackStore extends PlaybackState, PlaybackControls {
	setDuration: (duration: number) => void;
	setCurrentTime: (time: number) => void;
}

let playbackTimer: number | null = null;

const startTimer = (store: () => PlaybackStore) => {
	if (playbackTimer) cancelAnimationFrame(playbackTimer);

	// Use requestAnimationFrame for smoother updates
	const updateTime = () => {
		const state = store();
		if (state.isPlaying && state.currentTime < state.duration) {
			const now = performance.now();
			const delta = (now - lastUpdate) / 1000; // Convert to seconds
			lastUpdate = now;

			const newTime = state.currentTime + delta * state.speed;
			if (newTime >= state.duration) {
				// When video completes, pause and reset playhead to start
				state.pause();
				state.setCurrentTime(0);
				// Notify video elements to sync with reset
				window.dispatchEvent(
					new CustomEvent("playback-seek", { detail: { time: 0 } }),
				);
			} else {
				state.setCurrentTime(newTime);
				// Notify video elements to sync
				window.dispatchEvent(
					new CustomEvent("playback-update", { detail: { time: newTime } }),
				);
			}
		}
		playbackTimer = requestAnimationFrame(updateTime);
	};

	let lastUpdate = performance.now();
	playbackTimer = requestAnimationFrame(updateTime);
};

const stopTimer = () => {
	if (playbackTimer) {
		cancelAnimationFrame(playbackTimer);
		playbackTimer = null;
	}
};

export const usePlaybackStore = create<PlaybackStore>((set, get) => ({
	isPlaying: false,
	currentTime: 0,
	duration: 0,
	volume: 1,
	muted: false,
	previousVolume: 1,
	speed: 1.0,

	play: () => {
		set({ isPlaying: true });
		startTimer(get);
	},

	pause: () => {
		set({ isPlaying: false });
		stopTimer();
	},

	toggle: () => {
		const { isPlaying } = get();
		if (isPlaying) {
			get().pause();
		} else {
			get().play();
		}
	},

	seek: (time: number) => {
		const { duration } = get();
		const clampedTime = Math.max(0, Math.min(duration, time));
		set({ currentTime: clampedTime });

		const event = new CustomEvent("playback-seek", {
			detail: { time: clampedTime },
		});
		window.dispatchEvent(event);
	},

	setVolume: (volume: number) =>
		set((state) => ({
			volume: Math.max(0, Math.min(1, volume)),
			muted: volume === 0,
			previousVolume: volume > 0 ? volume : state.previousVolume,
		})),

	setSpeed: (speed: number) => {
		const newSpeed = Math.max(0.1, Math.min(2.0, speed));
		set({ speed: newSpeed });

		const event = new CustomEvent("playback-speed", {
			detail: { speed: newSpeed },
		});
		window.dispatchEvent(event);
	},

	setDuration: (duration: number) => set({ duration }),
	setCurrentTime: (time: number) => set({ currentTime: time }),

	mute: () => {
		const { volume, previousVolume } = get();
		set({
			muted: true,
			previousVolume: volume > 0 ? volume : previousVolume,
			volume: 0,
		});
	},

	unmute: () => {
		const { previousVolume } = get();
		set({ muted: false, volume: previousVolume ?? 1 });
	},

	toggleMute: () => {
		const { muted } = get();
		if (muted) {
			get().unmute();
		} else {
			get().mute();
		}
	},
}));