import { useEffect, useRef, useState } from "react"; import { api } from "../api/client"; interface SavedArchive { filename: string; size: number; modified_at: string; } interface Props { onLoadDesk: (file: File) => void; onLoadSavedDesk: (filename: string) => void; } function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } function formatWhen(iso: string): string { try { return new Date(iso).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); } catch { return iso; } } export function LoadDeskMenu({ onLoadDesk, onLoadSavedDesk }: Props) { const [open, setOpen] = useState(false); const [archives, setArchives] = useState([]); const [savedDir, setSavedDir] = useState("saved/"); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const ref = useRef(null); const fileInputRef = useRef(null); useEffect(() => { if (!open) return; setLoading(true); setError(null); api.sessions.listSavedDesks() .then((res) => { setSavedDir(res.dir.replace(/^.*[/\\]/, "") + "/"); setArchives(res.archives); }) .catch((e) => setError((e as Error).message || "Couldn't list saved desks")) .finally(() => setLoading(false)); }, [open]); useEffect(() => { if (!open) return; function onDoc(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); } window.addEventListener("mousedown", onDoc); return () => window.removeEventListener("mousedown", onDoc); }, [open]); async function pickSaved(filename: string) { setOpen(false); await onLoadSavedDesk(filename); } return (
{ const f = e.target.files?.[0]; if (f) { setOpen(false); onLoadDesk(f); } e.target.value = ""; }} /> {open && (
{savedDir}
Desk archives saved in the repo (default folder)
{loading && (
Loading…
)} {error && (
{error}
)} {!loading && !error && archives.length === 0 && (
No .tar.gz files in saved/ yet. Use ā€œSave deskā€ on a panel, then copy the download here.
)} {!loading && archives.map((a) => ( ))}
)}
); }