@vibes.diy/base 14.3.451 → 14.3.453
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ImgGen.js +9 -14
- package/components/dictation/dictation.d.ts +73 -0
- package/components/dictation/dictation.js +466 -0
- package/components/index.d.ts +1 -0
- package/components/index.js +1 -0
- package/components/vibe-menu/cards/ShareCard.js +8 -36
- package/components/vibe-menu/cards/share-card.styles.d.ts +1 -1
- package/components/vibe-menu/cards/share-card.styles.js +1 -8
- package/components/vibe-menu/copy.d.ts +2 -8
- package/components/vibe-menu/copy.js +1 -5
- package/components/vibe-menu/parts/Composer.d.ts +1 -0
- package/components/vibe-menu/parts/Composer.js +31 -2
- package/components/vibe-menu/parts/use-composer-draft.js +1 -0
- package/components/vibe-menu/slices.d.ts +0 -2
- package/components/vibe-menu/vibe-menu.styles.d.ts +1 -1
- package/components/vibe-menu/vibe-menu.styles.js +66 -0
- package/hooks/img-gen/use-img-gen.js +27 -1
- package/package.json +5 -5
package/components/ImgGen.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { useState, useCallback, useEffect, useMemo } from "react";
|
|
3
3
|
import { getRegisteredVibeApi } from "@vibes.diy/vibe-runtime";
|
|
4
|
-
import { IMG_GEN_PAUSED_MESSAGE, IMG_GEN_WAITING_MESSAGE, isCaptureRefused } from "@vibes.diy/vibe-types";
|
|
4
|
+
import { IMG_GEN_LOST_MESSAGE, IMG_GEN_PAUSED_MESSAGE, IMG_GEN_WAITING_MESSAGE, isCaptureRefused } from "@vibes.diy/vibe-types";
|
|
5
5
|
import { useImgGen } from "../hooks/img-gen/use-img-gen.js";
|
|
6
6
|
import { usePictureSrc } from "../hooks/img-gen/use-picture-src.js";
|
|
7
7
|
import { registeredPictureStore } from "../hooks/img-gen/local-pictures.js";
|
|
@@ -92,8 +92,7 @@ export function ImgGen({ prompt, _id: propId, images, database, className, alt,
|
|
|
92
92
|
}
|
|
93
93
|
if (paused !== undefined && !hasExistingImage) {
|
|
94
94
|
return (React.createElement("div", { className: className, "data-imggen-paused": true, style: { padding: 20, textAlign: "center", color: "#888" } },
|
|
95
|
-
React.createElement("div", null, IMG_GEN_PAUSED_MESSAGE),
|
|
96
|
-
React.createElement("div", { style: { fontSize: 14, marginTop: 8 } }, prompt),
|
|
95
|
+
React.createElement("div", null, paused.lost === true ? IMG_GEN_LOST_MESSAGE : IMG_GEN_PAUSED_MESSAGE),
|
|
97
96
|
showControls && inCapture === false && (React.createElement("div", { style: { marginTop: 12 } },
|
|
98
97
|
React.createElement("button", { type: "button", onClick: handleTapGeneration, disabled: loading, style: btnStyle, title: "Regenerate" }, "\u21BB")))));
|
|
99
98
|
}
|
|
@@ -107,18 +106,14 @@ export function ImgGen({ prompt, _id: propId, images, database, className, alt,
|
|
|
107
106
|
}
|
|
108
107
|
if (!hasExistingImage) {
|
|
109
108
|
if (needsTap === true) {
|
|
110
|
-
return (React.createElement("div", { className: className, style: { padding: 20, textAlign: "center", color: "#888" } },
|
|
111
|
-
playgroundButton,
|
|
112
|
-
React.createElement("div", { style: { fontSize: 14, marginTop: 8 } }, prompt)));
|
|
109
|
+
return (React.createElement("div", { className: className, style: { padding: 20, textAlign: "center", color: "#888" } }, playgroundButton));
|
|
113
110
|
}
|
|
114
|
-
return (React.createElement("div", { className: className, style: { padding: 20, textAlign: "center", color: "#888" } },
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
: "Generating image...")),
|
|
121
|
-
React.createElement("div", { style: { fontSize: 14, marginTop: 8 } }, prompt)));
|
|
111
|
+
return (React.createElement("div", { className: className, style: { padding: 20, textAlign: "center", color: "#888" } }, photoGoingIn && picture.unavailable === false ? (React.createElement(PhotoSteps, { safetyPassed: safetyPassed })) : (React.createElement("div", null, pending === true || picture.unavailable
|
|
112
|
+
? "Loading image..."
|
|
113
|
+
:
|
|
114
|
+
waiting === true
|
|
115
|
+
? IMG_GEN_WAITING_MESSAGE
|
|
116
|
+
: "Generating image..."))));
|
|
122
117
|
}
|
|
123
118
|
return (React.createElement("div", { style: { position: "relative", display: "inline-block" } },
|
|
124
119
|
React.createElement("img", { src: picture.src, onError: onImageError, alt: alt ?? GENERATED_IMAGE_NAME, className: className, style: style ?? { maxWidth: "100%", height: "auto" } }),
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export type DictationSurface = "home" | "change";
|
|
3
|
+
export interface DictationFailure {
|
|
4
|
+
readonly kind: "sign-in" | "credits" | "other";
|
|
5
|
+
readonly message?: string;
|
|
6
|
+
}
|
|
7
|
+
export type DictationAnswer = {
|
|
8
|
+
readonly ok: true;
|
|
9
|
+
readonly text: string;
|
|
10
|
+
} | {
|
|
11
|
+
readonly ok: false;
|
|
12
|
+
readonly failure: DictationFailure;
|
|
13
|
+
};
|
|
14
|
+
export interface DictationService {
|
|
15
|
+
transcribe(audio: Blob, surface: DictationSurface): Promise<DictationAnswer>;
|
|
16
|
+
ensurePrincipal?(): Promise<void>;
|
|
17
|
+
openMic?(): Promise<MediaStream>;
|
|
18
|
+
track?(event: DictationEvent, props: Record<string, string | number | boolean>): void;
|
|
19
|
+
}
|
|
20
|
+
export declare function openVoiceMic(): Promise<MediaStream>;
|
|
21
|
+
export type DictationEvent = "dictation_started" | "dictation_finished" | "dictation_cancelled" | "dictation_failed";
|
|
22
|
+
export declare function DictationProvider(props: {
|
|
23
|
+
readonly service: DictationService;
|
|
24
|
+
readonly children: React.ReactNode;
|
|
25
|
+
}): React.ReactElement;
|
|
26
|
+
export declare function useDictationService(): DictationService | undefined;
|
|
27
|
+
export declare function joinDictated(current: string, words: string): string;
|
|
28
|
+
export declare function pickRecorderMime(isTypeSupported: (mime: string) => boolean): string | undefined;
|
|
29
|
+
export declare function dictationSupported(g?: typeof globalThis): boolean;
|
|
30
|
+
export declare function micRefusalNote(error: unknown): string;
|
|
31
|
+
export declare function dictationFailureNote(failure: DictationFailure): string;
|
|
32
|
+
export type DictationPhase = "idle" | "opening" | "recording" | "transcribing";
|
|
33
|
+
export interface UseDictationOptions {
|
|
34
|
+
readonly surface: DictationSurface;
|
|
35
|
+
readonly onText: (words: string) => void;
|
|
36
|
+
readonly maxSeconds?: number;
|
|
37
|
+
readonly scope?: string | null;
|
|
38
|
+
}
|
|
39
|
+
export interface Dictation {
|
|
40
|
+
readonly available: boolean;
|
|
41
|
+
readonly phase: DictationPhase;
|
|
42
|
+
readonly startedAt: number | undefined;
|
|
43
|
+
readonly analyser: AnalyserNode | undefined;
|
|
44
|
+
readonly note: string | undefined;
|
|
45
|
+
start(): void;
|
|
46
|
+
finish(): void;
|
|
47
|
+
cancel(): void;
|
|
48
|
+
clearNote(): void;
|
|
49
|
+
}
|
|
50
|
+
export declare const VOICE_LEVEL = 0.1;
|
|
51
|
+
export declare function useDictation(opts: UseDictationOptions): Dictation;
|
|
52
|
+
export declare function formatTakeClock(ms: number): string;
|
|
53
|
+
export declare function levelOf(samples: Uint8Array): number;
|
|
54
|
+
export interface DictationWaveProps {
|
|
55
|
+
readonly analyser: AnalyserNode | undefined;
|
|
56
|
+
readonly startedAt: number | undefined;
|
|
57
|
+
readonly className?: string;
|
|
58
|
+
readonly style?: React.CSSProperties;
|
|
59
|
+
readonly color?: string;
|
|
60
|
+
}
|
|
61
|
+
export declare function DictationWave(props: DictationWaveProps): React.ReactElement;
|
|
62
|
+
export declare function MicIcon(props: {
|
|
63
|
+
readonly size?: number;
|
|
64
|
+
}): React.ReactElement;
|
|
65
|
+
export declare function DictationCheckIcon(props: {
|
|
66
|
+
readonly size?: number;
|
|
67
|
+
}): React.ReactElement;
|
|
68
|
+
export declare function DictationCancelIcon(props: {
|
|
69
|
+
readonly size?: number;
|
|
70
|
+
}): React.ReactElement;
|
|
71
|
+
export declare function DictationSpinner(props: {
|
|
72
|
+
readonly size?: number;
|
|
73
|
+
}): React.ReactElement;
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { exception2Result } from "@adviser/cement";
|
|
3
|
+
export function openVoiceMic() {
|
|
4
|
+
return navigator.mediaDevices.getUserMedia({
|
|
5
|
+
audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true, channelCount: 1 },
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
const DictationContext = React.createContext(undefined);
|
|
9
|
+
export function DictationProvider(props) {
|
|
10
|
+
return React.createElement(DictationContext.Provider, { value: props.service }, props.children);
|
|
11
|
+
}
|
|
12
|
+
export function useDictationService() {
|
|
13
|
+
return React.useContext(DictationContext);
|
|
14
|
+
}
|
|
15
|
+
export function joinDictated(current, words) {
|
|
16
|
+
const spoken = words.trim();
|
|
17
|
+
if (spoken === "")
|
|
18
|
+
return current;
|
|
19
|
+
if (current.trim() === "")
|
|
20
|
+
return spoken;
|
|
21
|
+
return /\s$/.test(current) ? `${current}${spoken}` : `${current} ${spoken}`;
|
|
22
|
+
}
|
|
23
|
+
export function pickRecorderMime(isTypeSupported) {
|
|
24
|
+
for (const mime of ["audio/webm;codecs=opus", "audio/webm", "audio/mp4", "audio/ogg;codecs=opus"]) {
|
|
25
|
+
if (isTypeSupported(mime))
|
|
26
|
+
return mime;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
export function dictationSupported(g = globalThis) {
|
|
31
|
+
const nav = g.navigator;
|
|
32
|
+
return (typeof nav?.mediaDevices?.getUserMedia === "function" && typeof g.MediaRecorder === "function");
|
|
33
|
+
}
|
|
34
|
+
export function micRefusalNote(error) {
|
|
35
|
+
const name = typeof error === "object" && error !== null ? error.name : undefined;
|
|
36
|
+
if (name === "NotAllowedError" || name === "SecurityError")
|
|
37
|
+
return "Allow the microphone for this site to speak your idea.";
|
|
38
|
+
if (name === "NotFoundError" || name === "OverconstrainedError")
|
|
39
|
+
return "Connect a microphone to speak your idea.";
|
|
40
|
+
if (name === "NotReadableError" || name === "AbortError")
|
|
41
|
+
return "Close the other app using your microphone to speak here.";
|
|
42
|
+
return "Tap the mic to try again, or type your idea.";
|
|
43
|
+
}
|
|
44
|
+
export function dictationFailureNote(failure) {
|
|
45
|
+
if (failure.kind === "credits")
|
|
46
|
+
return "Speaking uses credits. Type your idea, or get more credits to speak it.";
|
|
47
|
+
if (failure.kind === "sign-in")
|
|
48
|
+
return "Sign in to speak your idea, or type it.";
|
|
49
|
+
return "Tap the mic to try that once more.";
|
|
50
|
+
}
|
|
51
|
+
const MIN_TAKE_MS = 400;
|
|
52
|
+
const DEFAULT_MAX_SECONDS = 600;
|
|
53
|
+
export const VOICE_LEVEL = 0.1;
|
|
54
|
+
export function useDictation(opts) {
|
|
55
|
+
const service = useDictationService();
|
|
56
|
+
const [supported, setSupported] = React.useState(false);
|
|
57
|
+
React.useEffect(() => setSupported(dictationSupported()), []);
|
|
58
|
+
const [phase, setPhase] = React.useState("idle");
|
|
59
|
+
const [startedAt, setStartedAt] = React.useState(undefined);
|
|
60
|
+
const [analyser, setAnalyser] = React.useState(undefined);
|
|
61
|
+
const [note, setNote] = React.useState(undefined);
|
|
62
|
+
const take = React.useRef(undefined);
|
|
63
|
+
const phaseRef = React.useRef("idle");
|
|
64
|
+
const optsRef = React.useRef(opts);
|
|
65
|
+
optsRef.current = opts;
|
|
66
|
+
const serviceRef = React.useRef(service);
|
|
67
|
+
serviceRef.current = service;
|
|
68
|
+
const mounted = React.useRef(true);
|
|
69
|
+
const epoch = React.useRef(0);
|
|
70
|
+
const attempt = React.useRef(0);
|
|
71
|
+
const move = React.useCallback((next) => {
|
|
72
|
+
phaseRef.current = next;
|
|
73
|
+
if (mounted.current)
|
|
74
|
+
setPhase(next);
|
|
75
|
+
}, []);
|
|
76
|
+
const release = React.useCallback((t) => {
|
|
77
|
+
if (t.meter !== undefined)
|
|
78
|
+
clearInterval(t.meter);
|
|
79
|
+
t.meter = undefined;
|
|
80
|
+
for (const track of t.stream.getTracks())
|
|
81
|
+
track.stop();
|
|
82
|
+
if (t.context !== undefined && t.context.state !== "closed")
|
|
83
|
+
void t.context.close().catch(() => undefined);
|
|
84
|
+
if (mounted.current) {
|
|
85
|
+
setAnalyser(undefined);
|
|
86
|
+
setStartedAt(undefined);
|
|
87
|
+
}
|
|
88
|
+
}, []);
|
|
89
|
+
const transcribe = React.useCallback(async (t, mimeType) => {
|
|
90
|
+
const svc = serviceRef.current;
|
|
91
|
+
const ms = Date.now() - t.startedAt;
|
|
92
|
+
const blob = new Blob(t.chunks, { type: mimeType });
|
|
93
|
+
if (svc === undefined || ms < MIN_TAKE_MS || blob.size === 0) {
|
|
94
|
+
move("idle");
|
|
95
|
+
if (mounted.current)
|
|
96
|
+
setNote("Hold the mic open and say your idea, then tap the check.");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const surface = optsRef.current.surface;
|
|
100
|
+
if (t.peak !== undefined && t.peak < VOICE_LEVEL) {
|
|
101
|
+
move("idle");
|
|
102
|
+
svc.track?.("dictation_finished", { surface, seconds: Math.round(ms / 1000), heard: false, quiet: true });
|
|
103
|
+
if (mounted.current)
|
|
104
|
+
setNote("No voice came through. Tap the mic and speak a little closer.");
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
move("transcribing");
|
|
108
|
+
const startedEpoch = epoch.current;
|
|
109
|
+
const rAnswer = await exception2Result(() => svc.transcribe(blob, surface));
|
|
110
|
+
if (rAnswer.isErr())
|
|
111
|
+
console.warn("[dictation] transcription threw:", rAnswer.Err().message);
|
|
112
|
+
const r = rAnswer.isOk()
|
|
113
|
+
? rAnswer.Ok()
|
|
114
|
+
: { ok: false, failure: { kind: "other", message: rAnswer.Err().message } };
|
|
115
|
+
if (epoch.current !== startedEpoch)
|
|
116
|
+
return;
|
|
117
|
+
move("idle");
|
|
118
|
+
if (r.ok === false) {
|
|
119
|
+
const failure = r.failure;
|
|
120
|
+
svc.track?.("dictation_failed", { surface, reason: failure.kind, seconds: Math.round(ms / 1000) });
|
|
121
|
+
if (mounted.current)
|
|
122
|
+
setNote(dictationFailureNote(failure));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const words = r.text.trim();
|
|
126
|
+
svc.track?.("dictation_finished", { surface, seconds: Math.round(ms / 1000), heard: words !== "" });
|
|
127
|
+
if (words === "") {
|
|
128
|
+
if (mounted.current)
|
|
129
|
+
setNote("No words came through. Tap the mic and speak a little closer.");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (mounted.current)
|
|
133
|
+
optsRef.current.onText(words);
|
|
134
|
+
}, [move]);
|
|
135
|
+
const stopTake = React.useCallback((keep) => {
|
|
136
|
+
const t = take.current;
|
|
137
|
+
if (t === undefined)
|
|
138
|
+
return;
|
|
139
|
+
take.current = undefined;
|
|
140
|
+
t.keep = keep;
|
|
141
|
+
if (t.recorder.state !== "inactive") {
|
|
142
|
+
t.recorder.stop();
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
release(t);
|
|
146
|
+
if (keep === false)
|
|
147
|
+
move("idle");
|
|
148
|
+
}
|
|
149
|
+
}, [move, release]);
|
|
150
|
+
const start = React.useCallback(() => {
|
|
151
|
+
if (phaseRef.current !== "idle")
|
|
152
|
+
return;
|
|
153
|
+
const svc = serviceRef.current;
|
|
154
|
+
if (svc === undefined || dictationSupported() === false)
|
|
155
|
+
return;
|
|
156
|
+
setNote(undefined);
|
|
157
|
+
attempt.current += 1;
|
|
158
|
+
const mine = attempt.current;
|
|
159
|
+
const current = () => mounted.current && attempt.current === mine && phaseRef.current === "opening";
|
|
160
|
+
move("opening");
|
|
161
|
+
const AudioCtx = globalThis.AudioContext ??
|
|
162
|
+
globalThis.webkitAudioContext;
|
|
163
|
+
const rContext = exception2Result(() => (AudioCtx === undefined ? undefined : new AudioCtx()));
|
|
164
|
+
const context = rContext.isOk() ? rContext.Ok() : undefined;
|
|
165
|
+
void context?.resume().catch(() => undefined);
|
|
166
|
+
const principal = svc.ensurePrincipal?.().catch(() => undefined);
|
|
167
|
+
void (async () => {
|
|
168
|
+
const rStream = await exception2Result(() => (svc.openMic ?? openVoiceMic)());
|
|
169
|
+
if (rStream.isErr()) {
|
|
170
|
+
void context?.close().catch(() => undefined);
|
|
171
|
+
if (current() === false)
|
|
172
|
+
return;
|
|
173
|
+
move("idle");
|
|
174
|
+
if (mounted.current)
|
|
175
|
+
setNote(micRefusalNote(rStream.Err()));
|
|
176
|
+
svc.track?.("dictation_failed", { surface: optsRef.current.surface, reason: "mic" });
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const stream = rStream.Ok();
|
|
180
|
+
if (current() === false) {
|
|
181
|
+
for (const track of stream.getTracks())
|
|
182
|
+
track.stop();
|
|
183
|
+
void context?.close().catch(() => undefined);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const mime = pickRecorderMime((m) => MediaRecorder.isTypeSupported(m));
|
|
187
|
+
const rTuned = exception2Result(() => new MediaRecorder(stream, { ...(mime === undefined ? {} : { mimeType: mime }), audioBitsPerSecond: 32000 }));
|
|
188
|
+
const rRecorder = rTuned.isOk() ? rTuned : exception2Result(() => new MediaRecorder(stream));
|
|
189
|
+
if (rRecorder.isErr()) {
|
|
190
|
+
for (const track of stream.getTracks())
|
|
191
|
+
track.stop();
|
|
192
|
+
void context?.close().catch(() => undefined);
|
|
193
|
+
move("idle");
|
|
194
|
+
if (mounted.current)
|
|
195
|
+
setNote(micRefusalNote(rRecorder.Err()));
|
|
196
|
+
svc.track?.("dictation_failed", { surface: optsRef.current.surface, reason: "recorder" });
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const recorder = rRecorder.Ok();
|
|
200
|
+
const t = {
|
|
201
|
+
stream,
|
|
202
|
+
recorder,
|
|
203
|
+
context,
|
|
204
|
+
chunks: [],
|
|
205
|
+
startedAt: Date.now(),
|
|
206
|
+
keep: true,
|
|
207
|
+
peak: undefined,
|
|
208
|
+
meter: undefined,
|
|
209
|
+
};
|
|
210
|
+
recorder.ondataavailable = (e) => {
|
|
211
|
+
if (e.data.size > 0)
|
|
212
|
+
t.chunks.push(e.data);
|
|
213
|
+
};
|
|
214
|
+
recorder.onstop = () => {
|
|
215
|
+
release(t);
|
|
216
|
+
if (t.keep === false) {
|
|
217
|
+
move("idle");
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
void Promise.resolve(principal).then(() => transcribe(t, recorder.mimeType || mime || "audio/webm"));
|
|
221
|
+
};
|
|
222
|
+
if (context !== undefined) {
|
|
223
|
+
const rMeter = exception2Result(() => {
|
|
224
|
+
const node = context.createAnalyser();
|
|
225
|
+
node.fftSize = 512;
|
|
226
|
+
node.smoothingTimeConstant = 0.5;
|
|
227
|
+
context.createMediaStreamSource(stream).connect(node);
|
|
228
|
+
return node;
|
|
229
|
+
});
|
|
230
|
+
if (rMeter.isOk()) {
|
|
231
|
+
const node = rMeter.Ok();
|
|
232
|
+
const samples = new Uint8Array(node.fftSize);
|
|
233
|
+
t.peak = 0;
|
|
234
|
+
t.meter = setInterval(() => {
|
|
235
|
+
node.getByteTimeDomainData(samples);
|
|
236
|
+
t.peak = Math.max(t.peak ?? 0, levelOf(samples));
|
|
237
|
+
}, 50);
|
|
238
|
+
}
|
|
239
|
+
setAnalyser(rMeter.isOk() ? rMeter.Ok() : undefined);
|
|
240
|
+
}
|
|
241
|
+
const rStart = exception2Result(() => recorder.start(1000));
|
|
242
|
+
if (rStart.isErr()) {
|
|
243
|
+
release(t);
|
|
244
|
+
move("idle");
|
|
245
|
+
if (mounted.current)
|
|
246
|
+
setNote(micRefusalNote(rStart.Err()));
|
|
247
|
+
svc.track?.("dictation_failed", { surface: optsRef.current.surface, reason: "recorder" });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
take.current = t;
|
|
251
|
+
setStartedAt(t.startedAt);
|
|
252
|
+
move("recording");
|
|
253
|
+
svc.track?.("dictation_started", { surface: optsRef.current.surface });
|
|
254
|
+
})();
|
|
255
|
+
}, [move, release, transcribe]);
|
|
256
|
+
const finish = React.useCallback(() => {
|
|
257
|
+
if (phaseRef.current === "opening") {
|
|
258
|
+
attempt.current += 1;
|
|
259
|
+
move("idle");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (phaseRef.current !== "recording")
|
|
263
|
+
return;
|
|
264
|
+
stopTake(true);
|
|
265
|
+
}, [move, stopTake]);
|
|
266
|
+
const cancel = React.useCallback(() => {
|
|
267
|
+
if (phaseRef.current === "opening") {
|
|
268
|
+
attempt.current += 1;
|
|
269
|
+
move("idle");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (phaseRef.current !== "recording")
|
|
273
|
+
return;
|
|
274
|
+
serviceRef.current?.track?.("dictation_cancelled", { surface: optsRef.current.surface });
|
|
275
|
+
stopTake(false);
|
|
276
|
+
}, [move, stopTake]);
|
|
277
|
+
const scope = opts.scope ?? null;
|
|
278
|
+
const scopeSeen = React.useRef(scope);
|
|
279
|
+
React.useEffect(() => {
|
|
280
|
+
if (scopeSeen.current === scope)
|
|
281
|
+
return;
|
|
282
|
+
scopeSeen.current = scope;
|
|
283
|
+
epoch.current += 1;
|
|
284
|
+
const t = take.current;
|
|
285
|
+
take.current = undefined;
|
|
286
|
+
if (t !== undefined) {
|
|
287
|
+
t.keep = false;
|
|
288
|
+
if (t.recorder.state !== "inactive")
|
|
289
|
+
t.recorder.stop();
|
|
290
|
+
else
|
|
291
|
+
release(t);
|
|
292
|
+
}
|
|
293
|
+
move("idle");
|
|
294
|
+
setNote(undefined);
|
|
295
|
+
}, [scope, move, release]);
|
|
296
|
+
React.useEffect(() => {
|
|
297
|
+
if (phase !== "recording")
|
|
298
|
+
return;
|
|
299
|
+
const limit = (optsRef.current.maxSeconds ?? DEFAULT_MAX_SECONDS) * 1000;
|
|
300
|
+
const timer = setTimeout(() => stopTake(true), limit);
|
|
301
|
+
return () => clearTimeout(timer);
|
|
302
|
+
}, [phase, stopTake]);
|
|
303
|
+
React.useEffect(() => {
|
|
304
|
+
if (phase !== "recording" && phase !== "opening")
|
|
305
|
+
return;
|
|
306
|
+
const onKey = (e) => {
|
|
307
|
+
if (e.key === "Escape") {
|
|
308
|
+
e.preventDefault();
|
|
309
|
+
cancel();
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
const onHide = () => {
|
|
313
|
+
if (document.visibilityState !== "hidden")
|
|
314
|
+
return;
|
|
315
|
+
if (phaseRef.current === "opening")
|
|
316
|
+
cancel();
|
|
317
|
+
else
|
|
318
|
+
stopTake(true);
|
|
319
|
+
};
|
|
320
|
+
const onPageHide = () => (phaseRef.current === "opening" ? cancel() : stopTake(false));
|
|
321
|
+
window.addEventListener("keydown", onKey);
|
|
322
|
+
document.addEventListener("visibilitychange", onHide);
|
|
323
|
+
window.addEventListener("pagehide", onPageHide);
|
|
324
|
+
return () => {
|
|
325
|
+
window.removeEventListener("keydown", onKey);
|
|
326
|
+
document.removeEventListener("visibilitychange", onHide);
|
|
327
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
328
|
+
};
|
|
329
|
+
}, [phase, cancel, stopTake]);
|
|
330
|
+
React.useEffect(() => {
|
|
331
|
+
mounted.current = true;
|
|
332
|
+
return () => {
|
|
333
|
+
mounted.current = false;
|
|
334
|
+
const t = take.current;
|
|
335
|
+
take.current = undefined;
|
|
336
|
+
if (t !== undefined) {
|
|
337
|
+
t.keep = false;
|
|
338
|
+
if (t.recorder.state !== "inactive")
|
|
339
|
+
t.recorder.stop();
|
|
340
|
+
for (const track of t.stream.getTracks())
|
|
341
|
+
track.stop();
|
|
342
|
+
if (t.context !== undefined && t.context.state !== "closed")
|
|
343
|
+
void t.context.close().catch(() => undefined);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
}, []);
|
|
347
|
+
const clearNote = React.useCallback(() => setNote(undefined), []);
|
|
348
|
+
return {
|
|
349
|
+
available: supported && service !== undefined,
|
|
350
|
+
phase,
|
|
351
|
+
startedAt,
|
|
352
|
+
analyser,
|
|
353
|
+
note,
|
|
354
|
+
start,
|
|
355
|
+
finish,
|
|
356
|
+
cancel,
|
|
357
|
+
clearNote,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
export function formatTakeClock(ms) {
|
|
361
|
+
const total = Math.max(0, Math.floor(ms / 1000));
|
|
362
|
+
return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}`;
|
|
363
|
+
}
|
|
364
|
+
export function levelOf(samples) {
|
|
365
|
+
let sum = 0;
|
|
366
|
+
for (const s of samples) {
|
|
367
|
+
const v = (s - 128) / 128;
|
|
368
|
+
sum += v * v;
|
|
369
|
+
}
|
|
370
|
+
const rms = Math.sqrt(sum / Math.max(1, samples.length));
|
|
371
|
+
return Math.min(1, Math.pow(rms * 3.2, 0.7));
|
|
372
|
+
}
|
|
373
|
+
export function DictationWave(props) {
|
|
374
|
+
const canvas = React.useRef(null);
|
|
375
|
+
const clock = React.useRef(null);
|
|
376
|
+
React.useEffect(() => {
|
|
377
|
+
const el = canvas.current;
|
|
378
|
+
if (el === null)
|
|
379
|
+
return;
|
|
380
|
+
const ctx = el.getContext("2d");
|
|
381
|
+
if (ctx === null)
|
|
382
|
+
return;
|
|
383
|
+
const samples = new Uint8Array(props.analyser?.fftSize ?? 512);
|
|
384
|
+
const BAR = 3;
|
|
385
|
+
const GAP = 2;
|
|
386
|
+
let levels = [];
|
|
387
|
+
let last = 0;
|
|
388
|
+
let frame = 0;
|
|
389
|
+
const color = props.color ?? (getComputedStyle(el).color || "#000");
|
|
390
|
+
const draw = (now) => {
|
|
391
|
+
frame = requestAnimationFrame(draw);
|
|
392
|
+
const dpr = globalThis.devicePixelRatio || 1;
|
|
393
|
+
const w = el.clientWidth;
|
|
394
|
+
const h = el.clientHeight;
|
|
395
|
+
if (el.width !== Math.round(w * dpr) || el.height !== Math.round(h * dpr)) {
|
|
396
|
+
el.width = Math.round(w * dpr);
|
|
397
|
+
el.height = Math.round(h * dpr);
|
|
398
|
+
}
|
|
399
|
+
const slots = Math.max(1, Math.floor(w / (BAR + GAP)));
|
|
400
|
+
if (now - last > 70) {
|
|
401
|
+
last = now;
|
|
402
|
+
let level = 0;
|
|
403
|
+
if (props.analyser !== undefined) {
|
|
404
|
+
props.analyser.getByteTimeDomainData(samples);
|
|
405
|
+
level = levelOf(samples);
|
|
406
|
+
}
|
|
407
|
+
levels.push(level);
|
|
408
|
+
if (levels.length > slots)
|
|
409
|
+
levels = levels.slice(levels.length - slots);
|
|
410
|
+
}
|
|
411
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
412
|
+
ctx.clearRect(0, 0, w, h);
|
|
413
|
+
ctx.fillStyle = color;
|
|
414
|
+
const left = w - slots * (BAR + GAP) + GAP;
|
|
415
|
+
const empty = slots - levels.length;
|
|
416
|
+
for (let i = 0; i < slots; i++) {
|
|
417
|
+
const idx = i - empty;
|
|
418
|
+
const level = idx >= 0 ? levels[idx] : 0;
|
|
419
|
+
const x = left + i * (BAR + GAP);
|
|
420
|
+
const bh = Math.max(3, level * (h - 2));
|
|
421
|
+
const y = (h - bh) / 2;
|
|
422
|
+
ctx.globalAlpha = idx >= 0 ? 1 : 0.3;
|
|
423
|
+
ctx.beginPath();
|
|
424
|
+
if (typeof ctx.roundRect === "function")
|
|
425
|
+
ctx.roundRect(x, y, BAR, bh, BAR / 2);
|
|
426
|
+
else
|
|
427
|
+
ctx.rect(x, y, BAR, bh);
|
|
428
|
+
ctx.fill();
|
|
429
|
+
}
|
|
430
|
+
ctx.globalAlpha = 1;
|
|
431
|
+
if (clock.current !== null && props.startedAt !== undefined) {
|
|
432
|
+
const text = formatTakeClock(Date.now() - props.startedAt);
|
|
433
|
+
if (clock.current.textContent !== text)
|
|
434
|
+
clock.current.textContent = text;
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
frame = requestAnimationFrame(draw);
|
|
438
|
+
return () => cancelAnimationFrame(frame);
|
|
439
|
+
}, [props.analyser, props.startedAt, props.color]);
|
|
440
|
+
return (React.createElement("span", { className: props.className, "data-dictation-wave": "", style: { display: "flex", alignItems: "center", gap: 10, minWidth: 0, flex: 1, ...props.style } },
|
|
441
|
+
React.createElement("canvas", { ref: canvas, "aria-hidden": true, style: { flex: 1, minWidth: 0, height: 24, display: "block" } }),
|
|
442
|
+
React.createElement("span", { ref: clock, "aria-live": "off", style: { fontVariantNumeric: "tabular-nums", fontSize: 13, fontWeight: 700, flex: "none" } }, "0:00")));
|
|
443
|
+
}
|
|
444
|
+
export function MicIcon(props) {
|
|
445
|
+
const size = props.size ?? 18;
|
|
446
|
+
return (React.createElement("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true },
|
|
447
|
+
React.createElement("rect", { x: "9", y: "2.5", width: "6", height: "12", rx: "3" }),
|
|
448
|
+
React.createElement("path", { d: "M5 11a7 7 0 0 0 14 0" }),
|
|
449
|
+
React.createElement("path", { d: "M12 18v3.5" })));
|
|
450
|
+
}
|
|
451
|
+
export function DictationCheckIcon(props) {
|
|
452
|
+
const size = props.size ?? 18;
|
|
453
|
+
return (React.createElement("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.6, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true },
|
|
454
|
+
React.createElement("path", { d: "M5 12.5l4.5 4.5L19 7.5" })));
|
|
455
|
+
}
|
|
456
|
+
export function DictationCancelIcon(props) {
|
|
457
|
+
const size = props.size ?? 16;
|
|
458
|
+
return (React.createElement("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.6, strokeLinecap: "round", "aria-hidden": true },
|
|
459
|
+
React.createElement("path", { d: "M6 6l12 12M18 6L6 18" })));
|
|
460
|
+
}
|
|
461
|
+
export function DictationSpinner(props) {
|
|
462
|
+
const size = props.size ?? 18;
|
|
463
|
+
return (React.createElement("svg", { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.6, strokeLinecap: "round", "aria-hidden": true },
|
|
464
|
+
React.createElement("path", { d: "M12 3a9 9 0 1 0 9 9", opacity: "0.9" },
|
|
465
|
+
React.createElement("animateTransform", { attributeName: "transform", type: "rotate", from: "0 12 12", to: "360 12 12", dur: "0.8s", repeatCount: "indefinite" }))));
|
|
466
|
+
}
|
package/components/index.d.ts
CHANGED
package/components/index.js
CHANGED