From f6f17cc57efb6a7cabf66195f1163db17f061fc9 Mon Sep 17 00:00:00 2001 From: Lazy <2211717435@qq.com> Date: Sat, 2 Mar 2024 22:21:13 +0800 Subject: [PATCH] Opt: instance status code --- frontend/src/hooks/useInstance.ts | 17 ++++++++++++----- frontend/src/hooks/useTerminal.ts | 6 ++---- frontend/src/types/const.ts | 16 ++++++++-------- frontend/src/types/index.ts | 3 ++- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/frontend/src/hooks/useInstance.ts b/frontend/src/hooks/useInstance.ts index dfcb1e55..02fd2c6a 100644 --- a/frontend/src/hooks/useInstance.ts +++ b/frontend/src/hooks/useInstance.ts @@ -2,7 +2,7 @@ import type { InstanceDetail, MapData } from "@/types"; import { t } from "@/lang/i18n"; import { computed, onMounted, onUnmounted, ref, type Ref } from "vue"; import { getInstanceInfo } from "@/services/apis/instance"; -import { INSTANCE_STATUS } from "@/types/const"; +import { INSTANCE_STATUS, INSTANCE_STATUS_CODE } from "@/types/const"; export const TYPE_UNIVERSAL = "universal"; export const TYPE_WEB_SHELL = "universal/web_shell"; @@ -84,15 +84,19 @@ export function useInstanceInfo(params: Params) { let finalState = state; if (instanceInfo) finalState = instanceInfo; - const isRunning = computed(() => finalState?.value?.status === 3); - const isStopped = computed(() => finalState?.value?.status === 0); + const isUnknown = computed(() => finalState?.value?.status === INSTANCE_STATUS_CODE.UNKNOWN); + const isStopped = computed(() => finalState?.value?.status === INSTANCE_STATUS_CODE.STOPPED); + const isStopping = computed(() => finalState?.value?.status === INSTANCE_STATUS_CODE.STOPPING); + const isStarting = computed(() => finalState?.value?.status === INSTANCE_STATUS_CODE.STARTING); + const isRunning = computed(() => finalState?.value?.status === INSTANCE_STATUS_CODE.RUNNING); + const instanceTypeText = computed(() => { return ( INSTANCE_TYPE_TRANSLATION[String(finalState?.value?.config.type)] || t("TXT_CODE_da7a0328") ); }); const statusText = computed( - () => String(INSTANCE_STATUS[String(finalState?.value?.status)]) || t("TXT_CODE_c8333afa") + () => String(INSTANCE_STATUS[finalState.value?.status ?? -1]) || t("TXT_CODE_c8333afa") ); onMounted(async () => { @@ -128,8 +132,11 @@ export function useInstanceInfo(params: Params) { instanceInfo: state, execute, statusText, - isRunning, + isUnknown, isStopped, + isStopping, + isStarting, + isRunning, instanceTypeText }; } diff --git a/frontend/src/hooks/useTerminal.ts b/frontend/src/hooks/useTerminal.ts index 5a2a5e1a..7b114737 100644 --- a/frontend/src/hooks/useTerminal.ts +++ b/frontend/src/hooks/useTerminal.ts @@ -216,10 +216,8 @@ export function useTerminal() { socket?.removeAllListeners(); }); - const isStopped = computed(() => - [INSTANCE_STATUS_CODE.STOPPED, INSTANCE_STATUS_CODE.UNKNOWN].includes(state?.value?.status ?? 0) - ); - const isRunning = computed(() => !isStopped.value); + const isStopped = computed(() => state?.value?.status === INSTANCE_STATUS_CODE.STOPPED); + const isRunning = computed(() => state?.value?.status === INSTANCE_STATUS_CODE.RUNNING); return { events, diff --git a/frontend/src/types/const.ts b/frontend/src/types/const.ts index db0c5e45..451d0e15 100644 --- a/frontend/src/types/const.ts +++ b/frontend/src/types/const.ts @@ -14,14 +14,6 @@ export const TERMINAL_CODE = [ "UTF-16" ]; -export const INSTANCE_STATUS: Record = { - "-1": t("TXT_CODE_342a04a9"), - "0": t("TXT_CODE_4ef6b040"), - "1": t("TXT_CODE_a409b8a9"), - "2": t("TXT_CODE_175b570d"), - "3": t("TXT_CODE_bdb620b9") -}; - export enum INSTANCE_STATUS_CODE { UNKNOWN = -1, STOPPED = 0, @@ -30,6 +22,14 @@ export enum INSTANCE_STATUS_CODE { RUNNING = 3 } +export const INSTANCE_STATUS: Record = { + [INSTANCE_STATUS_CODE.UNKNOWN]: t("TXT_CODE_342a04a9"), + [INSTANCE_STATUS_CODE.STOPPED]: t("TXT_CODE_4ef6b040"), + [INSTANCE_STATUS_CODE.STOPPING]: t("TXT_CODE_a409b8a9"), + [INSTANCE_STATUS_CODE.STARTING]: t("TXT_CODE_175b570d"), + [INSTANCE_STATUS_CODE.RUNNING]: t("TXT_CODE_bdb620b9") +}; + export const defaultDockerFile = `FROM ubuntu:latest\nRUN mkdir -p /workspace\nWORKDIR /workspace\n`; export const openjdk8 = `FROM openjdk:8-jre diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts index 5d3104ab..1df6188f 100644 --- a/frontend/src/types/index.ts +++ b/frontend/src/types/index.ts @@ -7,6 +7,7 @@ import type { IMapData, IGlobalInstanceDockerConfig } from "../../../common/global"; +import type { INSTANCE_STATUS_CODE } from "./const"; export type JsonData = IJsonData; export type MapData = IMapData; @@ -29,7 +30,7 @@ export enum NEW_CARD_TYPE { export interface InstanceDetail { instanceUuid: string; started: number; - status: number; + status: INSTANCE_STATUS_CODE; info: { currentPlayers: number; fileLock: number;