import { ChatMarkdown } from "@rakazo/chat-ui/native"; import { useEffect, useState } from "react"; import { Alert, Modal, Pressable, SafeAreaView, ScrollView, Text, View } from "react-native"; import { mobileTokens } from "../lib/appearance"; import { type MobileArtifactTarget, openMobileArtifact, readMobileArtifactText, } from "../lib/artifact-open"; import { useI18n } from "../lib/i18n"; import { useResolvedAppearance } from "../lib/native"; 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 { t } = useI18n(); const colorScheme = useResolvedAppearance(); const tokens = mobileTokens(); 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 : t("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( t("Could not share file"), error instanceof Error ? error.message : t("Try again."), ), ) } style={{ padding: 10 }} > {state.status === "loading" ? ( {t("Loading preview…")} ) : state.status === "error" ? ( {state.message} ) : ( {state.markdown} )} ); }