From cba9af34a0436b7b0a1fd7a90383ca8bbbdbe852 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 18 Aug 2026 16:35:53 +0200 Subject: [PATCH] Improved serverlist structure --- src-tauri/src/commands.rs | 11 +++++----- src/app/app.ts | 20 ++++++++++-------- src/components/view/serverList.tsx | 12 +++++------ src/lib/serverList.ts | 33 ++++++++++++++---------------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index e36601a..12fba8f 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::fs; +use std::{collections::HashMap, fs}; use tauri::{AppHandle, Manager}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -7,10 +7,11 @@ pub struct KnownServer { pub name: String, pub description: String, pub public_key: String, - pub hostname: String, pub is_secure: bool, } +type ServerList = HashMap; + fn servers_file_path(app: &AppHandle) -> Result { let dir = app .path() @@ -24,7 +25,7 @@ fn servers_file_path(app: &AppHandle) -> Result { } #[tauri::command] -pub fn save_server_list(app: AppHandle, servers: Vec) -> Result<(), String> { +pub fn save_server_list(app: AppHandle, servers: ServerList) -> Result<(), String> { let path = servers_file_path(&app)?; let json = serde_json::to_string_pretty(&servers) @@ -36,13 +37,13 @@ pub fn save_server_list(app: AppHandle, servers: Vec) -> Result<(), } #[tauri::command] -pub fn get_server_list(app: AppHandle) -> Result, String> { +pub fn get_server_list(app: AppHandle) -> Result { let path = servers_file_path(&app)?; if !path.exists() { fs::write(&path, "[]").map_err(|e| format!("Failed to write first server list: {e}"))?; - return Ok(Vec::new()); + return Ok(ServerList::new()); } let json = fs::read_to_string(&path).map_err(|e| format!("Failed to read server list: {e}"))?; diff --git a/src/app/app.ts b/src/app/app.ts index 5bb4905..5cb6ba4 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -1,4 +1,9 @@ -import { getServerList, getWSUrl, KnownServer } from "@/lib/serverList"; +import { + getServerList, + getWSUrl, + KnownServer, + ServerList, +} from "@/lib/serverList"; import EnclaveServer from "./server"; import * as ed from "@noble/ed25519"; import { sha512 } from "@noble/hashes/sha2.js"; @@ -23,36 +28,35 @@ export default class Enclave { private clientSecretKey: Uint8Array; private clientPublicKey: Uint8Array; public server?: EnclaveServer; - public serverList: KnownServer[]; + public serverList: ServerList; public constructor() { this.clientSecretKey = ed.utils.randomSecretKey(); this.clientPublicKey = ed.getPublicKey(this.clientSecretKey); - this.serverList = []; + this.serverList = {}; } public async init() { this.serverList = await getServerList(); } - public async addServer(hostname: string, isSecure: boolean) { + public async connectToServer(hostname: string, isSecure: boolean) { if (this.server) { this.server.disconnect(); } - this.server = new EnclaveServer(getWSUrl({ hostname, isSecure })); + this.server = new EnclaveServer(getWSUrl(hostname, isSecure)); if (!this.server.serverPublicKey) { console.error("Failed to get public key"); return; } - this.serverList.push({ + this.serverList[hostname] = { name: "", description: "", - hostname, isSecure, publicKey: base58.encode(this.server.serverPublicKey), - }); + }; } } diff --git a/src/components/view/serverList.tsx b/src/components/view/serverList.tsx index 3301d43..19d21e5 100644 --- a/src/components/view/serverList.tsx +++ b/src/components/view/serverList.tsx @@ -7,12 +7,14 @@ export default function ServerList({ }: { appRef: React.RefObject; }) { + if (!appRef.current) return null; + return (