File uploading

This commit is contained in:
2026-10-29 17:59:34 -03:00
parent ec9bdb8e8e
commit b65d8efad8
18 changed files with 686 additions and 51 deletions
+67 -20
View File
@@ -1,17 +1,19 @@
import { Signal } from "@preact/signals";
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;
name: string;
size: number;
uploaded_at: number;
// TODO: replace with fetching and decrypting from the server once uploads exist
blob: Blob;
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" });
@@ -21,8 +23,42 @@ interface HubFileListProps {
}
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<void>) {
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) {
props.files.value = props.files.value.filter((other) => other.id !== file.id);
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) {
@@ -42,6 +78,7 @@ export default function HubFileList(props: HubFileListProps) {
Your files{" "}
<span class="font-normal text-gray-500 dark:text-gray-400">({props.files.value.length})</span>
</h1>
{error.value && <p role="alert" class="pb-2 text-error-600 dark:text-error-400">{error.value}</p>}
<ul class="-mx-2 flex min-h-0 flex-col gap-4 overflow-y-auto p-2">
{props.files.value.map((file) => (
<li
@@ -49,30 +86,40 @@ export default function HubFileList(props: HubFileListProps) {
class="flex flex-col rounded bg-white p-4 shadow-light dark:border dark:border-gray-700 dark:bg-gray-900"
>
<FileInfo
name={file.name}
size={file.size}
detail={DATE_FORMAT.format(file.uploaded_at)}
name={file.metadata?.name ?? "Unreadable file"}
size={file.metadata?.size ?? file.stored_size}
detail={file.metadata
? DATE_FORMAT.format(new Date(file.uploaded_at))
: "Can't be decrypted with your key"}
action={
<Button
variant="link"
class="flex-none self-start p-1 text-gray-500 hover:text-gray-700 focus:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 dark:focus:text-gray-200"
title="Delete"
aria-label={`Delete ${file.name}`}
aria-label={`Delete ${file.metadata?.name ?? "unreadable file"}`}
disabled={busy.value !== null}
onClick={() => remove(file)}
>
<CloseIcon class="size-3.5" />
</Button>
}
/>
<hr class="my-4 w-full border-gray-200 dark:border-gray-700" />
<Button
variant="link"
class="flex items-center gap-2 self-start"
onClick={() => download_blob(file.blob, file.name)}
>
<DownloadIcon class="h-4 w-3" />
Download
</Button>
{file.metadata && (
<>
<hr class="my-4 w-full border-gray-200 dark:border-gray-700" />
<Button
variant="link"
class="flex items-center gap-2 self-start"
disabled={busy.value !== null}
onClick={() => download(file)}
>
<DownloadIcon class="h-4 w-3" />
{busy.value?.id === file.id && busy.value.action === "download"
? "Downloading..."
: "Download"}
</Button>
</>
)}
</li>
))}
</ul>