nodeStatusMenu.js
1 import chalk from "chalk"; 2 3 export class NodeStatusMenu { 4 constructor(uiService, dataService, menuLoop) { 5 this.ui = uiService; 6 this.dataService = dataService; 7 this.loop = menuLoop; 8 9 this.loop.initialize(this.showNodeStatusMenu); 10 } 11 12 showNodeStatus = async () => { 13 this.debugInfo = await this.fetchDebugInfo(); 14 if (this.debugInfo == undefined) return; 15 16 const peerCount = this.debugInfo.table.nodes.length; 17 const isOnline = peerCount > 2; 18 19 if (isOnline) { 20 this.ui.showSuccessMessage( 21 "Node is ONLINE & DISCOVERABLE", 22 "🔌 Node Status", 23 ); 24 } else { 25 this.ui.showInfoMessage( 26 "Node is ONLINE but has few peers", 27 "🔌 Node Status", 28 ); 29 } 30 31 await this.loop.showLoop(); 32 }; 33 34 showNodeStatusMenu = async () => { 35 await this.ui.askMultipleChoice("Select information to view:", [ 36 { 37 label: "View connected peers", 38 action: this.showPeers, 39 }, 40 { 41 label: "View node information", 42 action: this.showNodeInfo, 43 }, 44 { 45 label: "Back to Main Menu", 46 action: this.loop.stopLoop, 47 }, 48 ]); 49 }; 50 51 showPeers = async () => { 52 const peerCount = this.debugInfo.table.nodes.length; 53 if (peerCount > 0) { 54 this.ui.showInfoMessage("Connected Peers"); 55 this.debugInfo.table.nodes.forEach((node, index) => { 56 this.ui.showInfoMessage( 57 `Peer ${index + 1}:\n` + 58 `${chalk.cyan("Peer ID:")} ${node.peerId}\n` + 59 `${chalk.cyan("Address:")} ${node.address}\n` + 60 `${chalk.cyan("Status:")} ${node.seen ? chalk.green("Active") : chalk.gray("Inactive")}`, 61 ); 62 }); 63 } else { 64 this.ui.showInfoMessage("No connected peers found."); 65 } 66 }; 67 68 showNodeInfo = async () => { 69 const data = this.debugInfo; 70 this.ui.showInfoMessage( 71 `${chalk.cyan("Version:")} ${data.codex.version}\n` + 72 `${chalk.cyan("Revision:")} ${data.codex.revision}\n\n` + 73 `${chalk.cyan("Node ID:")} ${data.table.localNode.nodeId}\n` + 74 `${chalk.cyan("Peer ID:")} ${data.table.localNode.peerId}\n` + 75 `${chalk.cyan("Listening Address:")} ${data.table.localNode.address}\n\n` + 76 `${chalk.cyan("Public IP:")} ${data.announceAddresses[0].split("/")[2]}\n` + 77 `${chalk.cyan("Port:")} ${data.announceAddresses[0].split("/")[4]}\n` + 78 `${chalk.cyan("Connected Peers:")} ${data.table.nodes.length}`, 79 ); 80 }; 81 82 fetchDebugInfo = async () => { 83 const spinner = this.ui.createAndStartSpinner("Fetching..."); 84 try { 85 return await this.dataService.debugInfo(); 86 } catch { 87 this.ui.showErrorMessage("Failed to fetch debug/info"); 88 return; 89 } finally { 90 this.ui.stopSpinnerSuccess(spinner); 91 } 92 }; 93 }