diff --git a/apps/android/src/screens/bot/view-card.tsx b/apps/android/src/screens/bot/view-card.tsx new file mode 100644 index 0000000..68ba2a6 --- /dev/null +++ b/apps/android/src/screens/bot/view-card.tsx @@ -0,0 +1,147 @@ +import { useMemo, useRef, useState } from 'react' +import { PanResponder, ScrollView, Text, TextInput, View } from 'react-native' +import { Toggle } from '../../ui/toggle' +import { Minus, Plus } from 'lucide-react-native' +import { strings as s } from '../../locales/i18n' +import { object, objects, text, type BotObject } from '../../services/bot-catalog' +import { checkedBotView } from '../../services/bot-view' +import { Button, IconButton } from '../../ui/components' +import { NativeMarkdown } from '../../ui/markdown' +import { useTheme } from '../../ui/theme-context' +import { useThemedStyles } from '../../ui/use-themed-styles' +import { asText, choiceValue, inputDefault, resolve, validateViewAnswer, viewInputs, viewScope, type Scope } from '../../vendor/ds-bot/views.js' +import { BotChoice } from './controls' +import { createBotStyles } from './styles' +import { BotChart } from './view-chart' + +type Reply = (action: string, values: BotObject) => void + +/** Native components and a bounded expression interpreter; HTML never executes. */ +export function BotViewCard({ pending, busy, reply }: { pending: BotObject; busy: boolean; reply: Reply }) { + const styles = useThemedStyles(createBotStyles) + const blueprint = useMemo(() => checkedBotView(pending), [pending.view, pending.title]) + return + {!!text(pending.title) && {text(pending.title)}} + {blueprint ? : {s.bots.unknownCard}} + +} +function ViewContent({ view, busy, reply }: { view: BotObject; busy: boolean; reply: Reply }) { + const styles = useThemedStyles(createBotStyles) + const [inputs, setInputs] = useState({}) + const [error, setError] = useState(false) + const scope = viewScope(view, inputs) + const answer: Reply = (action, own) => { + if (busy) return + const values = viewInputs(view).length ? { ...own, inputs } : own + if (validateViewAnswer(view, action, values).error) { setError(true); return } + setError(false) + reply(action, values) + } + return + { setInputs(current => ({ ...current, [id]: value })); setError(false) }} /> + {error && {s.bots.invalidViewAnswer}} + +} +type NodeProps = { node: BotObject; busy: boolean; reply: Reply; scope: Scope; inputs: BotObject; change: (id: string, value: unknown) => void } +function ViewNode(props: NodeProps) { + const { node, busy, reply, scope, inputs, change } = props + const styles = useThemedStyles(createBotStyles) + const { colors } = useTheme() + const [selected, setSelected] = useState([]) + const [fields, setFields] = useState>({}) + const id = text(node.id) + const value = (item: unknown) => resolve(item, scope, '') + const display = (item: unknown) => asText(value(item)) + const input = Object.hasOwn(inputs, id) ? inputs[id] : inputDefault(node) + const resolvedTone = value(node.tone) + const tone = resolvedTone === 'bad' ? colors.danger : resolvedTone === 'warn' ? colors.warning : resolvedTone === 'good' ? colors.success : resolvedTone === 'muted' ? colors.muted : colors.ink + switch (node.type) { + // Rows wrap on narrow screens instead of compressing labels and controls. + case 'stack': case 'row': return {objects(node.children).map((child, index) => )} + case 'text': case 'callout': return {display(node.text)} + case 'metric': return {text(node.label)}{display(node.value)}{!!node.caption && {display(node.caption)}} + case 'progress': { + const percent = node.percent !== undefined ? Number(value(node.percent)) : Number(value(node.max)) > 0 ? Number(value(node.value)) / Number(value(node.max)) * 100 : 0 + const bounded = Number.isFinite(percent) ? Math.max(0, Math.min(100, percent)) : 0 + return {text(node.label)} · {Math.round(bounded)}%{node.valueLabel ? ` · ${display(node.valueLabel)}` : ''} + } + case 'table': return + {objects(node.columns).map((column, i) => {text(column.label) || text(column.key)})} + {(node.rows as unknown[][]).map((row, i) => {row.map((cell, j) => {display(cell)})})} + + case 'chart': return + case 'sources': return {objects(node.items).map((item, i) => {text(item.label)}`} />)} + case 'html': return {!!text(node.summary) && {text(node.summary)}}{s.bots.htmlFallback} + case 'slider': case 'number': return change(id, value)} /> + case 'toggle': return {text(node.label)} change(id, value)} trackColor={{ true: colors.primary }} /> + case 'choice': return {text(node.label)}{objects(node.options).map((option, i) => change(id, choiceValue(option))} />)} + case 'options': return + {objects(node.options).map((option, i) => { + const label = text(option.label) + return setSelected(current => node.multi === true ? current.includes(label) ? current.filter(item => item !== label) : [...current, label] : [label])} /> + })} +