|
1 | 1 | import $ from 'jquery'; |
2 | | -import {hideElem, showElem} from '../utils/dom.js'; |
| 2 | +import {hideElem, queryElems, showElem} from '../utils/dom.js'; |
3 | 3 | import {POST} from '../modules/fetch.js'; |
| 4 | +import {showErrorToast} from '../modules/toast.js'; |
4 | 5 |
|
5 | | -async function getArchive($target, url, first) { |
6 | | - const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn'); |
7 | | - |
| 6 | +async function downloadArchive(target, url, retryCount = 0) { |
| 7 | + // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list |
| 8 | + const targetLoading = target.closest('.ui.dropdown') ?? target; |
| 9 | + let keepLoading = false; |
8 | 10 | try { |
9 | | - dropdownBtn.classList.add('is-loading'); |
| 11 | + targetLoading.classList.add('is-loading', 'loading-icon-2px'); |
10 | 12 | const response = await POST(url); |
11 | | - if (response.status === 200) { |
12 | | - const data = await response.json(); |
13 | | - if (!data) { |
14 | | - // XXX Shouldn't happen? |
15 | | - dropdownBtn.classList.remove('is-loading'); |
16 | | - return; |
17 | | - } |
18 | | - |
19 | | - if (!data.complete) { |
20 | | - // Wait for only three quarters of a second initially, in case it's |
21 | | - // quickly archived. |
22 | | - setTimeout(() => { |
23 | | - getArchive($target, url, false); |
24 | | - }, first ? 750 : 2000); |
25 | | - } else { |
26 | | - // We don't need to continue checking. |
27 | | - dropdownBtn.classList.remove('is-loading'); |
28 | | - window.location.href = url; |
29 | | - } |
| 13 | + if (!response.ok) { |
| 14 | + throw new Error(`Invalid server response: ${response.status}`); |
| 15 | + } |
| 16 | + const data = await response.json(); |
| 17 | + if (!data.complete) { |
| 18 | + keepLoading = true; // the archive is not ready yet, keep loading and then retry later |
| 19 | + const delay = Math.min((retryCount + 1) * 750, 2000); |
| 20 | + setTimeout(() => downloadArchive(target, url, retryCount + 1), delay); |
| 21 | + } else { |
| 22 | + window.location.href = url; // the archive is ready, start real downloading |
30 | 23 | } |
31 | | - } catch { |
32 | | - dropdownBtn.classList.remove('is-loading'); |
| 24 | + } catch (e) { |
| 25 | + console.error(e); |
| 26 | + showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500}); |
| 27 | + } finally { |
| 28 | + if (!keepLoading) targetLoading.classList.remove('is-loading', 'loading-icon-2px'); |
33 | 29 | } |
34 | 30 | } |
35 | 31 |
|
36 | 32 | export function initRepoArchiveLinks() { |
37 | | - $('.archive-link').on('click', function (event) { |
38 | | - event.preventDefault(); |
39 | | - const url = this.getAttribute('href'); |
40 | | - if (!url) return; |
41 | | - getArchive($(event.target), url, true); |
| 33 | + queryElems('.archive-link', (el) => { |
| 34 | + el.addEventListener('click', (e) => { |
| 35 | + const target = e.target.closest('.archive-link'); |
| 36 | + if (!target?.href) return; |
| 37 | + e.preventDefault(); |
| 38 | + downloadArchive(target, target.href); |
| 39 | + }); |
42 | 40 | }); |
43 | 41 | } |
44 | 42 |
|
|
0 commit comments