Files hub frontend
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { useSignal, useSignalEffect } from "@preact/signals";
|
||||
|
||||
import HubFileList, { type HubFile } from "@/islands/HubFileList.tsx";
|
||||
import HubUpload from "@/islands/HubUpload.tsx";
|
||||
|
||||
function has_files(event: DragEvent) {
|
||||
return event.dataTransfer?.types.includes("Files") ?? false;
|
||||
}
|
||||
|
||||
export default function Hub() {
|
||||
const pending = useSignal<File[]>([]);
|
||||
const files = useSignal<HubFile[]>([]);
|
||||
const dragging = useSignal(false);
|
||||
|
||||
function add_files(new_files: File[]) {
|
||||
pending.value = [...pending.value, ...new_files];
|
||||
}
|
||||
|
||||
// TODO: encrypt with the master key and upload, the list only lives in memory until the API exists
|
||||
function upload() {
|
||||
const uploaded_at = Date.now();
|
||||
const uploaded = pending.value.map((file): HubFile => ({
|
||||
id: crypto.randomUUID(),
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
uploaded_at,
|
||||
blob: file,
|
||||
}));
|
||||
files.value = [...uploaded.reverse(), ...files.value];
|
||||
pending.value = [];
|
||||
}
|
||||
|
||||
// Files can be dropped anywhere on the page, not only on the upload area
|
||||
useSignalEffect(() => {
|
||||
function drag_over(event: DragEvent) {
|
||||
if (has_files(event)) {
|
||||
event.preventDefault();
|
||||
dragging.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
function drag_leave(event: DragEvent) {
|
||||
// relatedTarget is null once the drag leaves the window
|
||||
if (!event.relatedTarget) {
|
||||
dragging.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function drop(event: DragEvent) {
|
||||
dragging.value = false;
|
||||
if (has_files(event)) {
|
||||
event.preventDefault();
|
||||
add_files(Array.from(event.dataTransfer?.files ?? []));
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("dragover", drag_over);
|
||||
document.addEventListener("dragleave", drag_leave);
|
||||
document.addEventListener("drop", drop);
|
||||
return () => {
|
||||
document.removeEventListener("dragover", drag_over);
|
||||
document.removeEventListener("dragleave", drag_leave);
|
||||
document.removeEventListener("drop", drop);
|
||||
};
|
||||
});
|
||||
|
||||
return (
|
||||
<div class="flex flex-col gap-6 md:h-[38rem] md:flex-row md:gap-8">
|
||||
<div class="flex min-h-0 flex-col md:w-1/2">
|
||||
<HubUpload pending={pending} dragging={dragging.value} on_add={add_files} on_upload={upload} />
|
||||
</div>
|
||||
<div class="flex min-h-0 flex-col md:w-1/2">
|
||||
<HubFileList files={files} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { Signal } from "@preact/signals";
|
||||
|
||||
import Button from "@/components/Button.tsx";
|
||||
import FileInfo from "@/components/FileInfo.tsx";
|
||||
import { CloseIcon, DownloadIcon } from "@/components/Icons.tsx";
|
||||
import { download_blob } from "@/src/files.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;
|
||||
}
|
||||
|
||||
const DATE_FORMAT = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" });
|
||||
|
||||
interface HubFileListProps {
|
||||
files: Signal<HubFile[]>;
|
||||
}
|
||||
|
||||
export default function HubFileList(props: HubFileListProps) {
|
||||
function remove(file: HubFile) {
|
||||
props.files.value = props.files.value.filter((other) => other.id !== file.id);
|
||||
}
|
||||
|
||||
if (props.files.value.length === 0) {
|
||||
return (
|
||||
<div class="flex h-full w-full flex-col justify-center py-6 md:px-6">
|
||||
<h1 class="text-3xl font-bold md:pb-2">Your files, encrypted</h1>
|
||||
<p class="mt-6 max-w-sm leading-loose text-gray-800 md:mt-2 dark:text-gray-400">
|
||||
Only you can open what you store here. Add some files on the left to get started.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div class="flex h-full min-h-0 w-full flex-col">
|
||||
<h1 class="flex-none pb-2 text-lg font-bold">
|
||||
Your files{" "}
|
||||
<span class="font-normal text-gray-500 dark:text-gray-400">({props.files.value.length})</span>
|
||||
</h1>
|
||||
<ul class="-mx-2 flex min-h-0 flex-col gap-4 overflow-y-auto p-2">
|
||||
{props.files.value.map((file) => (
|
||||
<li
|
||||
key={file.id}
|
||||
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)}
|
||||
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}`}
|
||||
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>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import Button from "@/components/Button.tsx";
|
||||
import InputField from "@/components/InputField.tsx";
|
||||
import TextAreaField from "@/components/TextAreaField.tsx";
|
||||
import { post_json } from "@/src/client.ts";
|
||||
import { download_blob } from "@/src/files.ts";
|
||||
import { save_master_key } from "@/src/key_store.ts";
|
||||
import {
|
||||
export_key_text,
|
||||
@@ -53,13 +54,7 @@ export default function KeysCreate(props: KeysCreateProps) {
|
||||
}
|
||||
|
||||
function download_key_file() {
|
||||
const url = URL.createObjectURL(new Blob([key_text.value + "\n"], { type: "text/plain" }));
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = "vces-key.txt";
|
||||
link.click();
|
||||
// Revoking right away can cancel the download in some browsers
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
||||
download_blob(new Blob([key_text.value + "\n"], { type: "text/plain" }), "vces-key.txt");
|
||||
}
|
||||
|
||||
async function finish(event: SubmitEvent) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
import AskCode from "./AskCode.tsx";
|
||||
import Hub from "@/islands/Hub.tsx";
|
||||
import KeysNew from "@/islands/KeysNew.tsx";
|
||||
|
||||
export default function MainIsland() {
|
||||
@@ -11,7 +12,7 @@ export default function MainIsland() {
|
||||
} else if (page.value === "keysnew") {
|
||||
return <KeysNew page={page} />;
|
||||
} else if (page.value === "hub") {
|
||||
return <p>TODO: hub</p>;
|
||||
return <Hub />;
|
||||
} else {
|
||||
return "Error";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user