"use client"; /** * Content handed over by the share chooser: a small sessionStorage payload * the wizards pick up on mount. Files are rebuilt from base64 into real File * objects so the normal intake (validation, compression, parsing) runs. */ export type PendingShareFile = { name: string; type: string; data: string }; const KEY = "freezmo.share.pending.v1"; export function setPendingShare(payload: { text?: string; files?: PendingShareFile[] }): void { try { window.sessionStorage.setItem(KEY, JSON.stringify(payload)); } catch { /* private mode */ } } function base64ToBytes(base64: string): Uint8Array { const binary = atob(base64); const bytes = new Uint8Array(binary.length); for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index); return bytes; } /** Reads and clears the pending share; returns real File objects. */ export function takePendingShare(): { text: string; files: File[] } | null { try { const raw = window.sessionStorage.getItem(KEY); if (!raw) return null; window.sessionStorage.removeItem(KEY); const parsed = JSON.parse(raw) as { text?: string; files?: PendingShareFile[] }; const files = (parsed.files ?? []).map( (file) => new File([base64ToBytes(file.data) as unknown as BlobPart], file.name, { type: file.type }), ); return { text: parsed.text ?? "", files }; } catch { return null; } }