Skip to content

Commit

Permalink
feat: add experimental download progress toast
Browse files Browse the repository at this point in the history
  • Loading branch information
trobonox committed Aug 14, 2023
1 parent c37e407 commit 2b6fbf5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/utils/files.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { auth } from "@/stores/auth";
import type { UploadedFile } from "@/types/api";
import { createSignal } from "solid-js";
import toast from "solid-toast";

export const createFileImporter = (onFileUploaded: (files: FileList) => unknown) => {
const input = document.createElement("input");
Expand Down Expand Up @@ -78,13 +80,25 @@ export const getUploadedFileURL = (file: UploadedFile) => {
return url;
};

const [downloadProgress, setDownloadProgress] = createSignal(0);

export const downloadUploadedFile = (file: UploadedFile) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", "/api/file/" + file.id, true);
xhr.setRequestHeader("authorization", auth.profile!.api_token);

xhr.responseType = "blob";

toast.custom(() => (
<div
class="relative flex flex-col gap-2 rounded-md bg-base px-6 py-3 pr-12 font-medium shadow-md">
Downloading... {downloadProgress()}%
<progress value={downloadProgress()} max={100} class="bg-lavender" />
</div>
), {
unmountDelay: 0
});

xhr.onload = () => {
if (xhr.status === 200) {
const blob = xhr.response;
Expand All @@ -107,6 +121,11 @@ export const downloadUploadedFile = (file: UploadedFile) => {
xhr.onprogress = (event) => {
if (event.lengthComputable) {
console.info(file.name, `${event.loaded}/${event.total} (${event.loaded * 100 / event.total}%)`);
setDownloadProgress(Math.floor(event.loaded * 100 / event.total));

if (event.loaded === 1) {
toast.dismiss();
}
}
};

Expand Down

0 comments on commit 2b6fbf5

Please sign in to comment.