diff --git a/components/FileInfo.tsx b/components/FileInfo.tsx new file mode 100644 index 0000000..719e426 --- /dev/null +++ b/components/FileInfo.tsx @@ -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 ( +
+ +
+

{props.name}

+

+ {format_bytes(props.size)} + {props.detail && ` ยท ${props.detail}`} +

+
+ {props.action} +
+ ); +} diff --git a/components/Icons.tsx b/components/Icons.tsx new file mode 100644 index 0000000..96ae1e4 --- /dev/null +++ b/components/Icons.tsx @@ -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 ( + + ); +} + +// The page is drawn in primary-400, the lines in currentColor so they can match the background +export function FileIcon(props: IconProps) { + return ( + + ); +} + +export function CloseIcon(props: IconProps) { + return ( + + ); +} + +export function DownloadIcon(props: IconProps) { + return ( + + ); +} diff --git a/deno.json b/deno.json index d419432..44c4bdd 100644 --- a/deno.json +++ b/deno.json @@ -16,7 +16,8 @@ } }, "exclude": [ - "**/_fresh/*" + "**/_fresh/*", + "__vces/" ], "imports": { "@/": "./", diff --git a/islands/Hub.tsx b/islands/Hub.tsx new file mode 100644 index 0000000..fa94e62 --- /dev/null +++ b/islands/Hub.tsx @@ -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([]); + const files = useSignal([]); + 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 ( +
+
+ +
+
+ +
+
+ ); +} diff --git a/islands/HubFileList.tsx b/islands/HubFileList.tsx new file mode 100644 index 0000000..ec2b258 --- /dev/null +++ b/islands/HubFileList.tsx @@ -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; +} + +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 ( +
+

Your files, encrypted

+

+ Only you can open what you store here. Add some files on the left to get started. +

+
+ ); + } + + return ( +
+

+ Your files{" "} + ({props.files.value.length}) +

+
    + {props.files.value.map((file) => ( +
  • + remove(file)} + > + + + } + /> +
    + +
  • + ))} +
+
+ ); +} diff --git a/islands/HubUpload.tsx b/islands/HubUpload.tsx new file mode 100644 index 0000000..34143f2 --- /dev/null +++ b/islands/HubUpload.tsx @@ -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 ; +} + +interface HubUploadProps { + pending: Signal; + 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 ( + + ); + } + + return ( +
+
    + {props.pending.value.toReversed().map((file, index) => ( +
  • + remove(file)} + > + + + } + /> +
  • + ))} +
+
+ +

+ Total size: {format_bytes(total_size.value)} +

+
+ +
+ ); +} diff --git a/islands/KeysCreate.tsx b/islands/KeysCreate.tsx index 050049d..3960e73 100644 --- a/islands/KeysCreate.tsx +++ b/islands/KeysCreate.tsx @@ -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) { diff --git a/islands/MainIsland.tsx b/islands/MainIsland.tsx index 04afdde..12b9450 100644 --- a/islands/MainIsland.tsx +++ b/islands/MainIsland.tsx @@ -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 ; } else if (page.value === "hub") { - return

TODO: hub

; + return ; } else { return "Error"; } diff --git a/src/files.ts b/src/files.ts new file mode 100644 index 0000000..296c406 --- /dev/null +++ b/src/files.ts @@ -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); +}