/ src / ui / mainMenu.js
mainMenu.js
  1  export class MainMenu {
  2    constructor(
  3      uiService,
  4      menuLoop,
  5      installMenu,
  6      configMenu,
  7      installer,
  8      processControl,
  9      codexApp,
 10      dataMenu,
 11      nodeStatusMenu,
 12    ) {
 13      this.ui = uiService;
 14      this.loop = menuLoop;
 15      this.installMenu = installMenu;
 16      this.configMenu = configMenu;
 17      this.installer = installer;
 18      this.processControl = processControl;
 19      this.codexApp = codexApp;
 20      this.dataMenu = dataMenu;
 21      this.nodeStatusMenu = nodeStatusMenu;
 22  
 23      this.loop.initialize(this.promptMainMenu);
 24    }
 25  
 26    show = async () => {
 27      this.ui.showLogo();
 28  
 29      await this.loop.showLoop();
 30    };
 31  
 32    promptMainMenu = async () => {
 33      if ((await this.processControl.getNumberOfCodexProcesses()) > 0) {
 34        await this.showRunningMenu();
 35      } else {
 36        if (await this.installer.isCodexInstalled()) {
 37          await this.showNotRunningMenu();
 38        } else {
 39          await this.showNotInstalledMenu();
 40        }
 41      }
 42    };
 43  
 44    showNotInstalledMenu = async () => {
 45      await this.ui.askMultipleChoice("Codex is not installed", [
 46        {
 47          label: "Install Codex",
 48          action: this.installMenu.show,
 49        },
 50        {
 51          label: "Exit",
 52          action: this.loop.stopLoop,
 53        },
 54      ]);
 55    };
 56  
 57    showRunningMenu = async () => {
 58      await this.ui.askMultipleChoice("Codex is running", [
 59        {
 60          label: "Open Codex app",
 61          action: this.codexApp.openCodexApp,
 62        },
 63        {
 64          label: "Stop Codex",
 65          action: this.stopCodex,
 66        },
 67        {
 68          label: "Show node status",
 69          action: this.nodeStatusMenu.showNodeStatus,
 70        },
 71        {
 72          label: "Upload a file",
 73          action: this.dataMenu.performUpload,
 74        },
 75        {
 76          label: "Download a file",
 77          action: this.dataMenu.performDownload,
 78        },
 79        {
 80          label: "Show local data",
 81          action: this.dataMenu.showLocalData,
 82        },
 83        {
 84          label: "Exit (Codex keeps running)",
 85          action: this.loop.stopLoop,
 86        },
 87      ]);
 88    };
 89  
 90    showNotRunningMenu = async () => {
 91      await this.ui.askMultipleChoice("Codex is installed but not running", [
 92        {
 93          label: "Start Codex",
 94          action: this.startCodex,
 95        },
 96        {
 97          label: "Edit Codex config",
 98          action: this.configMenu.show,
 99        },
100        {
101          label: "Uninstall Codex",
102          action: this.installMenu.show,
103        },
104        {
105          label: "Exit",
106          action: this.loop.stopLoop,
107        },
108      ]);
109    };
110  
111    startCodex = async () => {
112      const spinner = this.ui.createAndStartSpinner("Starting...");
113      try {
114        await this.processControl.startCodexProcess();
115        this.ui.stopSpinnerSuccess(spinner);
116      } catch (exception) {
117        this.ui.stopSpinnerError(spinner);
118        this.ui.showErrorMessage(`Failed to start Codex. "${exception}"`);
119      }
120    };
121  
122    stopCodex = async () => {
123      const spinner = this.ui.createAndStartSpinner("Stopping...");
124      try {
125        await this.processControl.stopCodexProcess();
126        this.ui.stopSpinnerSuccess(spinner);
127      } catch (exception) {
128        this.ui.stopSpinnerError(spinner);
129        this.ui.showErrorMessage(`Failed to stop Codex. "${exception}"`);
130      }
131    };
132  }