import { Signal, useSignal } from "@preact/signals"; import Button from "@/components/Button.tsx"; import FileInfo from "@/components/FileInfo.tsx"; import { CloseIcon, DownloadIcon } from "@/components/Icons.tsx"; import type { FileMetadata } from "@/src/file_crypto.ts"; import { download_blob } from "@/src/files.ts"; import { delete_file, download_file } from "@/src/files_api.ts"; export interface HubFile { id: string; uploaded_at: string; // Size of the encrypted file, shown when the metadata can't be read stored_size: number; // null when it can't be decrypted with this key metadata: FileMetadata | null; } const DATE_FORMAT = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }); interface HubFileListProps { files: Signal; } export default function HubFileList(props: HubFileListProps) { const busy = useSignal<{ id: string; action: "download" | "delete" } | null>(null); const error = useSignal(""); async function run(file: HubFile, action: "download" | "delete", fn: () => Promise) { if (busy.value) { return; } busy.value = { id: file.id, action }; error.value = ""; try { await fn(); } catch (err) { error.value = err instanceof Error ? err.message : String(err); } finally { busy.value = null; } } function download(file: HubFile) { const metadata = file.metadata; if (metadata) { run(file, "download", async () => download_blob(await download_file(file.id, metadata), metadata.name)); } } function remove(file: HubFile) { if (!confirm(`Delete "${file.metadata?.name ?? "this file"}"? This can't be undone.`)) { return; } run(file, "delete", async () => { const result = await delete_file(file.id); if (!result.ok && result.status !== 404) { throw new Error(result.error); } props.files.value = props.files.value.filter((other) => other.id !== file.id); }); } if (props.files.value.length === 0) { return (

Your files, encrypted

Only you can open what you store here. Add some files on the left to get started.

); } return (

Your files{" "} ({props.files.value.length})

{error.value && }
    {props.files.value.map((file) => (
  • remove(file)} > } /> {file.metadata && ( <>
    )}
  • ))}
); }