|
1 | 1 | import $ from 'jquery'; |
2 | 2 | import {hideElem, showElem} from '../utils/dom.js'; |
3 | 3 | import {POST} from '../modules/fetch.js'; |
| 4 | +import {showErrorToast} from '../modules/toast.js'; |
| 5 | +import {sleep} from '../utils.js'; |
4 | 6 |
|
5 | | -async function getArchive($target, url, first) { |
6 | | - const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn'); |
7 | | - |
| 7 | +async function onDownloadArchive(e) { |
| 8 | + e.preventDefault(); |
| 9 | + // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list |
| 10 | + const el = e.target.closest('a.archive-link[href]'); |
| 11 | + const targetLoading = el.closest('.ui.dropdown') ?? el; |
| 12 | + targetLoading.classList.add('is-loading', 'small-loading-icon'); |
8 | 13 | try { |
9 | | - dropdownBtn.classList.add('is-loading'); |
10 | | - 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 | | - } |
| 14 | + for (let tryCount = 0; ;tryCount++) { |
| 15 | + const response = await POST(el.href); |
| 16 | + if (!response.ok) throw new Error(`Invalid server response: ${response.status}`); |
18 | 17 |
|
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 | | - } |
| 18 | + const data = await response.json(); |
| 19 | + if (data.complete) break; |
| 20 | + await sleep(Math.min((tryCount + 1) * 750, 2000)); |
30 | 21 | } |
31 | | - } catch { |
32 | | - dropdownBtn.classList.remove('is-loading'); |
| 22 | + window.location.href = el.href; // the archive is ready, start real downloading |
| 23 | + } catch (e) { |
| 24 | + console.error(e); |
| 25 | + showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500}); |
| 26 | + } finally { |
| 27 | + targetLoading.classList.remove('is-loading', 'small-loading-icon'); |
33 | 28 | } |
34 | 29 | } |
35 | 30 |
|
36 | 31 | 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); |
42 | | - }); |
| 32 | + for (const el of document.querySelectorAll('a.archive-link[href]')) { |
| 33 | + el.addEventListener('click', onDownloadArchive); |
| 34 | + } |
43 | 35 | } |
44 | 36 |
|
45 | 37 | export function initRepoCloneLink() { |
|
0 commit comments