Skip to content

Commit be7f576

Browse files
committed
refactor: use zustand for state management
1 parent a400276 commit be7f576

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/App.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { download } from "@tauri-apps/plugin-upload";
77
import { fetch } from "@tauri-apps/plugin-http";
88
import { Child, Command } from "@tauri-apps/plugin-shell";
99
import { mkdir, exists, readDir, BaseDirectory } from "@tauri-apps/plugin-fs";
10+
import { useStore } from "./store";
1011
import "./App.css";
1112

1213
const urlNetwork = "https://test.net.zknet.io";
@@ -43,11 +44,18 @@ function App() {
4344
const [networkId, setNetworkId] = useState("");
4445
const [dlProgress, setDlProgress] = useState(0);
4546
const [clientPid, setClientPid] = useState(0);
46-
const [appVersion, setAppVersion] = useState("");
47-
const [platformArch, setPlatformArch] = useState("");
48-
const [platformSupported, setPlatformSupported] = useState(false);
49-
const [networks, setNetworks] = useState<string[]>([]);
50-
const [isConnected, setIsConnected] = useState(false);
47+
48+
const appVersion = useStore((s) => s.appVersion);
49+
const isConnected = useStore((s) => s.isConnected);
50+
const isPlatformSupported = useStore((s) => s.isPlatformSupported);
51+
const networks = useStore((s) => s.networks);
52+
const platformArch = useStore((s) => s.platformArch);
53+
54+
const setAppVersion = useStore((s) => s.setAppVersion);
55+
const setIsConnected = useStore((s) => s.setIsConnected);
56+
const setIsPlatformSupported = useStore((s) => s.setIsPlatformSupported);
57+
const setNetworks = useStore((s) => s.setNetworks);
58+
const setPlatformArch = useStore((s) => s.setPlatformArch);
5159

5260
// run once on startup (twice in dev mode)
5361
useEffect(() => {
@@ -59,7 +67,7 @@ function App() {
5967

6068
setAppVersion(v);
6169
setPlatformArch(getPlatformArch());
62-
setPlatformSupported(true);
70+
setIsPlatformSupported(true);
6371
setNetworks(await getNetworks());
6472
})();
6573
} catch (error: any) {
@@ -235,7 +243,7 @@ function App() {
235243
className={`logo ${isConnected ? "pulsing" : ""}`}
236244
/>
237245

238-
{platformSupported &&
246+
{isPlatformSupported &&
239247
(clientPid === 0 ? (
240248
<>
241249
<p>Enter a network identifier for access.</p>

src/store/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { create } from "zustand";
2+
import { combine } from "zustand/middleware";
3+
4+
export const useStore = create(
5+
combine(
6+
{
7+
appVersion: "",
8+
isConnected: false,
9+
isPlatformSupported: false,
10+
networks: [] as string[],
11+
platformArch: "",
12+
},
13+
(set) => ({
14+
setAppVersion: (appVersion: string) => set({ appVersion }),
15+
setIsConnected: (isConnected: boolean) => set({ isConnected }),
16+
setIsPlatformSupported: (isPlatformSupported: boolean) =>
17+
set({ isPlatformSupported }),
18+
setNetworks: (networks: string[]) => set({ networks }),
19+
setPlatformArch: (platformArch: string) => set({ platformArch }),
20+
}),
21+
),
22+
);

0 commit comments

Comments
 (0)