|
| 1 | +"use client"; |
| 2 | +import { useState, type ButtonHTMLAttributes } from "react"; |
| 3 | +import type { ApprovalQuestion, ChatError, ChatItem, ChatPhase, ChatStep, ChoiceQuestion, InputQuestion } from "@base44/platform/react"; |
| 4 | + |
| 5 | +// Tiny's look. The hook has no parts API, so Chat.tsx calls these itself; a question part gets |
| 6 | +// the question with its bound actions. |
| 7 | + |
| 8 | +// Stands in for Tiny's shadcn Button, so the example has no UI dependency. |
| 9 | +export function Button({ variant = "default", className = "", ...props }: ButtonHTMLAttributes<HTMLButtonElement> & { variant?: "default" | "outline" | "ghost" }) { |
| 10 | + const look = { default: "bg-primary text-primary-foreground", outline: "border", ghost: "" }[variant]; |
| 11 | + return <button {...props} data-variant={variant} className={`rounded-md px-2.5 py-1 text-sm ${look} ${className}`} />; |
| 12 | +} |
| 13 | + |
| 14 | +export function Text({ text, role }: { text: string; role: ChatItem["role"] }) { |
| 15 | + return <p className={role === "user" ? "ml-auto max-w-[80%] rounded-2xl bg-muted px-4 py-2 whitespace-pre-wrap" : "whitespace-pre-wrap"}>{text}</p>; |
| 16 | +} |
| 17 | + |
| 18 | +const stepIcons = { running: "…", waiting: "?", done: "✓", error: "✗" }; |
| 19 | + |
| 20 | +export function Step({ label, status }: ChatStep) { |
| 21 | + return <p className="text-xs text-muted-foreground">{stepIcons[status]} {label}</p>; |
| 22 | +} |
| 23 | + |
| 24 | +export function Choice({ questions, answer, decline }: ChoiceQuestion) { |
| 25 | + // One list of picked labels per question, in the same order as `questions`. |
| 26 | + const [picked, setPicked] = useState<string[][]>(questions.map(() => [])); |
| 27 | + function toggle(i: number, option: string, multi: boolean) { |
| 28 | + setPicked((all) => |
| 29 | + all.map((labels, j) => { |
| 30 | + if (j !== i) return labels; |
| 31 | + if (labels.includes(option)) return labels.filter((o) => o !== option); |
| 32 | + return multi ? [...labels, option] : [option]; |
| 33 | + }), |
| 34 | + ); |
| 35 | + } |
| 36 | + return ( |
| 37 | + <div className="flex flex-col gap-3 rounded-xl border p-3"> |
| 38 | + {questions.map((q, i) => ( |
| 39 | + <div key={i} className="flex flex-col gap-1.5"> |
| 40 | + <p className="font-medium">{q.text}</p> |
| 41 | + {q.description && <p className="text-xs text-muted-foreground">{q.description}</p>} |
| 42 | + <div className="flex flex-wrap gap-1"> |
| 43 | + {q.options.map((option) => ( |
| 44 | + <Button key={option} variant={picked[i].includes(option) ? "default" : "outline"} onClick={() => toggle(i, option, q.multi)}> |
| 45 | + {option} |
| 46 | + </Button> |
| 47 | + ))} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + ))} |
| 51 | + <div className="flex gap-2"> |
| 52 | + <Button onClick={() => answer(picked)}>Send answer</Button> |
| 53 | + <Button variant="ghost" onClick={decline}>Decline</Button> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +export function Secrets({ fields, submit, decline }: InputQuestion) { |
| 60 | + const [values, setValues] = useState<Record<string, string>>({}); |
| 61 | + return ( |
| 62 | + <div className="flex flex-col gap-2 rounded-xl border p-3"> |
| 63 | + {fields.map((field) => ( |
| 64 | + <input |
| 65 | + key={field.name} |
| 66 | + type="password" |
| 67 | + placeholder={field.name} |
| 68 | + title={field.description} |
| 69 | + onChange={(e) => setValues({ ...values, [field.name]: e.target.value })} |
| 70 | + className="rounded-md border px-2 py-1" |
| 71 | + /> |
| 72 | + ))} |
| 73 | + <div className="flex gap-2"> |
| 74 | + <Button onClick={() => submit(values)}>Save</Button> |
| 75 | + <Button variant="ghost" onClick={decline}>Decline</Button> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +export function Approval({ action, reason, approve, decline }: ApprovalQuestion) { |
| 82 | + return ( |
| 83 | + <div className="flex flex-col gap-2 rounded-xl border p-3"> |
| 84 | + <p className="font-medium">Allow {action}?</p> |
| 85 | + {reason && <p className="text-xs text-muted-foreground">{reason}</p>} |
| 86 | + <div className="flex gap-2"> |
| 87 | + <Button onClick={approve}>Approve</Button> |
| 88 | + <Button variant="ghost" onClick={decline}>Decline</Button> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +// The wording for each phase is the partner's, not the library's. |
| 95 | +const phaseText: Record<ChatPhase, string> = { |
| 96 | + idle: "", |
| 97 | + creating: "Creating app…", |
| 98 | + loading: "Loading the conversation…", |
| 99 | + waiting: "Waiting for your answer", |
| 100 | + building: "Building…", |
| 101 | +}; |
| 102 | + |
| 103 | +export function StatusLine({ phase, error, onDismiss }: { phase: ChatPhase; error: ChatError | null; onDismiss: () => void }) { |
| 104 | + if (error) { |
| 105 | + return ( |
| 106 | + <p className="text-xs text-destructive"> |
| 107 | + {error.message} <Button variant="ghost" onClick={onDismiss}>Dismiss</Button> |
| 108 | + </p> |
| 109 | + ); |
| 110 | + } |
| 111 | + return <p className="text-xs text-muted-foreground">{phaseText[phase]}</p>; |
| 112 | +} |
0 commit comments