Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/context-menu/TrayContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ export async function handleInstall(
}
}

export async function installLibrarySilently(app: any, libraryName: string): Promise<boolean> {
const normalized = normalizeLibraryName(libraryName);

const installPromise = requestAPI<any>('library/install', {
method: 'POST',
body: JSON.stringify({ libraryName: normalized })
});

Notification.promise(installPromise, {
pending: { message: `Installing ${libraryName} library...`, options: { autoClose: 3000 } },
success: { message: () => `Library ${libraryName} installed successfully.`, options: { autoClose: 3000 } },
error: { message: (err) => `Failed to install ${libraryName}: ${err}`, options: { autoClose: false } }
});

try {
const res = await installPromise;
if (res.status === 'OK') {
await app.commands.execute(commandIDs.refreshComponentList);
return true;
}
console.error(`Installation failed: ${res.error || 'Unknown error'}`);
return false;
} catch (e) {
console.error('Installation error:', e);
return false;
}
}

export interface TrayContextMenuProps {
app: any;
x: number;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/notificationEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function pathToLibraryId(rawPath?: string | null): string | null {
return normalizeLibraryName(m[1]);
}

async function loadLibraryIndex(): Promise<Map<string, LibraryEntry>> {
export async function loadLibraryIndex(): Promise<Map<string, LibraryEntry>> {
const res: any = await requestAPI('library/get_config', { method: 'GET' });
const libs = res?.config?.libraries;
if (!Array.isArray(libs)) throw new Error('Invalid library response');
Expand Down
58 changes: 31 additions & 27 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import type { Signal } from "@lumino/signaling";
import { commandIDs } from "./commands/CommandIDs";
import { IEditorTracker } from '@jupyterlab/fileeditor';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { handleInstall } from './context-menu/TrayContextMenu';

import { installLibrarySilently } from './context-menu/TrayContextMenu';
import { normalizeLibraryName } from './tray_library/ComponentLibraryConfig';
import { loadLibraryIndex } from './helpers/notificationEffects';
import { installComponentPreview } from './component_info_sidebar/previewHelper';
const FACTORY = 'Xircuits editor';

Expand Down Expand Up @@ -578,22 +579,17 @@ const xircuits: JupyterFrontEndPlugin<void> = {
});
}

async function getInstalledLibraries(): Promise<Set<string>> {
try {
const result = await requestAPI<any>('library/get_config', {
method: 'GET'
});

return new Set<string>(
result.config.libraries
.filter((lib: any) => lib.status === 'installed' && typeof lib.name === 'string')
.map((lib: any) => lib.name)
);
} catch (err) {
console.error('Failed to load library config via API:', err);
return new Set();
async function getInstalledIds(): Promise<Set<string>> {
const idx = await loadLibraryIndex();
const set = new Set<string>();
for (const [id, entry] of idx) {
if (String(entry.status).toLowerCase() === 'installed') {
set.add(id);
}
}
return set;
}

app.commands.addCommand(commandIDs.fetchExamples, {
label: 'Fetch Example Workflows',
caption: 'Fetch example workflows into the examples directory',
Expand All @@ -615,24 +611,32 @@ const xircuits: JupyterFrontEndPlugin<void> = {
icon: xircuitsIcon,
execute: async () => {
const currentPath = browserFactory.tracker.currentWidget?.model.path ?? '';
const installedLibs = await getInstalledLibraries();
const installedIds = await getInstalledIds();

for (const lib of libraries) {
if (installedLibs.has(lib)) {
console.log(`Library ${lib} already installed. Skipping.`);
continue;
}
const pairs = libraries.map(lib => ({
raw: lib,
id: normalizeLibraryName(lib)
}));

const ok = await handleInstall(app, lib, () =>
app.commands.execute(commandIDs.refreshComponentList)
);
const missing = pairs.filter(p => !installedIds.has(p.id));

if (missing.length) {
const list = missing.map(p => p.raw).join(', ');
const ok = window.confirm(`This workflow template requires the following component libraries: ${list}. Would you like to install them now?`);
if (!ok) {
console.warn('User cancelled installation.');
return;
}

for (const { raw, id } of missing) {
const ok = await installLibrarySilently(app, raw);
if (!ok) {
console.warn(`Aborted: ${lib} not installed.`);
console.warn(`Aborted: ${raw} not installed.`);
return;
}
installedLibs.add(lib);
installedIds.add(id);
}
}

// Currently the templates are stored at the `examples` dir
await app.commands.execute(commandIDs.fetchExamples);
Expand Down