File uploading
This commit is contained in:
@@ -24,8 +24,45 @@ function FilePicker(props: FilePickerProps) {
|
||||
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;
|
||||
@@ -38,6 +75,12 @@ export default function HubUpload(props: HubUploadProps) {
|
||||
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
|
||||
@@ -54,6 +97,7 @@ export default function HubUpload(props: HubUploadProps) {
|
||||
<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>
|
||||
);
|
||||
}
|
||||
@@ -100,6 +144,7 @@ export default function HubUpload(props: HubUploadProps) {
|
||||
</p>
|
||||
</div>
|
||||
<Button variant="primary" class="mt-4 flex-none" onClick={props.on_upload}>Upload</Button>
|
||||
{error}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user