import { ChatMarkdown } from "@rakazo/chat-ui/native"; import { useEffect, useState } from "react"; import { Alert, Modal, Pressable, SafeAreaView, ScrollView, Text, View } from "react-native"; import { type MobileArtifactTarget, openMobileArtifact, readMobileArtifactText, } from "../lib/artifact-open"; import { NativeSymbol } from "./native-symbol"; export type MarkdownArtifactPreviewTarget = { artifactId: string; name: string; mimeType: string; }; export function MarkdownArtifactPreview({ threadTarget, target, onClose, }: { threadTarget: MobileArtifactTarget; target: MarkdownArtifactPreviewTarget; onClose: () => void; }) { const [state, setState] = useState< | { status: "loading" } | { status: "ready"; markdown: string } | { status: "error"; message: string } >({ status: "loading" }); const targetBotId = "botId" in threadTarget ? threadTarget.botId : undefined; const targetGroupId = "groupId" in threadTarget ? threadTarget.groupId : undefined; useEffect(() => { let cancelled = false; const requestTarget: MobileArtifactTarget = targetBotId !== undefined ? { botId: targetBotId } : { groupId: targetGroupId! }; void readMobileArtifactText(requestTarget, target.artifactId, target.mimeType) .then((markdown) => { if (!cancelled) setState({ status: "ready", markdown }); }) .catch((error) => { if (cancelled) return; setState({ status: "error", message: error instanceof Error ? error.message : "Could not load this file.", }); }); return () => { cancelled = true; }; }, [targetBotId, targetGroupId, target.artifactId, target.mimeType]); return ( {target.name} void openMobileArtifact( threadTarget, target.artifactId, target.name, target.mimeType, ).catch((error) => Alert.alert( "Could not share file", error instanceof Error ? error.message : "Try again.", ), ) } style={{ padding: 10 }} > {state.status === "loading" ? ( Loading preview… ) : state.status === "error" ? ( {state.message} ) : ( {state.markdown} )} ); }