// Comments, inline edits and "Other…" boxes. None of them sends anything: each // writes one entry into the chat's reply queue, which the next Send carries. import { Icon, TextInput } from "@getpaseo/plugin/client/react-native"; import React, { useState } from "react"; import { Pressable, StyleSheet, Text, View } from "react-native"; import { commentText, editText } from "../../shared/logic/reply"; import { optionalText, type Props } from "../../shared/logic/values"; import { usePersistedState, useUiRuntime } from "../runtime"; type Fields = Readonly>; interface ItemNote { readonly comment?: string; readonly edits?: Fields; } type Notes = Readonly>; function acceptNotes(stored: unknown): Notes | null { return typeof stored === "object" && stored !== null && !Array.isArray(stored) ? (stored as Notes) : null; } /** * Per-item comments and edits for a list component. `fields` gives an item's * text with the user's edits applied; `controls` renders its Comment and Edit * actions. With neither option on, both are no-ops. */ export function useItemFeedback(scope: string, options: { comments: boolean; editable: boolean }) { const [notes, setNotes] = usePersistedState(`feedback:${scope}`, {}, acceptNotes); const { setQueued } = useUiRuntime(); const update = (index: number, label: string, original: Fields, note: ItemNote) => { setNotes((previous) => ({ ...previous, [index]: note })); setQueued(`${scope}:${index}:comment`, note.comment ? commentText(label, note.comment) : null); setQueued(`${scope}:${index}:edit`, note.edits ? editText(label, original, note.edits) : null); }; return { fields(index: number, original: T): T { return { ...original, ...(notes[index]?.edits ?? {}) }; }, controls(index: number, label: string, original: Fields, leading?: React.ReactNode): React.ReactNode { if (!options.comments && !options.editable) return leading ?? null; return ( update(index, label, original, note)} /> ); }, }; } function ItemFeedback({ note, original, comments, editable, leading, onChange, }: { note: ItemNote; original: Fields; comments: boolean; editable: boolean; /** Shown first in the item's action row, as Findings' "Fix this". */ leading?: React.ReactNode; onChange(note: ItemNote): void; }) { const { theme, answer, superseded } = useUiRuntime(); const [mode, setMode] = useState<"idle" | "comment" | "edit">("idle"); const [draft, setDraft] = useState(""); const [edits, setEdits] = useState({}); const open = !answer && !superseded; const edited = note.edits !== undefined && Object.keys(original).some((field) => (note.edits?.[field] ?? original[field]) !== original[field]); if (mode === "comment") { return ( setDraft(next.comment ?? "")} onCancel={() => setMode("idle")} onSave={() => { const comment = draft.trim(); onChange({ ...note, comment: comment === "" ? undefined : comment }); setMode("idle"); }} /> ); } if (mode === "edit") { return ( setMode("idle")} onSave={() => { onChange({ ...note, edits }); setMode("idle"); }} /> ); } const startComment = () => { setDraft(note.comment ?? ""); setMode("comment"); }; return ( {note.comment ? ( {note.comment} {open ? ( onChange({ ...note, comment: undefined })}> ) : null} ) : null} {open ? ( {leading} {comments && !note.comment ? : null} {editable ? ( { setEdits({ ...original, ...(note.edits ?? {}) }); setMode("edit"); }} /> ) : null} {editable && edited ? onChange({ ...note, edits: undefined })} /> : null} ) : ( leading )} ); } function QuietAction({ icon, label, onPress }: { icon?: string; label: string; onPress(): void }) { const { theme } = useUiRuntime(); return ( {icon ? : null} {label} ); } /** One text box per field, with Save and Cancel. */ function Editor({ fields, multiline = false, placeholder, onChange, onSave, onCancel, }: { fields: Fields; multiline?: boolean; placeholder?: string; onChange(fields: Fields): void; onSave(): void; onCancel(): void; }) { const { theme } = useUiRuntime(); const many = Object.keys(fields).length > 1; return ( {Object.entries(fields).map(([field, value]) => ( onChange({ ...fields, [field]: next })} style={{ borderRadius: 6, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, backgroundColor: theme.colors.surface1, color: theme.colors.foreground, paddingHorizontal: 8, paddingVertical: 6, fontSize: 12.5, minHeight: multiline || field === "detail" ? 56 : undefined, }} /> ))} ); } function SmallButton({ label, primary = false, onPress }: { label: string; primary?: boolean; onPress(): void }) { const { theme } = useUiRuntime(); return ( {label} ); } /** "Other…": free-form feedback the agent did not offer a button for. */ export function Other({ props }: { props: Props }) { const { theme, run, setQueued, queued: queuedText, answer, superseded } = useUiRuntime(); const label = optionalText(props.label) ?? "Other…"; const placeholder = optionalText(props.placeholder) ?? "Tell the agent something else"; const [open, setOpen] = useState(false); const [draft, setDraft] = useState(""); const id = `other:${label}`; const queued = queuedText(id); if (answer || superseded) return null; // Open or queued, the box takes the whole line of its button group. if (queued !== null && !open) { return ( {queued} { setDraft(queued); setOpen(true); }} /> setQueued(id, null)} /> ); } if (!open) { return ( setOpen(true)} style={{ alignSelf: "flex-start", paddingHorizontal: 12, paddingVertical: 7, borderRadius: 8, backgroundColor: theme.colors.surface2, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, }} > {label} ); } const text = draft.trim(); return ( { if (text === "") return; setQueued(id, null); setOpen(false); void run([{ kind: "action", type: "send", text }]); }} /> { setQueued(id, text === "" ? null : text); setOpen(false); }} /> setOpen(false)} /> ); }