import type { PluginTheme } from "@getpaseo/plugin"; import { useRpc, type PluginScreenProps, type PluginSidebarItemProps, type PluginWorkspacePanelProps, } from "@getpaseo/plugin/client"; import { Icon, TextInput, useToast } from "@getpaseo/plugin/client/react-native"; import { SidebarRow } from "@getpaseo/plugin/client/ui"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"; import { COMPONENTS, componentSignature, type ComponentGroup, type ComponentName } from "../shared/core/catalog"; const COMPONENT_COUNT = Object.keys(COMPONENTS).length; import { componentsIn, GALLERY_GROUPS, searchComponents } from "../shared/logic/gallery-search"; import { galleryGetRpc, galleryListRpc } from "../shared/rpc"; import { SpecBlockView } from "./live-ui"; import type { UiRuntime } from "./runtime"; const PLAYGROUND_SPEC = `$seats = 12 $plan = "Pro" price = $plan == "Pro" ? 29 : 12 Card("Interactive Pricing Calculator", [ Row([Choice($plan, ["Basic", "Pro"]), Slider($seats, 1, 50, label: "Seats")]), Row([ Metric("Per seat", "$" + fmt(price)), Metric("Monthly total", "$" + fmt(price * $seats), tone: "good") ]), Button("Select " + $plan + " (" + $seats + " seats)", Send("Selected " + $plan + " with " + $seats + " seats"), variant: "primary") ])`; function isComponent(name: string): name is ComponentName { return Object.hasOwn(COMPONENTS, name); } function componentOfFile(file: string): ComponentName | null { const prefix = file.replace(/\.ui$/, "").split("-")[0] ?? ""; return isComponent(prefix) ? prefix : null; } export function GallerySidebarItem({ currentScreen, openScreen }: PluginSidebarItemProps) { return ( openScreen({ screenId: "gallery" })} /> ); } export function GalleryScreen( props: PluginScreenProps & { playAudio: UiRuntime["playAudio"] }, ) { return ( ); } export function GalleryWorkspacePanel( props: PluginWorkspacePanelProps & { playAudio: UiRuntime["playAudio"] }, ) { return ( ); } function GalleryBody({ theme, compact, platform, playAudio, initialComponent, }: { theme: PluginTheme; compact: boolean; platform: "ios" | "android" | "web"; playAudio: UiRuntime["playAudio"]; initialComponent: string | null; }) { const listGallery = useRpc(galleryListRpc); const [files, setFiles] = useState([]); const [activeGroup, setActiveGroup] = useState("Layout"); const [selectedComponent, setSelectedComponent] = useState( initialComponent && isComponent(initialComponent) ? initialComponent : null, ); const [query, setQuery] = useState(""); const [mode, setMode] = useState<"browse" | "playground">("browse"); const [playgroundCode, setPlaygroundCode] = useState(PLAYGROUND_SPEC); useEffect(() => { let live = true; void listGallery({}).then((res) => { if (live) setFiles(res.names); }); return () => { live = false; }; }, [listGallery]); const byComponent = useMemo(() => { const map = new Map(); for (const f of files) { const comp = componentOfFile(f); if (!comp) continue; map.set(comp, [...(map.get(comp) ?? []), f]); } return map; }, [files]); const searchResults = useMemo(() => { if (!query.trim()) return null; return searchComponents(query, byComponent).map((m) => m.name); }, [byComponent, query]); const visibleComponents: ComponentName[] = useMemo(() => { if (selectedComponent) return [selectedComponent]; if (searchResults) return searchResults; return componentsIn(activeGroup); }, [activeGroup, searchResults, selectedComponent]); const toast = useToast(); const sendPreview = useCallback( async (msg: string) => { toast.show(`Would send: ${msg}`, { variant: "info" }); }, [toast], ); return ( Generative UI Gallery {COMPONENT_COUNT} components in 10 groups · {files.length} live examples {(["browse", "playground"] as const).map((tab) => { const active = mode === tab; return ( setMode(tab)} style={{ paddingHorizontal: 12, paddingVertical: 6, borderRadius: 8, backgroundColor: active ? theme.colors.accent : theme.colors.surface1, }} > {tab} ); })} {mode === "playground" ? ( Edit the ```ui spec below and interact with the live preview: ) : ( <> { setQuery(txt); if (txt.trim()) setSelectedComponent(null); }} placeholder={`Search ${COMPONENT_COUNT} components (e.g. Chart, Diagram, Map, Decision, Kanban)…`} placeholderTextColor={theme.colors.foregroundMuted} style={{ borderRadius: 8, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, backgroundColor: theme.colors.surface1, color: theme.colors.foreground, paddingHorizontal: 12, paddingVertical: 8, fontSize: 13, }} /> {GALLERY_GROUPS.map((group) => { const active = !selectedComponent && !query.trim() && activeGroup === group; return ( { setActiveGroup(group); setSelectedComponent(null); setQuery(""); }} style={{ paddingHorizontal: 10, paddingVertical: 6, borderRadius: 999, backgroundColor: active ? theme.colors.accent : theme.colors.surface1, borderWidth: StyleSheet.hairlineWidth, borderColor: active ? theme.colors.foregroundMuted : theme.colors.border, }} > {group} ({componentsIn(group).length}) ); })} {componentsIn(activeGroup).map((comp) => { const active = selectedComponent === comp; return ( setSelectedComponent(active ? null : comp)} style={{ paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: active ? theme.colors.surface2 : "transparent", borderWidth: StyleSheet.hairlineWidth, borderColor: active ? theme.colors.foregroundMuted : theme.colors.border, }} > {comp} ); })} {visibleComponents.map((comp) => ( ))} )} ); } function GalleryComponentSection({ name, files, theme, compact, platform, playAudio, send, }: { name: ComponentName; files: readonly string[]; theme: PluginTheme; compact: boolean; platform: "ios" | "android" | "web"; playAudio: UiRuntime["playAudio"]; send(text: string): Promise; }) { const [expandedAll, setExpandedAll] = useState(false); const shownFiles = expandedAll ? files : files.slice(0, 1); return ( {name} {componentSignature(name)} {files.length > 1 ? ( setExpandedAll((e) => !e)}> {expandedAll ? "Show primary example" : `Show all ${files.length} examples`} ) : null} {shownFiles.map((file) => ( ))} ); } function GalleryExampleCard({ file, theme, compact, platform, playAudio, send, }: { file: string; theme: PluginTheme; compact: boolean; platform: "ios" | "android" | "web"; playAudio: UiRuntime["playAudio"]; send(text: string): Promise; }) { const getSpec = useRpc(galleryGetRpc); const [source, setSource] = useState(null); useEffect(() => { let live = true; void getSpec({ name: file }).then((res) => { if (live) setSource(res.source); }); return () => { live = false; }; }, [file, getSpec]); if (source === null) { return ( Loading {file}… ); } return ( ); }