Files hub frontend
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { ComponentChildren } from "preact";
|
||||
|
||||
import { FileIcon } from "@/components/Icons.tsx";
|
||||
import { format_bytes } from "@/src/files.ts";
|
||||
|
||||
interface FileInfoProps {
|
||||
name: string;
|
||||
size: number;
|
||||
detail?: string;
|
||||
action?: ComponentChildren;
|
||||
}
|
||||
|
||||
export default function FileInfo(props: FileInfoProps) {
|
||||
return (
|
||||
<div class="flex w-full flex-row items-center gap-4">
|
||||
<FileIcon class="h-8 w-7 flex-none text-white dark:text-gray-900" />
|
||||
<div class="min-w-0 flex-1">
|
||||
<p class="font-medium break-all">{props.name}</p>
|
||||
<p class="pt-1 text-sm opacity-75">
|
||||
{format_bytes(props.size)}
|
||||
{props.detail && ` · ${props.detail}`}
|
||||
</p>
|
||||
</div>
|
||||
{props.action}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// SVG icons from Mozilla Send (__vces/send/assets), colored with currentColor
|
||||
|
||||
interface IconProps {
|
||||
class?: string;
|
||||
}
|
||||
|
||||
export function PlusIcon(props: IconProps) {
|
||||
return (
|
||||
<svg class={props.class} viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
||||
<path d="M12 22.667c5.891 0 10.667-4.776 10.667-10.667S17.89 1.333 12 1.333 1.333 6.11 1.333 12 6.11 22.667 12 22.667M12 24C5.373 24 0 18.627 0 12S5.373 0 12 0s12 5.373 12 12-5.373 12-12 12" />
|
||||
<path d="M11.143 6v5.143H6v1.714h5.143V18h1.714v-5.143H18v-1.714h-5.143V6z" />
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
// The page is drawn in primary-400, the lines in currentColor so they can match the background
|
||||
export function FileIcon(props: IconProps) {
|
||||
return (
|
||||
<svg class={props.class} viewBox="0 0 26 32" fill="none" aria-hidden="true">
|
||||
<path
|
||||
class="fill-primary-400"
|
||||
d="M21.848 31.665H2.89c-1.563 0-2.786-1.29-2.786-2.786V2.786C.104 1.223 1.395 0 2.89 0h11.824c.747 0 1.495.272 1.97.815l7.135 7.135c.544.544.816 1.224.816 1.97v18.959c0 1.495-1.223 2.786-2.786 2.786"
|
||||
/>
|
||||
<path
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
d="M24.303 10.647h-7.52a2.73 2.73 0 0 1-2.731-2.73V.395M7.283 8.939h1.636M7.283 15.78h10.258M7.283 22.62h10.258"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
export function CloseIcon(props: IconProps) {
|
||||
return (
|
||||
<svg class={props.class} viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
|
||||
<path d="M9.414 8l5.293-5.293a1 1 0 0 0-1.414-1.414L8 6.586 2.707 1.293a1 1 0 0 0-1.414 1.414L6.586 8l-5.293 5.293a1 1 0 1 0 1.414 1.414L8 9.414l5.293 5.293a1 1 0 0 0 1.414-1.414z" />
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
export function DownloadIcon(props: IconProps) {
|
||||
return (
|
||||
<svg class={props.class} viewBox="0 0 12 16" fill="currentColor" aria-hidden="true">
|
||||
<path d="M5.293 12.707a1 1 0 0 0 1.414 0l5-5a1 1 0 0 0-1.414-1.414L7 9.586V1a1 1 0 0 0-2 0v8.586L1.707 6.293A1 1 0 0 0 .293 7.707zM11 14H1a1 1 0 0 0 0 2h10a1 1 0 0 0 0-2" />
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
@@ -16,7 +16,8 @@
|
||||
}
|
||||
},
|
||||
"exclude": [
|
||||
"**/_fresh/*"
|
||||
"**/_fresh/*",
|
||||
"__vces/"
|
||||
],
|
||||
"imports": {
|
||||
"@/": "./",
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
const SIZE_UNITS = ["B", "KB", "MB", "GB", "TB"];
|
||||
|
||||
export function format_bytes(size: number) {
|
||||
if (size < 1) {
|
||||
return "0 B";
|
||||
}
|
||||
const exponent = Math.min(Math.floor(Math.log(size) / Math.log(1024)), SIZE_UNITS.length - 1);
|
||||
const value = size / 1024 ** exponent;
|
||||
return `${Number.isInteger(value) ? value : value.toFixed(1)} ${SIZE_UNITS[exponent]}`;
|
||||
}
|
||||
|
||||
export function download_blob(blob: Blob, file_name: string) {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = file_name;
|
||||
link.click();
|
||||
// Revoking right away can cancel the download in some browsers
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
||||
}
|
||||
Reference in New Issue
Block a user