Files hub frontend
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { Signal, useComputed } from "@preact/signals";
|
||||
|
||||
import Button, { button_class } from "@/components/Button.tsx";
|
||||
import FileInfo from "@/components/FileInfo.tsx";
|
||||
import { CloseIcon, PlusIcon } from "@/components/Icons.tsx";
|
||||
import { format_bytes } from "@/src/files.ts";
|
||||
import { twMerge } from "@/src/tw.ts";
|
||||
|
||||
interface FilePickerProps {
|
||||
on_add: (files: File[]) => void;
|
||||
}
|
||||
|
||||
// Visually hidden, wrap it in a label to open the picker
|
||||
function FilePicker(props: FilePickerProps) {
|
||||
function change(event: Event) {
|
||||
const input = event.currentTarget as HTMLInputElement;
|
||||
const files = Array.from(input.files ?? []);
|
||||
input.value = "";
|
||||
if (files.length) {
|
||||
props.on_add(files);
|
||||
}
|
||||
}
|
||||
|
||||
return <input type="file" multiple class="sr-only" onChange={change} />;
|
||||
}
|
||||
|
||||
interface HubUploadProps {
|
||||
pending: Signal<File[]>;
|
||||
dragging: boolean;
|
||||
on_add: (files: File[]) => void;
|
||||
on_upload: () => void;
|
||||
}
|
||||
|
||||
export default function HubUpload(props: HubUploadProps) {
|
||||
const total_size = useComputed(() => props.pending.value.reduce((total, file) => total + file.size, 0));
|
||||
|
||||
function remove(file: File) {
|
||||
props.pending.value = props.pending.value.filter((pending_file) => pending_file !== file);
|
||||
}
|
||||
|
||||
if (props.pending.value.length === 0) {
|
||||
return (
|
||||
<label
|
||||
class={twMerge(
|
||||
"flex h-full w-full cursor-pointer flex-col items-center justify-center rounded border-2 border-dashed border-gray-900/20 px-6 py-16 has-[:focus-visible]:border-primary-500 dark:border-gray-600",
|
||||
props.dragging && "border-primary-500 bg-primary-500/5 dark:border-primary-400",
|
||||
)}
|
||||
>
|
||||
<FilePicker on_add={props.on_add} />
|
||||
<PlusIcon class="size-10 text-primary-600 dark:text-primary-400" />
|
||||
<p class="pt-6 pb-2 text-center text-lg font-bold tracking-wide">Drag and drop files</p>
|
||||
<p class="pb-6 text-center">or click to select them</p>
|
||||
<span class={button_class("primary", "mt-4")}>Select files</span>
|
||||
<p class="mx-12 my-6 max-w-sm text-center text-sm leading-loose text-gray-500 dark:text-gray-400">
|
||||
Files are encrypted in this browser before they're uploaded, the server never sees what's inside.
|
||||
</p>
|
||||
</label>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div class="flex h-full w-full flex-col">
|
||||
<ul
|
||||
class={twMerge(
|
||||
"min-h-0 shrink overflow-y-auto rounded-t bg-gray-100 px-6 py-4 dark:bg-black",
|
||||
props.dragging && "outline-2 outline-primary-500 outline-dashed dark:outline-primary-400",
|
||||
)}
|
||||
>
|
||||
{props.pending.value.toReversed().map((file, index) => (
|
||||
<li
|
||||
key={`${index}-${file.name}`}
|
||||
class="my-2 rounded bg-white p-3 shadow-light dark:border dark:border-gray-800 dark:bg-gray-900"
|
||||
>
|
||||
<FileInfo
|
||||
name={file.name}
|
||||
size={file.size}
|
||||
action={
|
||||
<Button
|
||||
variant="link"
|
||||
class="flex-none 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="Remove"
|
||||
aria-label={`Remove ${file.name}`}
|
||||
onClick={() => remove(file)}
|
||||
>
|
||||
<CloseIcon class="size-3.5" />
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div class="mb-1 flex flex-1 flex-row items-end justify-between gap-4 rounded-b bg-gray-100 p-6 font-medium dark:bg-black">
|
||||
<label class={button_class("link", "flex items-center gap-2 has-[:focus-visible]:underline")}>
|
||||
<FilePicker on_add={props.on_add} />
|
||||
<PlusIcon class="size-6" />
|
||||
Add files
|
||||
</label>
|
||||
<p class="text-sm font-normal text-gray-700 dark:text-gray-400">
|
||||
Total size: {format_bytes(total_size.value)}
|
||||
</p>
|
||||
</div>
|
||||
<Button variant="primary" class="mt-4 flex-none" onClick={props.on_upload}>Upload</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user