Files
CatoandClaude 04dd6f86b8 feat: under every answer — Copy, Listen, Share; sources as a sheet; replies that lead (0.10.0) (#20)
The pieces of the ChatGPT app the owner put beside ours (2026-10-21), done
once here so Loki, OrangeCat and every chat in the fleet get them:

- `AnswerActions`: the row under an answer. Copy; Listen, reading the answer
  as prose (`plainTextForSpeech`, from loki) with the browser's voice or the
  `speaker` an app passes (loki's studio voice), with a small player — Stop
  and the time so far — while it reads; Share through the system sheet
  where one exists. `ChatThread` renders it under every answer.
- Sources on a touch screen are ONE "Sources · n" button that opens a sheet
  with every record (label, detail, link); chips stay on a mouse. `Sheet`
  (the attach sheet's <dialog>, extracted) and `SourcesRow` are exported.
- `AttachMenu` takes `extra` rows under Camera · Photos · Files, and then
  opens the sheet on a mouse too — an app's "one of my things" has no file
  dialog to live in.
- The replies instruction makes the model lead: 2–4 replies on every answer,
  the recommended next step first, drawn as the lead (`ck-reply-lead`).
- An empty box is one row on the narrowest phone: the placeholder is cut at
  the edge, never wrapped into a clipped second line.

Browser checks cover the player, the sources sheet on a touch screen and
chips on a mouse, and the one-row placeholder at 320px.


Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-21 06:21:21 +02:00

66 lines
2.4 KiB
JavaScript

"use client";
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
import { createSynthSpeaker, plainTextForSpeech } from "../speech.js";
const noSubscribe = () => () => { };
/**
* Read an answer aloud — the play button under every answer in the reference
* apps. The browser's own voices by default: no upload, no key, works
* offline. An app with a studio voice passes its own `speaker` (loki's
* Orpheus voice, through the same two methods) and the button, the timer and
* the stop are the same.
*
* One reading at a time: play on a second answer stops the first. Unmounting
* stops it too — a voice that keeps reading a page you left is the
* microphone bug's twin.
*/
export function useSpeech(speaker) {
const synthSupported = useSyncExternalStore(noSubscribe, () => typeof window !== "undefined" && "speechSynthesis" in window, () => false);
const supported = Boolean(speaker) || synthSupported;
const [speaking, setSpeaking] = useState(false);
const [elapsed, setElapsed] = useState(0);
const synth = useRef(null);
const active = useRef(null);
const token = useRef(0);
const stop = useCallback(() => {
token.current++;
active.current?.cancel();
active.current = null;
setSpeaking(false);
setElapsed(0);
}, []);
const speak = useCallback((markdown) => {
const who = speaker ?? (synth.current ??= createSynthSpeaker());
if (!who)
return;
const text = plainTextForSpeech(markdown);
if (!text)
return;
stop();
const my = ++token.current;
active.current = who;
setSpeaking(true);
void who.speak(text).then(() => {
if (my === token.current) {
active.current = null;
setSpeaking(false);
setElapsed(0);
}
});
}, [speaker, stop]);
const toggle = useCallback((markdown) => {
if (speaking)
stop();
else
speak(markdown);
}, [speak, speaking, stop]);
useEffect(() => {
if (!speaking)
return;
const started = Date.now();
const t = setInterval(() => setElapsed((Date.now() - started) / 1000), 500);
return () => clearInterval(t);
}, [speaking]);
useEffect(() => () => stop(), [stop]);
return { supported, speaking, elapsed, speak, stop, toggle };
}
//# sourceMappingURL=use-speech.js.map