Skip to content

Commit fc376c8

Browse files
GiteaBotwxiaoguangsilverwind
authored
Refactor and fix archive link bug (#30535) (#30570)
Backport #30535 by wxiaoguang Regression of #29920 Fixes: #30569 Also this is a rewriting to eliminate the remaining jQuery usages from code. --------- Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent ea2ea8e commit fc376c8

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

web_src/js/features/repo-common.js

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
import $ from 'jquery';
22
import {hideElem, showElem} from '../utils/dom.js';
33
import {POST} from '../modules/fetch.js';
4+
import {showErrorToast} from '../modules/toast.js';
5+
import {sleep} from '../utils.js';
46

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');
813
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}`);
1817

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));
3021
}
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');
3328
}
3429
}
3530

3631
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+
}
4335
}
4436

4537
export function initRepoCloneLink() {

0 commit comments

Comments
 (0)