feat(plugins): download several marketplace plugins at once (#7950)

Installing a marketplace plugin disabled every other Install and Update button until it finished. Each marketplace plugin now tracks its own download: others can be installed meanwhile, and the install pages of finished downloads follow one another. A second click on a plugin already on its way does nothing.

Refs #7911
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-04 14:03:58 +08:00
1 джерело 2cf5f5369f
коміт 34739e181d
3 files changed
+100 -21

No files matched your search

+8 -5
Переглянути файл
@@ -793,8 +793,8 @@ function InstalledDetails({ plugin }: { plugin: PluginInfo }) {
const locale = useI18n((s) => s.locale);
const t = usePluginT(plugin.id);
const busy = usePlugins((s) => !!s.busy[plugin.id]);
const installing = usePlugins((s) => s.installing);
const update = useMarketUpdate(plugin);
const updating = usePlugins((s) => !!update && !!s.downloading[update.id]);
const homepage = useHomepage(plugin);
const status = STATUS[plugin.status];
const [confirmUninstall, setConfirmUninstall] = useState(false);
@@ -970,7 +970,7 @@ function InstalledDetails({ plugin }: { plugin: PluginInfo }) {
{(update || plugin.previousVersion || busy) && (
<div className={styles.installedActions}>
{update && (
<Button variant="primary" disabled={busy || installing} onClick={() => void installFromMarket(update.id)}>
<Button variant="primary" disabled={busy || updating} onClick={() => void installFromMarket(update.id)}>
{tx`Update to ${update.version}`}
</Button>
)}
@@ -1229,7 +1229,7 @@ function MarketAction({
selected?: boolean;
onInstalled?: (id: string) => void;
}) {
const installing = usePlugins((s) => s.installing);
const downloading = usePlugins((s) => !!s.downloading[entry.id]);
const update = isUpdate(entry);
if (!entry.compatible) {
return compact ? <Badge tone="warning">{tx("Incompatible")}</Badge> : null;
@@ -1243,7 +1243,7 @@ function MarketAction({
size={compact ? "small" : "normal"}
variant={update || !compact || selected ? "primary" : "secondary"}
className={selected && !update ? styles.selectedInstall : undefined}
disabled={installing}
disabled={downloading}
aria-label={compact ? `${update ? tx("Update") : tx("Install")} ${entry.name}` : undefined}
onClick={(e) => {
e.stopPropagation();
@@ -1479,6 +1479,7 @@ function useSuggestions(market: readonly MarketPlugin[] | null): Map<string, str
export function PluginsPage({ initialTab = "marketplace" }: { initialTab?: Tab } = {}) {
const { plugins, loaded, loading, error, installing, catalog, catalogError, market, marketUrl, marketError } =
usePlugins();
const downloading = usePlugins((s) => Object.keys(s.downloading).length > 0);
const [tab, setTab] = useState<Tab>(initialTab);
const [collapsed, toggleGroup] = useCollapsed();
const [query, setQuery] = useState("");
@@ -1626,7 +1627,9 @@ export function PluginsPage({ initialTab = "marketplace" }: { initialTab?: Tab }
},
]}
/>
{(loading || installing) && <Spinner size={14} label={installing ? "Installing plugin" : "Loading plugins"} />}
{(loading || installing || downloading) && (
<Spinner size={14} label={installing || downloading ? "Installing plugin" : "Loading plugins"} />
)}
<IconButton icon="settings" title="Manage Plugins" aria-haspopup="menu" onClick={openGear} />
</div>
);
+52
Переглянути файл
@@ -0,0 +1,52 @@
import { afterEach, expect, it } from "vitest";
import { createMemoryPluginsBackend, setPluginsBackend, type PluginsBackend } from "../backend";
import { installFromMarket, usePlugins } from "../store";
afterEach(() => setPluginsBackend(null));
function deferred() {
let resolve!: (dir: string) => void;
const promise = new Promise<string>((r) => (resolve = r));
return { promise, resolve };
}
it("downloads several marketplace plugins at once and installs them one after another", async () => {
const downloads = new Map([
["acme.a", deferred()],
["acme.b", deferred()],
]);
const log: string[] = [];
let installs = 0;
const backend: PluginsBackend = {
...createMemoryPluginsBackend(),
marketDownload: (id) => {
log.push(`download ${id}`);
return downloads.get(id)!.promise;
},
previewInstall: async (dir) => {
log.push(`preview ${dir}`);
expect(++installs).toBe(1);
return { id: dir, name: dir, version: "1.0.0", needsConsent: false, capabilities: [] } as never;
},
installDir: async (dir) => {
await new Promise((r) => setTimeout(r, 5));
installs--;
log.push(`install ${dir}`);
return { id: dir, name: dir, version: "1.0.0", status: "running" } as never;
},
};
setPluginsBackend(backend);
const a = installFromMarket("acme.a", { quiet: true });
const b = installFromMarket("acme.b", { quiet: true });
// A second click on a plugin already on its way does nothing.
expect(await installFromMarket("acme.a")).toBeNull();
expect(log).toEqual(["download acme.a", "download acme.b"]);
expect(usePlugins.getState().downloading).toEqual({ "acme.a": true, "acme.b": true });
downloads.get("acme.b")!.resolve("/dl/b");
downloads.get("acme.a")!.resolve("/dl/a");
expect(await Promise.all([a, b])).toEqual(["/dl/a", "/dl/b"]);
expect(log.slice(2)).toEqual(["preview /dl/b", "install /dl/b", "preview /dl/a", "install /dl/a"]);
expect(usePlugins.getState().downloading).toEqual({});
});
+40 -16
Переглянути файл
@@ -33,6 +33,8 @@ interface PluginsState {
/** Plugin ids with an operation in flight. */
busy: Readonly<Record<string, boolean>>;
installing: boolean;
/** Marketplace plugin ids being downloaded or installed: several download at once. */
downloading: Readonly<Record<string, boolean>>;
/** Locally available packages (Marketplace tab); `null` until loaded. */
catalog: CatalogPlugin[] | null;
catalogError: string | null;
@@ -52,6 +54,7 @@ export const usePlugins = create<PluginsState>(() => ({
error: null,
busy: {},
installing: false,
downloading: {},
catalog: null,
catalogError: null,
marketUrl: null,
@@ -282,25 +285,46 @@ export function isUpdate(entry: MarketPlugin): boolean {
return entry.compatible && !!entry.installed && compareVersions(entry.version, entry.installed) > 0;
}
/** Downloads (and checks) a marketplace plugin, then the install page as from disk. */
const setDownloading = (id: string, on: boolean) =>
usePlugins.setState((s) => {
const { [id]: _, ...rest } = s.downloading;
return { downloading: on ? { ...rest, [id]: true } : rest };
});
/** Install pages of finished downloads, one after another. */
let installQueue: Promise<unknown> = Promise.resolve();
function nextInstall<T>(install: () => Promise<T>): Promise<T> {
const run = installQueue.then(install, install);
installQueue = run.catch(() => undefined);
return run;
}
/**
* Downloads (and checks) a marketplace plugin, then the install page as from disk. Several
* plugins download at once; their install pages follow one another.
*/
export async function installFromMarket(id: string, options: InstallOptions = {}): Promise<string | null> {
usePlugins.setState({ installing: true });
let dir: string;
if (usePlugins.getState().downloading[id]) return null;
setDownloading(id, true);
try {
dir = await pluginsBackend().marketDownload(id);
} catch (e) {
usePlugins.setState({ installing: false });
notifications().notify({
group: "plugins",
level: "error",
title: "Could not download plugin",
body: errorText(e),
});
return null;
let dir: string;
try {
dir = await pluginsBackend().marketDownload(id);
} catch (e) {
notifications().notify({
group: "plugins",
level: "error",
title: "Could not download plugin",
body: errorText(e),
});
return null;
}
const installed = await nextInstall(() => installFromDisk(dir, false, options));
void refreshMarket(false);
return installed;
} finally {
setDownloading(id, false);
}
const installed = await installFromDisk(dir, false, options);
void refreshMarket(false);
return installed;
}
/** `plugins.installFromMarket`: the id or ids in `ids`, one install page after another. */