Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

voiceConsent.ts

127 lines4.7 KB

1import { VoiceTranscriptionDestination } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { useSyncExternalStore } from "react";
4import { randomUUID } from "../lib/utils";
5import {
6 getLocalStorageItem,
7 readLocalStorageValue,
8 subscribeLocalStorageKey,
9 useLocalStorage,
10 writeLocalStorageValue,
11} from "../hooks/useLocalStorage";
12​
13// Separate keys: a stale window saving ordinary preferences must not restore either grant.
14const AUDIO_KEY = "t3code:voice-audio-grant:v1";
15const CLEANUP_KEY = "t3code:voice-cleanup-grant:v1";
16// "device" is Apple Speech in the macOS desktop app. Audio stays on this device, so it stays out of the shared contract.
17const AudioDestination = Schema.Union([
18 VoiceTranscriptionDestination,
19 Schema.Struct({ kind: Schema.Literal("device") }),
20]);
21export type AudioDestination = typeof AudioDestination.Type;
22const Grant = Schema.Struct({ id: Schema.String, destination: AudioDestination });
23const AudioGrant = Schema.NullOr(Grant);
24const sameGrant = Schema.toEquivalence(AudioGrant);
25const SavedChoice = Schema.Union([AudioGrant, Schema.Literal("off")]);
26const AudioChoice = Schema.Union([
27 SavedChoice,
28 Schema.Struct({ pending: Grant, previous: SavedChoice }),
29]);
30type AudioChoice = typeof AudioChoice.Type;
31const savedKey = (id: string) => `${AUDIO_KEY}:saved:${id}`;
32const pendingGrant = (choice: AudioChoice) =>
33 choice && choice !== "off" && "pending" in choice ? choice.pending : null;
34const CleanupGrant = Schema.NullOr(Schema.String);
35const decodeChoice = Schema.decodeSync(Schema.fromJsonString(AudioChoice));
36const readChoice = () => readLocalStorageValue(AUDIO_KEY, null, AudioChoice);
37​
38function readSaveMarker(id: string | undefined) {
39 try {
40 return id ? window.localStorage.getItem(savedKey(id)) : null;
41 } catch {
42 return undefined;
43 }
45​
46function activeChoice(choice: AudioChoice, marker: string | null | undefined) {
47 if (!choice || choice === "off" || !("pending" in choice)) return choice;
48 // Only a confirmed missing marker means the previous grant still applies.
49 if (marker === null) return choice.previous;
50 return marker === "true" ? choice.pending : ("off" as const);
52​
53export function useAudioChoice() {
54 const [choice] = useLocalStorage(AUDIO_KEY, null, AudioChoice);
55 const id = pendingGrant(choice)?.id;
56 const marker = useSyncExternalStore(
57 (notify) => (id ? subscribeLocalStorageKey(savedKey(id), notify) : () => {}),
58 () => readSaveMarker(id),
59 () => undefined,
60 );
61 return activeChoice(choice, marker);
63​
64function replaceChoice(choice: AudioChoice, previous = readChoice()) {
65 const old = pendingGrant(previous);
66 writeLocalStorageValue(AUDIO_KEY, choice, null, AudioChoice);
67 if (old && pendingGrant(getLocalStorageItem(AUDIO_KEY, AudioChoice))?.id !== old.id)
68 window.localStorage.removeItem(savedKey(old.id));
70​
71export function chooseAudioDestination(destination: AudioDestination | null) {
72 replaceChoice(destination ? { id: randomUUID(), destination } : "off");
74​
75export function pendingCloudAudioChoice(
76 destination: Extract<VoiceTranscriptionDestination, { kind: "cloud" }>,
77) {
78 const choice = readChoice();
79 const pending = { id: randomUUID(), destination };
80 replaceChoice(
81 { pending, previous: activeChoice(choice, readSaveMarker(pendingGrant(choice)?.id)) },
82 choice,
83 );
84 // A late response can acknowledge only its own choice, never write the destination another tab picked.
85 return () => {
86 if (pendingGrant(readChoice())?.id === pending.id)
87 writeLocalStorageValue(savedKey(pending.id), true, false, Schema.Boolean);
88 };
90​
91// Bind this recording to this act of consent, even if the user later reselects the same destination.
92export function bindAudioGrant(id: string) {
93 try {
94 let cachedRaw: string | null = null;
95 let choice: AudioChoice = null;
96 const current = () => {
97 const raw = window.localStorage.getItem(AUDIO_KEY);
98 if (raw !== cachedRaw) {
99 choice = raw ? decodeChoice(raw) : null;
100 cachedRaw = raw;
101 }
102 const active = activeChoice(choice, readSaveMarker(pendingGrant(choice)?.id));
103 return active === "off" ? null : active;
104 };
105 const bound = current();
106 if (bound?.id !== id) return () => false;
107 return () => {
108 try {
109 return sameGrant(current(), bound);
110 } catch {
111 return false;
112 }
113 };
114 } catch {
115 return () => false;
116 }
118​
119export function useCleanupGrant() {
120 return useLocalStorage(CLEANUP_KEY, null, CleanupGrant)[0];
122export function readCleanupGrant() {
123 return readLocalStorageValue(CLEANUP_KEY, null, CleanupGrant);
125export function chooseTranscriptCleanup(enabled: boolean) {
126 writeLocalStorageValue(CLEANUP_KEY, enabled ? randomUUID() : null, null, CleanupGrant);