From 0c16e2211b0ef7c9f3b60790c362875f279f6411 Mon Sep 17 00:00:00 2001 From: Mg Pig Date: Thu, 8 Jan 2026 17:03:51 +0800 Subject: [PATCH] feat(gui): persist and restore last used network instance ID (#1762) --- easytier-gui/src/auto-imports.d.ts | 4 ++++ easytier-gui/src/composables/config.ts | 20 ++++++++++++++++++++ easytier-gui/src/pages/index.vue | 12 ++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 easytier-gui/src/composables/config.ts diff --git a/easytier-gui/src/auto-imports.d.ts b/easytier-gui/src/auto-imports.d.ts index cc69d397..5db4b676 100644 --- a/easytier-gui/src/auto-imports.d.ts +++ b/easytier-gui/src/auto-imports.d.ts @@ -43,6 +43,7 @@ declare global { const isWebClientConnected: typeof import('./composables/backend')['isWebClientConnected'] const listNetworkInstanceIds: typeof import('./composables/backend')['listNetworkInstanceIds'] const listenGlobalEvents: typeof import('./composables/event')['listenGlobalEvents'] + const loadLastNetworkInstanceId: typeof import('./composables/config')['loadLastNetworkInstanceId'] const loadMode: typeof import('./composables/mode')['loadMode'] const mapActions: typeof import('pinia')['mapActions'] const mapGetters: typeof import('pinia')['mapGetters'] @@ -76,6 +77,7 @@ declare global { const ref: typeof import('vue')['ref'] const resolveComponent: typeof import('vue')['resolveComponent'] const runNetworkInstance: typeof import('./composables/backend')['runNetworkInstance'] + const saveLastNetworkInstanceId: typeof import('./composables/config')['saveLastNetworkInstanceId'] const saveMode: typeof import('./composables/mode')['saveMode'] const saveNetworkConfig: typeof import('./composables/backend')['saveNetworkConfig'] const sendConfigs: typeof import('./composables/backend')['sendConfigs'] @@ -165,6 +167,7 @@ declare module 'vue' { readonly isWebClientConnected: UnwrapRef readonly listNetworkInstanceIds: UnwrapRef readonly listenGlobalEvents: UnwrapRef + readonly loadLastNetworkInstanceId: UnwrapRef readonly loadMode: UnwrapRef readonly mapActions: UnwrapRef readonly mapGetters: UnwrapRef @@ -198,6 +201,7 @@ declare module 'vue' { readonly ref: UnwrapRef readonly resolveComponent: UnwrapRef readonly runNetworkInstance: UnwrapRef + readonly saveLastNetworkInstanceId: UnwrapRef readonly saveMode: UnwrapRef readonly saveNetworkConfig: UnwrapRef readonly sendConfigs: UnwrapRef diff --git a/easytier-gui/src/composables/config.ts b/easytier-gui/src/composables/config.ts new file mode 100644 index 00000000..72e8cae9 --- /dev/null +++ b/easytier-gui/src/composables/config.ts @@ -0,0 +1,20 @@ +/** + * 配置持久化相关的函数 + * 用于保存和加载应用程序的各种配置状态 + */ + +/** + * 保存上次使用的网络实例 ID + * @param instanceId 网络实例 ID + */ +export function saveLastNetworkInstanceId(instanceId: string) { + localStorage.setItem('last_network_instance_id', instanceId) +} + +/** + * 加载上次使用的网络实例 ID + * @returns 上次使用的网络实例 ID,如果没有则返回 null + */ +export function loadLastNetworkInstanceId(): string | null { + return localStorage.getItem('last_network_instance_id') +} \ No newline at end of file diff --git a/easytier-gui/src/pages/index.vue b/easytier-gui/src/pages/index.vue index 620772b5..32db5a7d 100644 --- a/easytier-gui/src/pages/index.vue +++ b/easytier-gui/src/pages/index.vue @@ -13,6 +13,7 @@ import { GUIRemoteClient } from '~/modules/api' import { useToast, useConfirm } from 'primevue' import { loadMode, saveMode, WebClientConfig, type Mode } from '~/composables/mode' +import { saveLastNetworkInstanceId, loadLastNetworkInstanceId } from '~/composables/config' import ModeSwitcher from '~/components/ModeSwitcher.vue' import { getServiceStatus } from '~/composables/backend' @@ -190,6 +191,12 @@ const remoteClient = computed(() => new GUIRemoteClient()); const instanceId = ref(undefined); const clientRunning = ref(false); +watch(instanceId, (newVal) => { + if (newVal) { + saveLastNetworkInstanceId(newVal); + } +}); + watch(clientRunning, async (newVal, oldVal) => { if (!newVal && oldVal) { if (manualDisconnect.value) { @@ -197,6 +204,11 @@ watch(clientRunning, async (newVal, oldVal) => { return } await reconnectClient() + } else if (newVal && !oldVal) { + const lastInstanceId = loadLastNetworkInstanceId(); + if (lastInstanceId) { + instanceId.value = lastInstanceId; + } } })