prevent uninstall multiple click (#567)

This commit is contained in:
ABevier 2023-05-05 22:57:34 -04:00 committed by GitHub
parent c50a0058b3
commit f0e1af6253
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 21 deletions

View file

@ -19,16 +19,40 @@
export let pkg: GUIPackage;
let installing = false;
let pruning = false;
let uninstalling = false;
const install = async (version: string) => {
if (installing) {
return;
}
try {
installing = true;
await packagesStore.installPkg(pkg, version);
} finally {
installing = false;
}
};
const uninstall = async () => {
if (uninstalling) {
return;
}
try {
uninstalling = true;
await packagesStore.uninstallPkg(pkg);
} finally {
uninstalling = false;
}
};
const prune = async () => {
pruning = true;
if (uninstalling) {
return;
}
try {
uninstalling = true;
const versions = (pkg?.installed_versions || []).sort((a, b) => semverCompare(b, a));
for (const [i, v] of versions.entries()) {
if (i) {
@ -40,7 +64,9 @@
}
}
}
pruning = false;
} finally {
uninstalling = false;
}
};
let copied = false;
@ -100,10 +126,8 @@
class="h-10"
type="plain"
color="blue"
onClick={async () => {
packagesStore.uninstallPkg(pkg);
}}
loading={pruning}
onClick={uninstall}
loading={uninstalling}
>
<div class="version-item flex w-full items-center justify-center gap-x-1 text-xs">
<div class="icon-trash" />
@ -125,7 +149,7 @@
type="plain"
color="blue"
onClick={prune}
loading={pruning}
loading={uninstalling}
>
<div class="version-item flex w-full items-center justify-center gap-x-1 text-xs">
<div class="icon-scissors" />

View file

@ -27,6 +27,7 @@ import withRetry from "$libs/utils/retry";
import log from "$libs/logger";
import { isPackageUpToDate } from "../packages/pkg-utils";
import withDelay from "$libs/utils/delay";
const packageRefreshInterval = 1000 * 60 * 60; // 1 hour
@ -254,11 +255,11 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
await deletePkg(pkg, v);
}
setTimeout(() => {
await withDelay(() => {
updatePackage(pkg.full_name, {
installed_versions: []
});
}, 3000);
}, 1000);
} catch (error) {
log.error(error);
notificationStore.add({

View file

@ -0,0 +1,5 @@
// withDelay adds a delay before calling the provided function.
export default async function withDelay<T>(f: () => T, delayMs: number) {
await new Promise((resolve) => setTimeout(resolve, delayMs));
return f();
}