Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download progress indicator #598 #1677

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

verheyenkoen
Copy link
Contributor

@verheyenkoen verheyenkoen commented Jul 30, 2024

Implementation details described here.

This PR includes quite a few more things than described in #598.

  • Update of bootstrap.native
  • Fixed toast the-Bootstrap-way so we don't need htmx-ext-remove-me any more for hiding it. Also some CSS fixes (also removed hacks that are not needed in Bootstrap 5)
  • Fixed popover the-Bootstrap-way
  • Fixes to modal error implementation (also extracted the <template> from layout to a Templ component)
  • Adding mechanism to intercept downloads and display a progress toast (just a spinner, no actual progress).
  • The two new script files are Typescript, which worked out-of-the-box in esbuild.

FIXES #598

@verheyenkoen verheyenkoen self-assigned this Jul 30, 2024
Comment on lines 6 to 40
e.preventDefault();

const flashMessage = showFlashMessage({
isLoading: true,
text: "Preparing download...",
isDismissible: false,
toastOptions: {
autohide: false,
},
});

try {
const response = await fetch(anchor.href);
if (!response.ok) {
throw new Error(
"An unexpected error occurred while downloading the file. Please try again.",
);
}

const filename = extractFileName(response);
const blob = await response.blob();

const dummyAnchor = document.createElement("a");
dummyAnchor.href = URL.createObjectURL(blob);
dummyAnchor.setAttribute("class", "link-primary text-nowrap");
dummyAnchor.setAttribute("download", filename);
dummyAnchor.textContent = filename;

flashMessage?.setLevel("success");
flashMessage?.setIsLoading(false);
flashMessage?.setText("Download ready: " + dummyAnchor.outerHTML);
flashMessage?.setIsDismissible(true);

// Trigger download (save-as window or auto-download, depending on browser settings)
dummyAnchor.click();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a hack that's used widespread. The click on the <a> tag is prevented at the top of this function so we can intervene and display the toast first. Then the file is downloaded via the fetch() API. When ready, we create a dummy anchor tag using a data URL with the downloaded file. At the end, the click() method is triggered on that which will actually present the download to the browser. The dummy anchor is also included in the updated toast, but that is not necessary. Just an alternate way to get the file, in case you canceled the save-as-dialog.

Tested in Chrome, Firefox, Safari and Edge.

@verheyenkoen verheyenkoen marked this pull request as ready for review July 30, 2024 09:58
@mietcls
Copy link
Member

mietcls commented Aug 6, 2024

Discussion to be had:

  • TBC The goal of this issue is to find a case for large asynchronous tasks. This is a step in the direction to make sure you can continue doing other tasks and navigate away while executing large requests. Possible next steps:
    • Communication with server.
    • Making sure you can navigate away, having an indicator somewhere so you can return to your "scheduled downloads" (new issue needed).
  • When the download is rather small, it goes really fast it's not very useful to show the progress indication as it shows the same functionality as native browser functionality (progress, download indication, go to folder). A delay would be useful.

Useful steps taken in this PR that could be used in different contexts:

  • Better toast management: trigger client-side
  • Case to be found to keep this code, is useful

@mietcls mietcls marked this pull request as draft August 6, 2024 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

As a librarian, when I export metadata of records to Excel, I get visual feedback.
2 participants