From 856cd33f26f019d2b8269e802036f1bc690d9904 Mon Sep 17 00:00:00 2001 From: m1m1sha <18262227804@163.com> Date: Sun, 5 May 2024 23:12:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E6=8B=86=E5=88=86store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easytier-gui/src/stores/network.ts | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 easytier-gui/src/stores/network.ts diff --git a/easytier-gui/src/stores/network.ts b/easytier-gui/src/stores/network.ts new file mode 100644 index 0000000..e676e86 --- /dev/null +++ b/easytier-gui/src/stores/network.ts @@ -0,0 +1,97 @@ +import { DEFAULT_NETWORK_CONFIG, NetworkConfig, NetworkInstance } from '~/types/network'; + +export const useNetworkStore = defineStore('networkStore', { + state: () => { + const networkList = [DEFAULT_NETWORK_CONFIG()]; + return { + // for initially empty lists + networkList: networkList as NetworkConfig[], + // for data that is not yet loaded + curNetwork: networkList[0], + + // uuid -> instance + instances: {} as Record, + + networkInfos: {} as Record, + } + }, + + getters: { + lastNetwork(): NetworkConfig { + return this.networkList[this.networkList.length - 1]; + }, + + curNetworkId(): string { + return this.curNetwork.instance_id; + }, + + networkInstances(): Array { + return Object.values(this.instances); + }, + + networkInstanceIds(): Array { + return Object.keys(this.instances); + } + }, + + actions: { + addNewNetwork() { + this.networkList.push(DEFAULT_NETWORK_CONFIG()); + }, + + delCurNetwork() { + const curNetworkIdx = this.networkList.indexOf(this.curNetwork); + this.networkList.splice(curNetworkIdx, 1); + const nextCurNetworkIdx = Math.min(curNetworkIdx, this.networkList.length - 1); + this.curNetwork = this.networkList[nextCurNetworkIdx]; + }, + + removeNetworkInstance(instanceId: string) { + delete this.instances[instanceId]; + }, + + addNetworkInstance(instanceId: string) { + this.instances[instanceId] = { + instance_id: instanceId, + running: false, + error_msg: "", + detail: {}, + }; + }, + + updateWithNetworkInfos(networkInfos: Record) { + this.networkInfos = networkInfos; + for (const [instanceId, info] of Object.entries(networkInfos)) { + if (this.instances[instanceId] === undefined) { + this.addNetworkInstance(instanceId); + } + this.instances[instanceId].running = info["running"]; + this.instances[instanceId].error_msg = info["error_msg"]; + this.instances[instanceId].detail = info; + } + }, + + loadFromLocalStorage() { + const networkList = JSON.parse(localStorage.getItem("networkList") || '[]'); + let result = []; + for (const cfg of networkList) { + result.push({ + ...DEFAULT_NETWORK_CONFIG, + ...cfg, + }); + } + if (result.length === 0) { + result.push(DEFAULT_NETWORK_CONFIG); + } + this.networkList = result; + this.curNetwork = this.networkList[0]; + }, + + saveToLocalStorage() { + localStorage.setItem("networkList", JSON.stringify(this.networkList)); + } + } +}) + +if (import.meta.hot) + import.meta.hot.accept(acceptHMRUpdate(useNetworkStore as any, import.meta.hot)) \ No newline at end of file