145 lines
3.3 KiB
JavaScript
Raw Normal View History

import { Codex } from "@codex-storage/sdk-js";
import { NodeUploadStategy } from "@codex-storage/sdk-js/node";
2025-02-25 13:59:26 +01:00
export class MainMenu {
2025-04-14 14:31:22 +02:00
constructor(
uiService,
menuLoop,
installMenu,
configMenu,
installer,
processControl,
codexApp,
2025-04-14 14:31:22 +02:00
) {
2025-02-25 13:59:26 +01:00
this.ui = uiService;
2025-04-01 14:09:20 +02:00
this.loop = menuLoop;
2025-03-07 13:14:32 +01:00
this.installMenu = installMenu;
2025-03-31 15:02:37 +02:00
this.configMenu = configMenu;
2025-04-14 14:31:22 +02:00
this.installer = installer;
this.processControl = processControl;
this.codexApp = codexApp;
2025-04-01 14:09:20 +02:00
this.loop.initialize(this.promptMainMenu);
2025-02-25 13:59:26 +01:00
}
show = async () => {
this.ui.showLogo();
2025-03-07 13:14:32 +01:00
2025-04-01 14:09:20 +02:00
await this.loop.showLoop();
2025-02-25 15:25:14 +01:00
};
2025-03-07 13:14:32 +01:00
promptMainMenu = async () => {
2025-04-15 14:01:44 +02:00
if ((await this.processControl.getNumberOfCodexProcesses()) > 0) {
2025-04-14 14:31:22 +02:00
await this.showRunningMenu();
} else {
if (await this.installer.isCodexInstalled()) {
2025-04-15 14:01:44 +02:00
await this.showNotRunningMenu();
2025-04-14 14:31:22 +02:00
} else {
await this.showNotInstalledMenu();
}
}
};
showNotInstalledMenu = async () => {
await this.ui.askMultipleChoice("Codex is not installed", [
2025-03-07 13:14:32 +01:00
{
2025-04-14 14:31:22 +02:00
label: "Install Codex",
2025-03-07 13:14:32 +01:00
action: this.installMenu.show,
},
2025-03-31 15:02:37 +02:00
{
2025-04-14 14:31:22 +02:00
label: "Exit",
action: this.loop.stopLoop,
},
]);
};
showRunningMenu = async () => {
await this.ui.askMultipleChoice("Codex is running", [
{
label: "Open Codex app",
action: this.codexApp.openCodexApp,
2025-04-14 14:31:22 +02:00
},
2025-04-15 14:01:44 +02:00
{
label: "Stop Codex",
action: this.stopCodex,
2025-04-15 14:01:44 +02:00
},
{
label: "DoThing",
action: this.doThing,
},
2025-04-14 14:31:22 +02:00
{
label: "Exit (Codex keeps running)",
2025-04-14 14:31:22 +02:00
action: this.loop.stopLoop,
},
]);
};
2025-04-15 14:01:44 +02:00
showNotRunningMenu = async () => {
2025-04-16 09:42:11 +02:00
await this.ui.askMultipleChoice("Codex is installed but not running", [
2025-04-14 14:31:22 +02:00
{
label: "Start Codex",
action: this.startCodex,
2025-04-14 14:31:22 +02:00
},
{
label: "Edit Codex config",
2025-03-31 15:02:37 +02:00
action: this.configMenu.show,
},
2025-04-14 14:31:22 +02:00
{
label: "Uninstall Codex",
action: this.installMenu.show,
},
{
2025-03-07 13:14:32 +01:00
label: "Exit",
2025-04-01 14:09:20 +02:00
action: this.loop.stopLoop,
},
2025-03-07 13:14:32 +01:00
]);
2025-02-25 13:59:26 +01:00
};
startCodex = async () => {
const spinner = this.ui.createAndStartSpinner("Starting...");
try {
await this.processControl.startCodexProcess();
this.ui.stopSpinnerSuccess(spinner);
} catch (exception) {
this.ui.stopSpinnerError(spinner);
this.ui.showErrorMessage(`Failed to start Codex. "${exception}"`);
}
};
stopCodex = async () => {
const spinner = this.ui.createAndStartSpinner("Stopping...");
try {
await this.processControl.stopCodexProcess();
this.ui.stopSpinnerSuccess(spinner);
} catch (exception) {
this.ui.stopSpinnerError(spinner);
this.ui.showErrorMessage(`Failed to stop Codex. "${exception}"`);
}
};
doThing = async () => {
console.log("A!");
const codex = new Codex("http://localhost:8080");
const data = codex.data;
const stategy = new NodeUploadStategy("Hello World !");
const uploadResponse = data.upload(stategy);
const res = await uploadResponse.result;
if (res.error) {
console.error(res.data);
return;
}
console.info("CID is", res.data);
const cid = res.data;
const result = await data.networkDownloadStream(cid);
console.log("download: " + JSON.stringify(result));
}
2025-02-25 13:59:26 +01:00
}