2025-10-17 16:12:02 +01:00
|
|
|
import { useEffect, useMemo, useReducer, useRef, useState, type ReactNode } from "react";
|
2025-09-30 14:51:37 +01:00
|
|
|
import { SoundContext } from "../SoundContext";
|
2025-10-01 15:21:07 +01:00
|
|
|
import { initialState, reducer } from "../reducers/SoundContextReducer";
|
2025-10-08 11:08:41 +01:00
|
|
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
2025-09-30 14:51:37 +01:00
|
|
|
|
|
|
|
|
type SoundContextProviderProps = {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
|
2025-10-15 11:00:52 +01:00
|
|
|
const audioReady = useRef(false);
|
|
|
|
|
const [audioArmed, setAudioArmed] = useState(false);
|
|
|
|
|
const audioCtxRef = useRef<AudioContext | null>(null);
|
2025-10-01 15:21:07 +01:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
2025-10-08 11:08:41 +01:00
|
|
|
const { mutation } = useCameraBlackboard();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchSound = async () => {
|
|
|
|
|
const result = await mutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "soundSettings",
|
|
|
|
|
});
|
2025-10-20 10:59:38 +01:00
|
|
|
|
2025-10-08 11:08:41 +01:00
|
|
|
dispatch({ type: "UPDATE", payload: result.result });
|
|
|
|
|
};
|
|
|
|
|
fetchSound();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
2025-10-15 11:00:52 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const unlock = async () => {
|
|
|
|
|
if (!audioCtxRef.current) audioCtxRef.current = new AudioContext();
|
|
|
|
|
if (audioCtxRef.current.state !== "running") {
|
|
|
|
|
try {
|
|
|
|
|
await audioCtxRef.current.resume();
|
|
|
|
|
} catch {
|
|
|
|
|
/* empty */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const armed = audioCtxRef.current.state === "running";
|
|
|
|
|
audioReady.current = audioCtxRef.current.state === "running";
|
|
|
|
|
setAudioArmed(armed);
|
|
|
|
|
if (audioReady.current) {
|
|
|
|
|
window.removeEventListener("pointerdown", unlock);
|
|
|
|
|
window.removeEventListener("keydown", unlock);
|
|
|
|
|
window.removeEventListener("touchstart", unlock);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener("pointerdown", unlock, { once: false });
|
|
|
|
|
window.addEventListener("keydown", unlock, { once: false });
|
|
|
|
|
window.addEventListener("touchstart", unlock, { once: false });
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("pointerdown", unlock);
|
|
|
|
|
window.removeEventListener("keydown", unlock);
|
|
|
|
|
window.removeEventListener("touchstart", unlock);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-10-17 16:12:02 +01:00
|
|
|
const value = useMemo(() => ({ state, dispatch, audioArmed }), [state, audioArmed]);
|
|
|
|
|
return <SoundContext.Provider value={value}>{children}</SoundContext.Provider>;
|
2025-09-30 14:51:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SoundContextProvider;
|