2026-08-22 04:28:13 +03:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
2026-10-06 03:55:14 +03:00
|
|
|
|
import { ArrowRightIcon, CornerDownLeftIcon, SquareIcon } from "lucide-react";
|
2026-08-22 04:28:13 +03:00
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-09-25 01:52:20 +03:00
|
|
|
|
import { Hint } from "@/components/ui/hint";
|
2026-08-22 04:28:13 +03:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-22 05:02:15 +03:00
|
|
|
|
* The chat composer — content row ONLY (the persistent rounded shell lives in
|
|
|
|
|
|
* the chat view's overlay, so the shell can later morph chat → question
|
|
|
|
|
|
* without remounting).
|
2026-08-22 04:28:13 +03:00
|
|
|
|
*
|
2026-08-22 05:02:15 +03:00
|
|
|
|
* Corner-radius math (concentric with the shell): shell rounded-2xl (16px) −
|
|
|
|
|
|
* row padding p-1.5 (6px) = button rounded-[10px] = 25% of the 40px button.
|
2026-08-22 04:28:13 +03:00
|
|
|
|
*/
|
2026-09-27 00:20:07 +03:00
|
|
|
|
/** What one Enter press does, in precedence order: stop a busy stream,
|
2026-10-06 03:47:05 +03:00
|
|
|
|
* otherwise send the message, otherwise — practice sessions only — fire
|
|
|
|
|
|
* the next-problem signal while the between-problems pause is live
|
|
|
|
|
|
* (owner round 16: Enter on an EMPTY composer is the go-ahead; typing
|
|
|
|
|
|
* anything makes Enter a send again). Pure so the static-render harness
|
2026-09-27 00:20:07 +03:00
|
|
|
|
* can pin the decision (key events can't be simulated in
|
|
|
|
|
|
* renderToStaticMarkup). */
|
2026-10-06 03:47:05 +03:00
|
|
|
|
export function enterAction(state: {
|
|
|
|
|
|
busy: boolean;
|
|
|
|
|
|
canSend: boolean;
|
|
|
|
|
|
/** The between-problems pause is live: practice session, nothing
|
|
|
|
|
|
* active, no turn/compaction in flight (the caller's fold). */
|
|
|
|
|
|
nextProblem?: boolean;
|
|
|
|
|
|
}): "stop" | "send" | "next" | "none" {
|
2026-10-03 03:01:51 +03:00
|
|
|
|
// Composer steering (owner arc 2026-10-03): while a turn runs, typed
|
|
|
|
|
|
// text makes Enter a STEER (the queue-and-redirect send); Enter with
|
|
|
|
|
|
// an empty composer stays the stop shortcut. The button itself keeps
|
|
|
|
|
|
// the square — Stop stays one click away at all times.
|
|
|
|
|
|
if (state.busy) return state.canSend ? "send" : "stop";
|
2026-09-19 13:16:34 +03:00
|
|
|
|
if (state.canSend) return "send";
|
2026-10-06 03:47:05 +03:00
|
|
|
|
if (state.nextProblem) return "next";
|
2026-09-19 13:16:34 +03:00
|
|
|
|
return "none";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-22 04:28:13 +03:00
|
|
|
|
export function Composer({
|
|
|
|
|
|
busy,
|
|
|
|
|
|
value,
|
|
|
|
|
|
onChange,
|
|
|
|
|
|
onSend,
|
|
|
|
|
|
onStop,
|
|
|
|
|
|
canSend,
|
|
|
|
|
|
autoFocus,
|
2026-10-02 01:37:18 +03:00
|
|
|
|
inputRef,
|
2026-09-17 19:31:25 +03:00
|
|
|
|
onEscape,
|
2026-09-24 19:21:03 +03:00
|
|
|
|
placeholder = "Ask, answer, or steer the lesson…",
|
2026-10-06 03:47:05 +03:00
|
|
|
|
nextProblem = false,
|
|
|
|
|
|
onNext,
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}: {
|
|
|
|
|
|
busy: boolean;
|
|
|
|
|
|
value: string;
|
|
|
|
|
|
onChange: (v: string) => void;
|
|
|
|
|
|
onSend: () => void;
|
|
|
|
|
|
onStop: () => void;
|
|
|
|
|
|
canSend: boolean;
|
|
|
|
|
|
autoFocus?: boolean;
|
2026-10-02 01:37:18 +03:00
|
|
|
|
/** The page's handle to the textarea — the dictation-end focus (owner,
|
|
|
|
|
|
* 2026-10-01): a run ending hands the cursor to the bare composer. */
|
|
|
|
|
|
inputRef?: React.RefObject<HTMLTextAreaElement | null>;
|
2026-09-17 19:31:25 +03:00
|
|
|
|
/** Escape while a dictation runs stops it (STT wave, 2026-09-17). No-op
|
|
|
|
|
|
* when absent — Esc otherwise means nothing in the composer. */
|
|
|
|
|
|
onEscape?: () => void;
|
2026-09-24 19:21:03 +03:00
|
|
|
|
/** The textarea's placeholder (owner refinement round, 2026-09-24):
|
|
|
|
|
|
* active sessions read "Ask, answer, or steer the lesson…"; the fresh
|
|
|
|
|
|
* new-session page asks the graph view's opening question instead. */
|
|
|
|
|
|
placeholder?: string;
|
2026-10-06 03:47:05 +03:00
|
|
|
|
/** The practice pause is live (owner round 16): Enter on the empty
|
|
|
|
|
|
* composer fires the next-problem signal instead of doing nothing —
|
|
|
|
|
|
* the keyboard-first move-on. */
|
|
|
|
|
|
nextProblem?: boolean;
|
|
|
|
|
|
/** The signal's dispatcher (send({type:"next"})) — the chip and the
|
|
|
|
|
|
* empty-Enter share it. */
|
|
|
|
|
|
onNext?: () => void;
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}) {
|
2026-10-06 03:47:05 +03:00
|
|
|
|
const act = () => {
|
|
|
|
|
|
const action = enterAction({ busy, canSend, nextProblem });
|
|
|
|
|
|
if (action === "stop") onStop();
|
|
|
|
|
|
else if (action === "send") onSend();
|
|
|
|
|
|
else if (action === "next") onNext?.();
|
|
|
|
|
|
};
|
2026-08-22 04:28:13 +03:00
|
|
|
|
return (
|
|
|
|
|
|
<form
|
|
|
|
|
|
data-testid="composer"
|
2026-08-22 05:02:15 +03:00
|
|
|
|
className="flex items-end gap-1.5 p-1.5"
|
2026-08-22 04:28:13 +03:00
|
|
|
|
onSubmit={(e) => {
|
|
|
|
|
|
e.preventDefault();
|
2026-10-06 03:47:05 +03:00
|
|
|
|
act();
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<textarea
|
2026-10-02 01:37:18 +03:00
|
|
|
|
ref={inputRef}
|
2026-08-22 04:28:13 +03:00
|
|
|
|
value={value}
|
|
|
|
|
|
onChange={(e) => onChange(e.target.value)}
|
2026-08-22 05:02:15 +03:00
|
|
|
|
onKeyDown={(e) => {
|
2026-09-17 19:31:25 +03:00
|
|
|
|
// Esc ends dictation before anything else Esc might someday mean
|
|
|
|
|
|
// here (the dictate decisions: click-again or Esc to stop).
|
2026-09-17 20:47:23 +03:00
|
|
|
|
// stopPropagation claims the Esc (keyboard wave A): the
|
|
|
|
|
|
// window-level chain must not also abort a turn under us.
|
2026-09-17 19:31:25 +03:00
|
|
|
|
if (e.key === "Escape" && onEscape) {
|
|
|
|
|
|
e.preventDefault();
|
2026-09-17 20:47:23 +03:00
|
|
|
|
e.stopPropagation();
|
2026-09-17 19:31:25 +03:00
|
|
|
|
onEscape();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-09-09 02:25:27 +03:00
|
|
|
|
// Chat convention: Enter sends, Shift+Enter is a newline.
|
|
|
|
|
|
// ⌘/Ctrl+Enter also sends — the universal submit shortcut
|
|
|
|
|
|
// across every input in the app (thinking field, answers).
|
|
|
|
|
|
if (e.key === "Enter" && (e.metaKey || e.ctrlKey || !e.shiftKey)) {
|
2026-08-22 05:02:15 +03:00
|
|
|
|
e.preventDefault();
|
2026-10-06 03:47:05 +03:00
|
|
|
|
act();
|
2026-09-27 00:20:07 +03:00
|
|
|
|
}
|
2026-08-22 05:02:15 +03:00
|
|
|
|
}}
|
2026-09-24 19:21:03 +03:00
|
|
|
|
placeholder={placeholder}
|
2026-08-22 04:28:13 +03:00
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
|
|
rows={1}
|
2026-09-17 13:15:35 +03:00
|
|
|
|
className="field-sizing-content lh-input max-h-48 min-h-10 flex-1 resize-none bg-transparent px-2.5 py-2 outline-none placeholder:text-muted-foreground"
|
2026-08-22 04:28:13 +03:00
|
|
|
|
/>
|
2026-10-03 03:07:49 +03:00
|
|
|
|
{/* The button follows the text (owner report 2026-10-03): while
|
|
|
|
|
|
a turn runs WITH text typed it stays the SEND affordance
|
|
|
|
|
|
(click = steer — steering must be clickable, not Enter-only);
|
|
|
|
|
|
only an empty composer morphs it to Stop. ESC keeps its
|
|
|
|
|
|
confirm-popup abort in every state, so stop is never
|
|
|
|
|
|
unreachable. No tooltip — the ↵ glyph is the affordance
|
2026-10-06 03:55:14 +03:00
|
|
|
|
(owner, subtlety round 2026-09-25).
|
|
|
|
|
|
|
|
|
|
|
|
THE NEXT MORPH (owner round 16 refinement, 2026-10-06): an
|
|
|
|
|
|
empty composer in the practice pause morphs the button into
|
|
|
|
|
|
a wide green NEXT PROBLEM — the affordance IS the button
|
|
|
|
|
|
(click and Enter share one lane), and typing anything
|
|
|
|
|
|
returns the regular send. The green is the app's correct-
|
|
|
|
|
|
answer register (answered-card's green-500/600). */}
|
|
|
|
|
|
{nextProblem && !busy && !canSend ? (
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
data-testid="next-problem-button"
|
|
|
|
|
|
aria-label="Next problem"
|
|
|
|
|
|
className="flex h-10 shrink-0 items-center justify-center gap-1.5 rounded-[10px] bg-green-600 px-4 font-ui text-xs font-medium uppercase tracking-wide text-white transition-colors hover:bg-green-500"
|
|
|
|
|
|
>
|
|
|
|
|
|
Next problem
|
|
|
|
|
|
<ArrowRightIcon className="size-4" />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
aria-label={busy && !canSend ? "Stop" : "Send"}
|
|
|
|
|
|
disabled={!busy && !canSend}
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"flex size-10 shrink-0 items-center justify-center rounded-[10px] transition-colors",
|
|
|
|
|
|
busy && !canSend
|
|
|
|
|
|
? "bg-secondary text-foreground hover:bg-accent"
|
|
|
|
|
|
: "bg-primary text-primary-foreground hover:opacity-90 disabled:opacity-40",
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{busy && !canSend ? <SquareIcon className="size-4" /> : <CornerDownLeftIcon className="size-5" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
2026-08-22 04:28:13 +03:00
|
|
|
|
</form>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|