mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:20:17 +08:00
Always strip dollar-sign math delimiters so the voice model never says dollar. When Math speak is on (default), also convert fractions, powers, Greek, and common operators into spoken words. Settings > Voice exposes the toggle; the server applies it on every /tts call.
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import { apiFetch, apiUrl } from "@/lib/api";
|
|
|
|
const GLOBAL_EVENT = "deeptutor:voice-math-speak";
|
|
|
|
let cached: boolean | null = null;
|
|
let inflight: Promise<boolean> | null = null;
|
|
|
|
function fetchPreference(): Promise<boolean> {
|
|
if (cached !== null) return Promise.resolve(cached);
|
|
if (!inflight) {
|
|
inflight = apiFetch(apiUrl("/api/settings"))
|
|
.then((r) => (r.ok ? r.json() : null))
|
|
.then((payload) => {
|
|
const next = payload?.ui?.voice_math_speak !== false;
|
|
cached = next;
|
|
return next;
|
|
})
|
|
.catch(() => {
|
|
cached = true;
|
|
return true;
|
|
})
|
|
.finally(() => {
|
|
inflight = null;
|
|
});
|
|
}
|
|
return inflight;
|
|
}
|
|
|
|
/**
|
|
* Settings-page hook: read/write whether TTS verbalizes LaTeX as spoken math.
|
|
* Default is on. The server applies this on every /tts call.
|
|
*/
|
|
export function useVoiceMathSpeakPreference() {
|
|
const [value, setVal] = useState<boolean>(cached ?? true);
|
|
const [loading, setLoading] = useState<boolean>(cached === null);
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
fetchPreference().then((v) => {
|
|
if (active) {
|
|
setVal(v);
|
|
setLoading(false);
|
|
}
|
|
});
|
|
const onGlobal = (e: Event) =>
|
|
setVal(Boolean((e as CustomEvent).detail?.value));
|
|
window.addEventListener(GLOBAL_EVENT, onGlobal);
|
|
return () => {
|
|
active = false;
|
|
window.removeEventListener(GLOBAL_EVENT, onGlobal);
|
|
};
|
|
}, []);
|
|
|
|
const setValue = useCallback(async (next: boolean) => {
|
|
setVal(next);
|
|
cached = next;
|
|
if (typeof window !== "undefined") {
|
|
window.dispatchEvent(
|
|
new CustomEvent(GLOBAL_EVENT, { detail: { value: next } }),
|
|
);
|
|
}
|
|
await apiFetch(apiUrl("/api/settings/voice-math-speak"), {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ voice_math_speak: next }),
|
|
});
|
|
}, []);
|
|
|
|
return { value, setValue, loading };
|
|
}
|