import { Icon as PaseoIcon, Modal, copyText, useToast } from "@getpaseo/plugin/client/react-native"; import { ImageLightbox } from "./image-lightbox"; import { ExternalLink } from "@getpaseo/plugin/client/ui"; import React, { useContext, useEffect, useState } from "react"; import { Image as RNImage, Platform, Pressable, StyleSheet, Text as RNText, View } from "react-native"; import { keyValuePairs } from "../../shared/logic/content-model"; import { parseListRows } from "../../shared/logic/explain-logic"; import { resolveIconName } from "../../shared/logic/icons"; import { entityKind, imageRef, inlineFacts, sourceLinks } from "../../shared/logic/references-model"; import { finiteNumber, isNode, list, optionalText, text, tone, type Props } from "../../shared/logic/values"; import { InlineMarkdown, MarkdownText } from "../markdown"; import { ErrorLine, RenderNode } from "../node-renderer"; import { InTableContext, propsKey, toneColor, toneMutedColor, useUiRuntime, type ImageUrl } from "../runtime"; import { useItemFeedback } from "./feedback"; export function Text({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const content = text(props.text); const t = tone(props.tone); const color = t === "neutral" ? theme.colors.foreground : toneColor(theme, t); return ; } export function Callout({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const body = text(props.text); const title = optionalText(props.title); const t = tone(props.tone); return ( {t === "neutral" ? null : ( )} {title ? ( {title} ) : null} ); } const CALLOUT_ICONS = { good: "CircleCheck", warn: "AlertTriangle", bad: "CircleX", info: "Info" } as const; export function Draft({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const toast = useToast(); const [source, setSource] = useState(false); const body = text(props.markdown); const title = optionalText(props.title); const action = (label: string, onPress: () => void, active = false) => ( {label} ); return ( {title ?? "Draft"} {action("Source", () => setSource((value) => !value), source)} {action("Copy", () => { void copyText(body).then( () => toast.show("Copied", { variant: "success" }), () => toast.error("Could not copy"), ); })} {source ? ( {body} ) : ( )} ); } export function Metric({ props }: { props: Props }) { const { theme } = useUiRuntime(); const label = text(props.label); const value = text(props.value); const note = optionalText(props.note); const t = tone(props.tone); return ( {label} {value} {note ? ( {note} ) : null} ); } export function Badge({ props }: { props: Props }) { const { theme } = useUiRuntime(); const inTable = useContext(InTableContext); const label = text(props.text); const t = tone(props.tone); const color = t === "neutral" ? theme.colors.foregroundMuted : toneColor(theme, t); return ( {label} ); } export function Icon({ props }: { props: Props }) { const { theme } = useUiRuntime(); const rawName = text(props.name); const resolved = resolveIconName(rawName) ?? rawName; const t = tone(props.tone); return ; } export function KeyValue({ props }: { props: Props }) { const { theme } = useUiRuntime(); const pairs = keyValuePairs(props.pairs); if (pairs === null) return ; return ( {pairs.map(([k, v], idx) => ( {k} {isNode(v) ? ( ) : ( {text(v)} )} ))} ); } export function LoadedImageView({ src, alt, height = 200, zoomable = true, }: { src: string; alt?: string | null; height?: number; /** Tapping opens the image full size; off where the image sits inside another tap target. */ zoomable?: boolean; }) { const { theme, loadImage } = useUiRuntime(); const [state, setState] = useState(null); const [zoomed, setZoomed] = useState(false); useEffect(() => { if (!loadImage || !src) return; let live = true; void loadImage(src).then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [loadImage, src]); if (!src) return ; if (state === null) { return ( Loading image… ); } if (!state.ok) { return ( {state.error} ({src}) ); } const picture = (frameHeight: number) => ( ); return ( {zoomable ? ( setZoomed(true)}> {picture(height)} ) : ( picture(height) )} {alt ? ( {alt} ) : null} {zoomed ? setZoomed(false)} /> : null} ); } export function Image({ props }: { props: Props }) { const src = text(props.src); const alt = optionalText(props.alt); return ; } export function File({ props }: { props: Props }) { const { theme, openFile } = useUiRuntime(); const filePath = text(props.path); const line = finiteNumber(props.line); const label = line !== null ? `${filePath}:${Math.round(line)}` : filePath; const onWeb = Platform.OS === "web"; const chip = ( { if (!onWeb && openFile) void openFile(filePath, line ?? undefined); }} style={{ alignSelf: "flex-start", flexDirection: "row", alignItems: "center", gap: 6, paddingHorizontal: 8, paddingVertical: 4, borderRadius: 6, backgroundColor: theme.colors.surface2, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, }} > {label} ); if (!onWeb) return chip; // Paseo renders chat file links as ``. Plugins such as // wiki-viewer catch clicks on those and replay the click when they decline, so the // anchor's own handler is the fallback that opens a Paseo file tab. return React.createElement( "a", { href: label, style: { display: "contents" }, onClick: (event: { preventDefault(): void }) => { event.preventDefault(); if (openFile) void openFile(filePath, line ?? undefined); }, }, chip, ); } export function Entity({ props }: { props: Props }) { const { theme } = useUiRuntime(); const [open, setOpen] = useState(false); const name = text(props.name); const kind = entityKind(props.kind); const note = optionalText(props.note); const url = optionalText(props.url); const imgRef = imageRef(text(props.image)); const rawPairs = keyValuePairs(props.facts) ?? []; const previewFacts = inlineFacts(rawPairs, 4); return ( {imgRef.kind === "load" ? : null} {name} {kind} {note ? {note} : null} {previewFacts.length > 0 ? ( {previewFacts.map((fact, idx) => ( {fact.label} {fact.value} ))} {rawPairs.length > 4 ? ( setOpen(true)}> View all {rawPairs.length} facts ) : null} ) : null} {url && /^https?:\/\//i.test(url) ? ( {url} ) : null} {open ? ( {rawPairs.map(([label, val], idx) => ( {label} {text(val)} ))} ) : null} ); } export function Sources({ props }: { props: Props }) { const { theme } = useUiRuntime(); const links = sourceLinks(list(props.links)); return ( {links.map((item, idx) => item.ok ? ( [{item.number}] {item.title} {item.domain} {item.note ? ( {item.note} ) : null} ) : ( ), )} ); } export function List({ props }: { props: Props }) { const { theme, run } = useUiRuntime(); const rows = parseListRows(props.items); const feedback = useItemFeedback(`list:${propsKey(props.items)}`, { comments: props.comments === true, editable: props.editable === true, }); return ( {rows.map((item, idx) => { const iconName = item.icon ? resolveIconName(item.icon) ?? item.icon : null; const t = item.tone ?? "neutral"; const original = { title: item.title, detail: item.detail ?? "" }; const shown = feedback.fields(idx, original); const controls = feedback.controls(idx, `"${item.title}"`, original); const rowContent = ( {iconName ? ( ) : null} {shown.title} {shown.detail ? ( ) : null} {item.value !== null ? ( isNode(item.value) ? ( ) : ( {text(item.value)} ) ) : null} ); return ( {item.actions.length > 0 ? ( void run(item.actions)}> {rowContent} ) : ( rowContent )} {controls ? {controls} : null} ); })} ); }