import { Copy, X } from "lucide-react"; import { useEffect, useState, type ReactNode } from "react"; import { createPortal } from "react-dom"; import { useI18n } from "../i18n"; import { useToast } from "./Toast"; import { IN_APP_DOWNLOAD_GUIDE_EVENT, type InAppDownloadGuideDetail, } from "./messageStream/utils/downloadFile"; import { inAppBrowserName } from "../utils/inAppBrowser"; async function copyTextToClipboard(text: string): Promise { try { if ( typeof navigator !== "undefined" && navigator.clipboard && typeof navigator.clipboard.writeText === "function" ) { await navigator.clipboard.writeText(text); return true; } } catch { // fall through to legacy path } try { const ta = document.createElement("textarea"); ta.value = text; ta.setAttribute("readonly", ""); ta.style.position = "fixed"; ta.style.top = "0"; ta.style.left = "0"; ta.style.opacity = "0"; document.body.append(ta); ta.select(); const ok = document.execCommand("copy"); ta.remove(); return ok; } catch { return false; } } export function InAppDownloadGuideProvider({ children, }: { children: ReactNode; }) { const { t } = useI18n(); const { showToast } = useToast(); const [detail, setDetail] = useState(null); useEffect(() => { const onShow = (event: Event) => { const ce = event as CustomEvent; if (!ce.detail) return; setDetail(ce.detail); }; window.addEventListener(IN_APP_DOWNLOAD_GUIDE_EVENT, onShow); return () => window.removeEventListener(IN_APP_DOWNLOAD_GUIDE_EVENT, onShow); }, []); useEffect(() => { if (!detail) return; const onKey = (event: KeyboardEvent) => { if (event.key === "Escape") setDetail(null); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [detail]); const close = () => setDetail(null); const handleCopy = async () => { if (!detail) return; const ok = await copyTextToClipboard(detail.url); if (ok) { showToast(t("inAppDownloadCopied")); } else { showToast(t("inAppDownloadCopyFail"), "error"); } }; const browser = inAppBrowserName(); const intro = browser ? t("inAppDownloadIntroNamed").replace("{browser}", browser) : t("inAppDownloadIntro"); return ( <> {children} {detail ? createPortal(
event.stopPropagation()} >

{t("download")}

{t("inAppDownloadTitle")}

{intro}

  1. 1 {t("inAppDownloadStepCopy")}
  2. 2 {t("inAppDownloadStepOpen")}
  3. 3 {t("inAppDownloadStepDownload")}

{detail.url}

, document.body, ) : null} ); }