"use client" /** * Contextual rewrite bar — Leo attached to the field being edited. * * Shares the Leo search-bar pill and its thinking wash, but the job is * different: the search bar finds records, this one writes or changes the text * already in front of the user. * * One intent, two content states: * empty — nothing written yet, so Leo drafts from a description. * filled — there is text, so Leo rewrites it. Quick actions live in the * overflow menu; undo / redo sit after it once Leo has written. * * Scope is a third axis, independent of both: with `selection` set, Leo sees * only that span and the result is spliced back into place. Undo still restores * the whole field, so a scoped rewrite is no harder to take back than any other. * * The bar owns the undo / redo stack for Leo's writes only. Typing by the user * is not tracked, so undo always means "put back what Leo replaced". * * @see components/ask-leo-composer.tsx * @see components/leo-assist-field.tsx — selection scope and the collapsed form */ import * as React from "react" import { AnimatePresence, motion, useReducedMotion } from "motion/react" import { AskLeoButton } from "@/components/ask-leo-button" import { AskLeoComposer } from "@/components/ask-leo-composer" import { useLeoAmbience } from "@/components/leo-ambience-context" import { searchBarShellClassName } from "@/components/search-bar-shell" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { Kbd } from "@/components/ui/kbd" import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip" import { cn } from "@/lib/utils" /** Which content state the field is in. Drives copy, actions, and placeholder. */ export type LeoAssistMode = "generate" | "rewrite" export interface LeoAssistAction { id: string /** Button or menu label. Also the instruction when `instruction` is omitted. */ label: string /** FontAwesome name without the style prefix, e.g. `fa-wand-magic-sparkles`. */ icon?: string /** Sent to Leo when the label is too terse to act on. */ instruction?: string } /** A span of the field Leo has been narrowed to. */ export interface LeoAssistSelection { start: number end: number text: string } export interface LeoAssistRequest { mode: LeoAssistMode /** What the user asked for, typed or from an action. */ instruction: string /** * The text in scope when the request is made: the selected span when there * is one, the whole field otherwise, empty in generate mode. */ text: string /** The span in scope, or null when Leo is working on the whole field. */ selection: LeoAssistSelection | null } export interface LeoAssistBarProps { /** The text Leo works on. Controlled by the host field. */ text: string onTextChange: (next: string) => void /** * Runs the request and resolves with the new text **for the scope it was * given**: the replacement for the span when `request.selection` is set, the * whole field otherwise. The bar does the splicing, so a host never has to * reason about offsets. Reject to surface an inline error; the bar never * writes on a rejection, so the undo stack stays truthful. */ onRun: (request: LeoAssistRequest) => Promise /** * Narrows Leo to a span of `text`. Everything outside it is left alone, byte * for byte, which is the whole reason a user selects before asking. */ selection?: LeoAssistSelection | null /** What the scope chip calls the narrowed span. */ scopeLabel?: string /** * Renders as a single button until the user asks for Leo. For forms where a * bar per field would be ten bars, and for fields the user mostly types in * unaided. */ collapsible?: boolean /** Controlled open state. Leave unset to let the bar own it. */ open?: boolean onOpenChange?: (open: boolean) => void /** Label on the collapsed trigger. */ collapsedLabel?: string /** Offered once the field has content. All entries live in the overflow. */ actions?: readonly LeoAssistAction[] /** Offered while the field is empty. All entries live in the overflow. */ emptyActions?: readonly LeoAssistAction[] /** Names the field Leo is editing, for screen readers. */ fieldLabel?: string /** Rotating example instructions for the filled state. */ examples?: readonly string[] /** Rotating example instructions for the empty state. */ emptyExamples?: readonly string[] /** * Fires when Leo starts and stops. Hosts use it to lock the field and the * rest of the form, so nothing is edited underneath a run in flight. */ onRunningChange?: (running: boolean) => void disabled?: boolean className?: string } const MODE_COPY: Record< LeoAssistMode, { placeholder: string; inputLabel: string; submitLabel: string } > = { generate: { placeholder: "Describe what to write", inputLabel: "Describe what Leo should write", submitLabel: "Write with Leo", }, rewrite: { placeholder: "Describe your change", inputLabel: "Describe a change for Leo", submitLabel: "Rewrite with Leo", }, } /** What Leo is told to do. The label is the fallback when it says it plainly. */ function instructionFor(action: LeoAssistAction) { return action.instruction ?? action.label } const GHOST_ICON_BTN = "icon-button-chrome size-8 shrink-0 rounded-full hover:bg-accent hover:text-interactive-hover-foreground" /** * The two widths the bar lives at, in pixels rather than utilities because the * open gesture animates between them. * * The collapsed width is the pill's own resting height, so the trigger is not a * button that happens to be round: it is the pill closed down to a circle. The * shell keeps a radius of half that height, so at one row it *is* a circle and * the frame the user clicks is the frame that grows. * * The height is not free to choose: it is what `AskLeoComposer` measures to with * a 32px control and `p-1.5`, so it moves when the composer's controls do. */ const BAR_COLLAPSED_PX = 46 const BAR_OPEN_PX = 448 /** * The same two numbers as classes, spelled out because Tailwind reads source * text: an interpolated `rounded-[${n}px]` compiles to nothing. They are asserted * against `BAR_COLLAPSED_PX` below so the pair cannot drift silently. */ const BAR_COLLAPSED_SIZE_CLASS = "size-[46px]" const BAR_SHELL_RADIUS_CLASS = "rounded-[23px]!" if (process.env.NODE_ENV !== "production") { const size = Number(BAR_COLLAPSED_SIZE_CLASS.match(/\d+/)?.[0]) const radius = Number(BAR_SHELL_RADIUS_CLASS.match(/\d+/)?.[0]) if (size !== BAR_COLLAPSED_PX || radius !== BAR_COLLAPSED_PX / 2) { throw new Error( `LeoAssistBar geometry disagrees: ${BAR_COLLAPSED_PX}px tall, but the classes say ${size}px and radius ${radius}px.`, ) } } /** * The open gesture is a reveal, not a resize. The pill is already at its full * width behind the circle, so moving the clip edge shows more of a box that * never changes shape, and the browser never has to lay the page out again * mid-animation. The radius matches the circle so the moving edge stays a cap * rather than a straight cut. * * The settled shape reaches past the box on every side. Left at the border the * clip would shave off the focus ring and the working wash. */ const CLIP_RADIUS_PX = BAR_COLLAPSED_PX / 2 const CLIP_COLLAPSED = `inset(0px ${BAR_OPEN_PX - BAR_COLLAPSED_PX}px 0px 0px round ${CLIP_RADIUS_PX}px)` const CLIP_OPEN = `inset(0px 0px 0px 0px round ${CLIP_RADIUS_PX}px)` const CLIP_SETTLED = `inset(-64px -64px -64px -64px round ${CLIP_RADIUS_PX}px)` // ── Pure helpers ────────────────────────────────────────────────────────────── // A collapsed range is not a scope: clicking into a field to place the caret // would otherwise narrow Leo to nothing and leave him unable to write. function resolveScope(selection: LeoAssistSelection | null | undefined): LeoAssistSelection | null { return selection && selection.end > selection.start ? selection : null } function resolveModeState(f: { scopedText: string actions?: readonly LeoAssistAction[] emptyActions?: readonly LeoAssistAction[] examples?: readonly string[] emptyExamples?: readonly string[] }) { const mode: LeoAssistMode = f.scopedText.trim() ? "rewrite" : "generate" const rewrite = mode === "rewrite" return { mode, copy: MODE_COPY[mode], modeActions: (rewrite ? f.actions : f.emptyActions) ?? [], modeExamples: rewrite ? f.examples : f.emptyExamples, } } function spliceResult(text: string, target: LeoAssistSelection | null, result: string): string { if (!target) return result return text.slice(0, target.start) + result + text.slice(target.end) } function withFieldLabel(label: string, fieldLabel: string | undefined): string { return fieldLabel ? `${label}. Field: ${fieldLabel}` : label } type BarKeyIntent = "clear-draft" | "close" | "redo" | "undo" | null function interpretBarKey( event: React.KeyboardEvent, f: { collapsible: boolean; running: boolean; draft: string; locked: boolean }, ): BarKeyIntent { // Escape is the way back out of the expanded bar. It only closes once the // instruction is empty, so a half-typed thought is never thrown away by a // keystroke the user meant for a menu. if (event.key === "Escape" && f.collapsible && !f.running) { return f.draft ? "clear-draft" : "close" } const isUndoChord = (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "z" if (!isUndoChord || f.locked) return null return event.shiftKey ? "redo" : "undo" } function busyShellClass(busy: boolean): string | undefined { return busy ? "border-[color:color-mix(in_oklch,var(--brand-color)_45%,var(--control-border))]! bg-card/70! shadow-sm! ring-2 ring-brand/15!" : undefined } // ── Hooks ───────────────────────────────────────────────────────────────────── /** Leo's writes only. `past` holds what the text was before each one. */ function useLeoHistory(text: string, onTextChange: (next: string) => void) { const [past, setPast] = React.useState([]) const [future, setFuture] = React.useState([]) const canUndo = past.length > 0 const canRedo = future.length > 0 function applyWrite(next: string) { setPast(stack => [...stack, text]) setFuture([]) onTextChange(next) } function undo() { if (!canUndo) return const previous = past[past.length - 1] setPast(stack => stack.slice(0, -1)) setFuture(stack => [text, ...stack]) onTextChange(previous) } function redo() { if (!canRedo) return const next = future[0] setFuture(stack => stack.slice(1)) setPast(stack => [...stack, text]) onTextChange(next) } return { canUndo, canRedo, applyWrite, undo, redo } } function useLeoBarOpen( collapsible: boolean, openProp: boolean | undefined, onOpenChange: ((open: boolean) => void) | undefined, rootRef: React.RefObject, ) { /** True once the open gesture has finished and the pill is at full width. */ const [settled, setSettled] = React.useState(false) const [openState, setOpenState] = React.useState(false) const open = !collapsible || (openProp ?? openState) const setOpen = React.useCallback( (next: boolean) => { setOpenState(next) onOpenChange?.(next) }, [onOpenChange], ) // The user asked for the bar, so the bar takes the caret. Without this the // expand costs a second click before anything can be typed. React.useEffect(() => { if (!collapsible || !open) return rootRef.current?.querySelector("textarea")?.focus() }, [collapsible, open, rootRef]) // Closing arms the next open gesture: the pill has to start clipped again or // it would appear at full width inside the collapsed frame. React.useEffect(() => { if (!open) setSettled(false) }, [open]) return { open, setOpen, settled, setSettled } } // ── Sub-components ──────────────────────────────────────────────────────────── function HistoryButton(props: { label: string icon: string tooltip: React.ReactNode disabled: boolean onClick: () => void }) { return (