import { useActionQuery } from "@agent-native/core/client/hooks"; import { useT } from "@agent-native/core/client/i18n"; import { IconAlertTriangle } from "@tabler/icons-react"; import { useQuery } from "@tanstack/react-query"; import { fetchEmailPreview, type EmailPreview, } from "../client/transactional-emails"; import { Alert, AlertDescription, AlertTitle } from "./ui/alert"; import { Skeleton } from "./ui/skeleton"; /** * The "core" app id means the email was read from Dispatch's own local * catalog (see transactional-email.tsx), not a cross-app fetch, so its * preview must render the same way — Dispatch always has the definition * registered locally and a cross-app fetch would have no real appPath to hit. */ function useEmailPreviewQuery(appId: string, appPath: string, id: string) { const isCore = appId === "core"; const local = useActionQuery( "render-transactional-email-preview", { id }, { enabled: isCore, retry: false }, ); const remote = useQuery({ queryKey: ["transactional-email-preview", appPath, id], queryFn: () => fetchEmailPreview(appPath, id), enabled: !isCore, retry: false, }); return isCore ? local : remote; } export function EmailPreviewPane({ appId, appPath, id, name, }: { appId: string; appPath: string; id: string; name: string; }) { const t = useT(); const preview = useEmailPreviewQuery(appId, appPath, id); if (preview.isError) { return ( {t("dispatch.transactionalEmail.previewFailed")} {preview.error instanceof Error ? preview.error.message : String(preview.error)} ); } if (preview.isLoading || !preview.data) { return ; } return (
{t("dispatch.transactionalEmail.subject")}
{preview.data.subject}
{/* sandbox="" (no allow-scripts) keeps arbitrary email HTML from running script in the Dispatch origin. */}