aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-04-22 05:12:47 +0800
committerGitHub <noreply@github.com>2024-04-21 23:12:47 +0200
commitfc376c863aff1d138541156e6b4b612d96305906 (patch)
treea45a7403e869a55876b4c650913f0199d4a12002
parentea2ea8ef28b6a2207ec00bafaf42d428612d69eb (diff)
downloadgitea-fc376c863aff1d138541156e6b4b612d96305906.tar.gz
gitea-fc376c863aff1d138541156e6b4b612d96305906.zip
Refactor and fix archive link bug (#30535) (#30570)
Backport #30535 by wxiaoguang Regression of #29920 Fixes: https://github.com/go-gitea/gitea/issues/30569 Also this is a rewriting to eliminate the remaining jQuery usages from code. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
-rw-r--r--web_src/js/features/repo-common.js54
1 files changed, 23 insertions, 31 deletions
diff --git a/web_src/js/features/repo-common.js b/web_src/js/features/repo-common.js
index b750addb07..65d4e08069 100644
--- a/web_src/js/features/repo-common.js
+++ b/web_src/js/features/repo-common.js
@@ -1,45 +1,37 @@
import $ from 'jquery';
import {hideElem, showElem} from '../utils/dom.js';
import {POST} from '../modules/fetch.js';
+import {showErrorToast} from '../modules/toast.js';
+import {sleep} from '../utils.js';
-async function getArchive($target, url, first) {
- const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn');
-
+async function onDownloadArchive(e) {
+ e.preventDefault();
+ // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list
+ const el = e.target.closest('a.archive-link[href]');
+ const targetLoading = el.closest('.ui.dropdown') ?? el;
+ targetLoading.classList.add('is-loading', 'small-loading-icon');
try {
- dropdownBtn.classList.add('is-loading');
- const response = await POST(url);
- if (response.status === 200) {
- const data = await response.json();
- if (!data) {
- // XXX Shouldn't happen?
- dropdownBtn.classList.remove('is-loading');
- return;
- }
+ for (let tryCount = 0; ;tryCount++) {
+ const response = await POST(el.href);
+ if (!response.ok) throw new Error(`Invalid server response: ${response.status}`);
- if (!data.complete) {
- // Wait for only three quarters of a second initially, in case it's
- // quickly archived.
- setTimeout(() => {
- getArchive($target, url, false);
- }, first ? 750 : 2000);
- } else {
- // We don't need to continue checking.
- dropdownBtn.classList.remove('is-loading');
- window.location.href = url;
- }
+ const data = await response.json();
+ if (data.complete) break;
+ await sleep(Math.min((tryCount + 1) * 750, 2000));
}
- } catch {
- dropdownBtn.classList.remove('is-loading');
+ window.location.href = el.href; // the archive is ready, start real downloading
+ } catch (e) {
+ console.error(e);
+ showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
+ } finally {
+ targetLoading.classList.remove('is-loading', 'small-loading-icon');
}
}
export function initRepoArchiveLinks() {
- $('.archive-link').on('click', function (event) {
- event.preventDefault();
- const url = this.getAttribute('href');
- if (!url) return;
- getArchive($(event.target), url, true);
- });
+ for (const el of document.querySelectorAll('a.archive-link[href]')) {
+ el.addEventListener('click', onDownloadArchive);
+ }
}
export function initRepoCloneLink() {