From fa86a80eab6fb00d8ac7fc6e84c33f93a11bd622 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 3 Jun 2025 10:49:01 +0200 Subject: [PATCH] guard conditions for menu items --- src/ui/mainMenu.js | 56 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/ui/mainMenu.js b/src/ui/mainMenu.js index e1ab8ae..fc3772a 100644 --- a/src/ui/mainMenu.js +++ b/src/ui/mainMenu.js @@ -35,35 +35,35 @@ export class MainMenu { await this.ui.askMultipleChoice("Select an option", [ { label: "Download and install Codex", - action: this.installMenu.show, + action: this.ifNotInstalled(this.installMenu.show), }, { label: "Start Codex node", - action: this.startCodex, + action: this.ifInstalled(this.ifNotRunning(this.startCodex)), }, { label: "Check node status", - action: this.nodeStatusMenu.showNodeStatus, + action: this.ifRunning(this.nodeStatusMenu.showNodeStatus), }, { label: "Upload a file", - action: this.dataMenu.performUpload, + action: this.ifRunning(this.dataMenu.performUpload), }, { label: "Download a file", - action: this.dataMenu.performDownload, + action: this.ifRunning(this.dataMenu.performDownload), }, { label: "Show local data", - action: this.dataMenu.showLocalData, + action: this.ifRunning(this.dataMenu.showLocalData), }, { label: "Stop Codex node", - action: this.stopCodex, + action: this.ifRunning(this.stopCodex), }, { label: "Uninstall Codex node", - action: this.installMenu.show, + action: this.ifInstalled(this.ifNotRunning(this.installMenu.show)), }, { label: "Submit feedback", @@ -97,4 +97,44 @@ export class MainMenu { this.ui.showErrorMessage(`Failed to stop Codex. "${exception}"`); } }; + + ifInstalled = async (call) => { + if (await this.isInstalled()) { + await call(); + } else { + this.ui.showInfoMessage("Codex is not yet installed."); + } + } + + ifNotInstalled = async (call) => { + if (!await this.isInstalled()) { + await call(); + } else { + this.ui.showInfoMessage("Codex is installed."); + } + } + + ifRunning = async (call) => { + if (await this.isRunning()) { + await call(); + } else { + this.ui.showInfoMessage("Codex is not yet running."); + } + }; + + ifNotRunning = async (call) => { + if (!await this.isRunning()) { + await call(); + } else { + this.ui.showInfoMessage("Codex is running."); + } + }; + + isInstalled = async () => { + return await this.installer.isCodexInstalled(); + }; + + isRunning = async () => { + return ((await this.processControl.getNumberOfCodexProcesses()) > 0); + }; }