"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Button, Spinner } from "@/components/ui"; import { useI18n } from "@/lib/i18n"; import { setPendingShare, type PendingShareFile } from "@/lib/share-pending"; import { openSettings } from "@/lib/ui-events"; type Payload = { title?: string; text?: string; url?: string; files: PendingShareFile[] }; /** * The share-target chooser: shows what was shared and lets the user send it * to New deck or Make my homework. Files are stashed in sessionStorage and * picked up by the wizard on mount. */ export function ShareChooser({ id, text, url, error, }: { id?: string; text?: string; url?: string; error?: string; }) { const { t } = useI18n(); const router = useRouter(); const [payload, setPayload] = useState(id ? null : { text, url, files: [] }); const [failed, setFailed] = useState(Boolean(error)); useEffect(() => { if (!id) return; let cancelled = false; void (async () => { try { const response = await fetch(`/share-target?id=${encodeURIComponent(id)}`, { cache: "no-store" }); if (!response.ok) throw new Error("expired"); const data = (await response.json()) as Payload; if (!cancelled) setPayload(data); } catch { if (!cancelled) setFailed(true); } })(); return () => { cancelled = true; }; }, [id]); const combinedText = [payload?.text, payload?.url].filter(Boolean).join("\n\n").trim(); const fileCount = payload?.files.length ?? 0; const go = (target: "/create" | "/homework") => { setPendingShare({ text: combinedText || undefined, files: payload?.files ?? [] }); router.push(target); }; if (failed) { return (

🧊

{t(error === "large" ? "share.tooLarge" : "share.expired")}

); } if (!payload) { return (
{t("share.loading")}
); } return (

{t("share.title")}

{t("share.intro")}

{t("share.preview")}

{fileCount > 0 ? (

{fileCount === 1 ? payload.files[0].name : `${fileCount} files`}

) : null} {combinedText ? (

{combinedText}

) : null} {fileCount === 0 && !combinedText ? (

{t("share.nothing")}

) : null}
); }