import { Icon, TextInput } from "@getpaseo/plugin/client/react-native";
import { InlineMarkdown } from "../markdown";
import React, { useState } from "react";
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native";
import { formMessage } from "../../shared/core/form";
import type { Action, Scalar, UiNode, Value } from "../../shared/core/types";
import {
blankRow,
cardOptions,
followUpPrompts,
parseNumberInput,
sameColor,
setCell,
swatchColors,
tableColumns,
tableRows,
togglePick,
} from "../../shared/logic/choose-logic";
import {
addItem,
itemText,
radioOptions,
stepBy,
stepperValue,
stepRange,
} from "../../shared/logic/fieldLogic";
import { resolveIconName } from "../../shared/logic/icons";
import {
finiteNumber,
formatNumber,
isAction,
isBinding,
isScalar,
list,
nodes,
optionalText,
text,
type Props,
} from "../../shared/logic/values";
import { ErrorLine, RenderNodes, type GroupedButton } from "../node-renderer";
import { propsKey, useUiRuntime } from "../runtime";
import { useItemFeedback } from "./feedback";
import { LoadedImageView } from "./content";
function FieldLabel({ label, value }: { label: string | null; value?: string }) {
const { theme } = useUiRuntime();
if (!label && !value) return null;
return (
{label ? (
{label}
) : (
)}
{value !== undefined ? (
{value}
) : null}
);
}
export function Slider({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const min = finiteNumber(props.min) ?? 0;
const max = Math.max(min, finiteNumber(props.max) ?? 100);
const step = Math.max(0.0001, finiteNumber(props.step) ?? 1);
const label = optionalText(props.label);
const current = Math.min(max, Math.max(min, finiteNumber(binding.value) ?? min));
const span = Math.max(0.0001, max - min);
const pct = Math.min(100, Math.max(0, ((current - min) / span) * 100));
const range = stepRange(min, max, step);
const ticks = [0, 0.25, 0.5, 0.75, 1].map((t) => {
const raw = min + t * span;
const snapped = Math.round((raw - min) / step) * step + min;
return Math.min(max, Math.max(min, Number(snapped.toFixed(6))));
});
return (
setValue(binding.name, stepBy(current, -1, range))}
style={{
width: 26,
height: 26,
borderRadius: 6,
backgroundColor: theme.colors.surface2,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
alignItems: "center",
justifyContent: "center",
}}
>
−
{
const locX = (e.nativeEvent as { locationX?: number }).locationX;
if (typeof locX === "number" && locX >= 0) {
const ratio = Math.min(1, Math.max(0, locX / 200));
const next = Math.round(((min + ratio * span) - min) / step) * step + min;
setValue(binding.name, Math.min(max, Math.max(min, Number(next.toFixed(6)))));
}
}}
>
setValue(binding.name, stepBy(current, 1, range))}
style={{
width: 26,
height: 26,
borderRadius: 6,
backgroundColor: theme.colors.surface2,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
alignItems: "center",
justifyContent: "center",
}}
>
+
{ticks.map((val, idx) => (
setValue(binding.name, val)}>
{formatNumber(val)}
))}
);
}
export function Stepper({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const range = stepRange(props.min, props.max, props.step);
const current = stepperValue(binding.value, range);
const label = optionalText(props.label);
return (
{label ? (
{label}
) : null}
setValue(binding.name, stepBy(current, -1, range))}
style={{
width: 28,
height: 28,
borderRadius: 6,
backgroundColor: theme.colors.surface2,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
alignItems: "center",
justifyContent: "center",
}}
>
−
{formatNumber(current)}
setValue(binding.name, stepBy(current, 1, range))}
style={{
width: 28,
height: 28,
borderRadius: 6,
backgroundColor: theme.colors.surface2,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
alignItems: "center",
justifyContent: "center",
}}
>
+
);
}
export function Toggle({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const checked = Boolean(binding.value);
const label = text(props.label);
return (
setValue(binding.name, !checked)}
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
gap: 12,
paddingVertical: 4,
}}
>
{label}
);
}
export function Choice({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const options = list(props.options).filter(isScalar);
const label = optionalText(props.label);
return (
{options.map((opt, idx) => {
const active = binding.value === opt;
return (
setValue(binding.name, opt)}
style={{
flex: 1,
minWidth: 72,
paddingHorizontal: 10,
paddingVertical: 6,
borderRadius: 6,
backgroundColor: active ? theme.colors.accent : "transparent",
alignItems: "center",
}}
>
{itemText(opt)}
);
})}
);
}
export function Radio({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const options = radioOptions(props.options);
const label = optionalText(props.label);
return (
{options.map((opt, idx) => {
const active = binding.value === opt.value;
return (
setValue(binding.name, opt.value)}
style={{
flexDirection: "row",
alignItems: "flex-start",
gap: 10,
padding: 10,
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface2,
}}
>
{active ? (
) : null}
{itemText(opt.value)}
{opt.detail ? (
) : null}
);
})}
);
}
export function Select({ props }: { props: Props }) {
return ;
}
export function Checklist({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const picked = Array.isArray(binding.value) ? binding.value : [];
const options = list(props.options).filter(isScalar);
const label = optionalText(props.label);
return (
{options.map((opt, idx) => {
const checked = picked.includes(opt);
return (
setValue(binding.name, togglePick(picked, opt, options))}
style={{
flexDirection: "row",
alignItems: "center",
gap: 10,
paddingHorizontal: 10,
paddingVertical: 8,
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface2,
}}
>
{itemText(opt)}
);
})}
);
}
export function ListEditor({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
const [draft, setDraft] = useState("");
if (!binding) return ;
const items = Array.isArray(binding.value) ? binding.value : [];
const label = optionalText(props.label);
const placeholder = optionalText(props.placeholder) ?? "Add item…";
const add = () => {
const next = addItem(items, draft);
if (next) {
setValue(binding.name, next);
setDraft("");
}
};
return (
{items.map((item, idx) => (
{itemText(item)}
setValue(binding.name, items.filter((_, i) => i !== idx))}
>
))}
Add
);
}
export function Input({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const label = optionalText(props.label);
const placeholder = optionalText(props.placeholder) ?? "";
return (
setValue(binding.name, next)}
placeholder={placeholder}
placeholderTextColor={theme.colors.foregroundMuted}
style={{
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface2,
color: theme.colors.foreground,
paddingHorizontal: 10,
paddingVertical: 8,
fontSize: 13,
}}
/>
);
}
export function TextArea({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const label = optionalText(props.label);
const placeholder = optionalText(props.placeholder) ?? "";
return (
setValue(binding.name, next)}
placeholder={placeholder}
placeholderTextColor={theme.colors.foregroundMuted}
style={{
minHeight: 72,
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface2,
color: theme.colors.foreground,
paddingHorizontal: 10,
paddingVertical: 8,
fontSize: 13,
textAlignVertical: "top",
}}
/>
);
}
export function DateInput({ props }: { props: Props }) {
return ;
}
export function Rating({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const max = Math.max(1, Math.min(10, Math.round(finiteNumber(props.max) ?? 5)));
const current = Math.round(finiteNumber(binding.value) ?? 0);
const label = optionalText(props.label);
return (
0 ? `${current}/${max}` : undefined} />
{Array.from({ length: max }, (_, idx) => {
const star = idx + 1;
const active = star <= current;
return (
setValue(binding.name, star === current ? 0 : star)}
accessibilityLabel={`${star} of ${max}`}
style={{ padding: 3 }}
>
);
})}
);
}
export function OptionCards({ props }: { props: Props }) {
const { theme, compact, setValue } = useUiRuntime();
const feedback = useItemFeedback(`options:${propsKey(props.options)}`, { comments: props.comments === true, editable: false });
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const options = cardOptions(props.options);
if (!options) return ;
const label = optionalText(props.label);
const isMulti = Array.isArray(binding.value);
const pickedList = isMulti ? (binding.value as readonly Value[]) : [];
const allValues = options.map((o) => o.value);
return (
{options.map((opt, idx) => {
const selected = isMulti ? pickedList.includes(opt.value) : binding.value === opt.value;
const iconName = opt.icon ? resolveIconName(opt.icon) ?? opt.icon : null;
return (
{
if (isMulti) {
setValue(binding.name, togglePick(pickedList, opt.value, allValues));
} else {
setValue(binding.name, opt.value);
}
}}
style={{ gap: 6 }}
>
{opt.image ? : null}
{iconName ? : null}
{opt.title}
{opt.detail ? (
) : null}
{feedback.controls(idx, `option "${opt.title}"`, {})}
);
})}
);
}
export function Swatches({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
const parsed = swatchColors(props.colors, (c) => /^#([0-9a-f]{3,8})$|^(rgb|hsl|oklch|[a-z]+)$/i.test(c));
if (!parsed) return ;
const label = optionalText(props.label);
return (
{parsed.colors.map((swatch, idx) => {
const active = binding ? sameColor(binding.value, swatch.value) : false;
return (
{
if (binding) setValue(binding.name, swatch.value);
}}
style={{
flexDirection: "row",
alignItems: "center",
gap: 6,
paddingHorizontal: 8,
paddingVertical: 5,
borderRadius: 8,
borderWidth: active ? 1.5 : StyleSheet.hairlineWidth,
borderColor: active ? theme.colors.foregroundMuted : theme.colors.border,
backgroundColor: theme.colors.surface2,
}}
>
{swatch.name ?? swatch.value}
);
})}
);
}
export function EditableTable({ props }: { props: Props }) {
const { theme, setValue } = useUiRuntime();
const binding = isBinding(props.value) ? props.value : null;
if (!binding) return ;
const rows = tableRows(binding.value);
if (rows === null) return ;
const columns = tableColumns(props.columns, rows);
const label = optionalText(props.label);
return (
{columns.map((col) => (
{col.label}
))}
{rows.map((row, rIdx) => (
{columns.map((col) => {
const cellVal = row[col.key] ?? null;
if (col.type === "boolean") {
return (
setValue(binding.name, setCell(rows, rIdx, col.key, !cellVal))}
style={{ width: 130, paddingHorizontal: 8, paddingVertical: 6 }}
>
);
}
return (
{
if (col.type === "number") {
const parsed = parseNumberInput(txt);
if (parsed !== undefined) {
setValue(binding.name, setCell(rows, rIdx, col.key, parsed));
}
} else {
setValue(binding.name, setCell(rows, rIdx, col.key, txt));
}
}}
style={{
color: theme.colors.foreground,
fontSize: 12.5,
paddingHorizontal: 6,
paddingVertical: 4,
borderRadius: 4,
backgroundColor: theme.colors.surface2,
}}
/>
);
})}
setValue(binding.name, rows.filter((_, i) => i !== rIdx))}
style={{ width: 36, alignItems: "center", justifyContent: "center" }}
>
))}
setValue(binding.name, [...rows, blankRow(columns)])}
style={{
alignSelf: "flex-start",
paddingHorizontal: 10,
paddingVertical: 5,
borderRadius: 6,
backgroundColor: theme.colors.surface2,
}}
>
+ Add row
);
}
export function Button({ props }: { props: Props }) {
const { theme, run, queued } = useUiRuntime();
const label = text(props.label);
const variant = text(props.variant) || "secondary";
const rawIcon = optionalText(props.icon);
const iconName = rawIcon ? resolveIconName(rawIcon) ?? rawIcon : null;
const actions: Action[] = Array.isArray(props.action)
? props.action.filter(isAction)
: isAction(props.action)
? [props.action]
: [{ kind: "action", type: "send", text: label }];
// A button whose actions only queue shows as pressed while its text is in the reply.
const queueTexts = actions.flatMap((action) => (action.type === "queue" ? [action.text] : []));
const isQueued = queueTexts.length > 0 && queueTexts.every((t) => queued(`queue:${t}`) !== null);
const bg =
variant === "primary"
? theme.colors.accent
: variant === "destructive"
? theme.colors.statusDanger
: theme.colors.surface2;
const fg =
variant === "primary" || variant === "destructive"
? theme.colors.accentForeground
: theme.colors.foreground;
return (
void run(actions)}
style={{
alignSelf: "flex-start",
flexDirection: "row",
alignItems: "center",
gap: 6,
paddingHorizontal: 12,
paddingVertical: 7,
borderRadius: 8,
backgroundColor: bg,
borderWidth: StyleSheet.hairlineWidth,
borderColor: variant === "secondary" ? theme.colors.border : bg,
}}
>
{isQueued ? : iconName ? : null}
{label}
);
}
export function ButtonGroup({ buttons }: { buttons: readonly GroupedButton[] }) {
return (
{buttons.map((b) => (
{b.element}
))}
);
}
export function Form({ props }: { props: Props }) {
const { theme, run, answer } = useUiRuntime();
const children = nodes(props.children);
const submitLabel = optionalText(props.submit) ?? "Submit";
const formNode: UiNode = { kind: "node", type: "Form", key: "form", props };
return (
{answer ? (
Submitted
) : (
{
const msg = formMessage(formNode);
if (msg.trim() !== "") {
void run([{ kind: "action", type: "send", text: msg }]);
}
}}
style={{
alignSelf: "flex-start",
paddingHorizontal: 14,
paddingVertical: 8,
borderRadius: 8,
backgroundColor: theme.colors.accent,
}}
>
{submitLabel}
)}
);
}
export function FollowUps({ props }: { props: Props }) {
const { theme, run } = useUiRuntime();
const prompts = followUpPrompts(props.prompts);
return (
{prompts.map((prompt, idx) => (
void run([{ kind: "action", type: "send", text: prompt }])}
style={{
paddingHorizontal: 10,
paddingVertical: 6,
borderRadius: 999,
backgroundColor: theme.colors.surface2,
borderWidth: StyleSheet.hairlineWidth,
borderColor: theme.colors.border,
}}
>
{prompt}
))}
);
}
/** A real checkbox: the box alone shows the state, so rows around it keep neutral borders. */
export function CheckBox({ checked }: { checked: boolean }) {
const { theme } = useUiRuntime();
return (
{checked ? : null}
);
}