Skip to content

Commit 752ce10

Browse files
committed
feat: enable selection of previously connected network
1 parent 8cc54b4 commit 752ce10

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/App.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { arch, platform } from "@tauri-apps/plugin-os";
55
import { download } from "@tauri-apps/plugin-upload";
66
import { fetch } from "@tauri-apps/plugin-http";
77
import { Child, Command } from "@tauri-apps/plugin-shell";
8-
import { mkdir, exists } from "@tauri-apps/plugin-fs";
8+
import { mkdir, exists, readDir, BaseDirectory } from "@tauri-apps/plugin-fs";
99
import "./App.css";
1010

1111
const urlNetwork = "https://test.net.zknet.io";
@@ -28,6 +28,14 @@ const getPlatformArch = (): string => {
2828
}
2929
};
3030

31+
// Get networks with previously downloaded assets
32+
const getNetworks = async () => {
33+
const entries = await readDir("networks", {
34+
baseDir: BaseDirectory.AppLocalData,
35+
});
36+
return entries.filter((i) => i.isDirectory).map((i) => i.name);
37+
};
38+
3139
function App() {
3240
const [msg, setMsg] = useState("");
3341
const [msgType, setMsgType] = useState(""); // error, info, success
@@ -36,12 +44,18 @@ function App() {
3644
const [clientPid, setClientPid] = useState(0);
3745
const [platformArch, setPlatformArch] = useState("");
3846
const [platformSupported, setPlatformSupported] = useState(false);
47+
const [networks, setNetworks] = useState<string[]>([]);
3948

49+
// run once on startup (twice in dev mode)
4050
useEffect(() => {
4151
try {
4252
log.info(`Platform: ${platform()}-${arch()}`);
4353
setPlatformArch(getPlatformArch());
4454
setPlatformSupported(true);
55+
56+
(async () => {
57+
setNetworks(await getNetworks());
58+
})();
4559
} catch (error: any) {
4660
log.error(`${error}`);
4761
setMsgType("error");
@@ -55,6 +69,7 @@ function App() {
5569
setClientPid(pid);
5670
setMsgType("info");
5771
setMsg("");
72+
setNetworks(await getNetworks());
5873
} catch (error: any) {
5974
log.error(`${error}`);
6075
setMsgType("error");
@@ -210,7 +225,13 @@ function App() {
210225
className="input focus:outline-none join-item"
211226
onChange={(e) => setNetworkId(e.currentTarget.value)}
212227
placeholder="Enter a network_id..."
228+
list="networks"
213229
/>
230+
<datalist id="networks">
231+
{networks.map((n) => (
232+
<option key={n} value={n} />
233+
))}
234+
</datalist>
214235
<button className="btn btn-primary join-item" type="submit">
215236
Connect
216237
</button>

0 commit comments

Comments
 (0)