151 lines
5.1 KiB
TypeScript
151 lines
5.1 KiB
TypeScript
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} />;
|
|
}
|
|
|
|
export interface UploadState {
|
|
file: File;
|
|
index: number;
|
|
count: number;
|
|
// null while the file is being encrypted
|
|
progress: number | null;
|
|
controller: AbortController;
|
|
}
|
|
|
|
interface UploadingProps {
|
|
state: UploadState;
|
|
}
|
|
|
|
function Uploading({ state }: UploadingProps) {
|
|
const percent = Math.round((state.progress ?? 0) * 100);
|
|
return (
|
|
<div class="flex w-full flex-col rounded bg-white p-4 shadow-light dark:border dark:border-gray-700 dark:bg-gray-900">
|
|
<FileInfo
|
|
name={state.file.name}
|
|
size={state.file.size}
|
|
detail={state.count > 1 ? `${state.index + 1} of ${state.count}` : undefined}
|
|
/>
|
|
<p class="mt-4 text-sm font-medium text-primary-600 dark:text-primary-400">
|
|
{state.progress === null ? "Encrypting..." : `Uploading ${percent}%`}
|
|
</p>
|
|
<progress
|
|
class="my-3 h-1 w-full appearance-none overflow-hidden rounded bg-gray-200 dark:bg-gray-700 [&::-moz-progress-bar]:bg-primary-500 [&::-webkit-progress-bar]:bg-gray-200 dark:[&::-webkit-progress-bar]:bg-gray-700 [&::-webkit-progress-value]:bg-primary-500"
|
|
max={100}
|
|
value={state.progress === null ? undefined : percent}
|
|
/>
|
|
<Button variant="link" class="self-end" onClick={() => state.controller.abort()}>Cancel</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
interface HubUploadProps {
|
|
pending: Signal<File[]>;
|
|
upload_state: UploadState | null;
|
|
error: string;
|
|
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.upload_state) {
|
|
return <Uploading state={props.upload_state} />;
|
|
}
|
|
|
|
const error = props.error && <p role="alert" class="mt-4 text-error-600 dark:text-error-400">{props.error}</p>;
|
|
|
|
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>
|
|
{error}
|
|
</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>
|
|
{error}
|
|
</div>
|
|
);
|
|
}
|