/** PWA share target: Android's "Share" sheet opens /?share_title=…&share_text=…&share_url=… */ const SHARED = "codecraft.shared"; const PREFILL = "codecraft.prefill."; /** Move shared content from the URL into sessionStorage (survives the login screen). */ export function captureShare() { const q = new URLSearchParams(location.search); if (!q.has("share_title") && !q.has("share_text") && !q.has("share_url")) return; const parts = [q.get("share_title"), q.get("share_text"), q.get("share_url")].map((s) => (s ?? "").trim()).filter(Boolean); // Many apps put the URL into the text as well. const unique = parts.filter((p, i) => !parts.some((o, j) => j !== i && o.includes(p) && (o.length > p.length || j < i))); if (unique.length) sessionStorage.setItem(SHARED, unique.join("\n\n")); history.replaceState(null, "", "/" + location.hash); } export function pendingShare(): string | null { return sessionStorage.getItem(SHARED); } export function clearShare() { sessionStorage.removeItem(SHARED); } /** Text to put into a composer once ("new" = new-session wizard, else a session id). */ export function setPrefill(target: string, text: string) { sessionStorage.setItem(PREFILL + target, text); } export function takePrefill(target: string): string | null { const v = sessionStorage.getItem(PREFILL + target); // Removed a moment later: React StrictMode runs state initialisers twice. if (v !== null) setTimeout(() => sessionStorage.removeItem(PREFILL + target), 500); return v; }