From 4eec1e0e09dc5c691a4409df24c6c3878aa01e42 Mon Sep 17 00:00:00 2001 From: Adam Lamers Date: Sat, 4 Apr 2026 13:44:37 -0400 Subject: [PATCH] select spacetime server --- index.html | 1 + package.json | 5 +- src/App.svelte | 23 ++-- src/auth/AuthGate.svelte | 148 ++++++++++++++++++++++++-- src/auth/auth.svelte.ts | 12 ++- src/chat/ChatContainer.svelte | 4 +- src/chat/components/ServerList.svelte | 18 ++++ src/config.ts | 66 ++++++++++-- 8 files changed, 244 insertions(+), 33 deletions(-) diff --git a/index.html b/index.html index 1e6caf8..55259c6 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@
+ diff --git a/package.json b/package.json index aba0741..7b3d4ef 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,11 @@ "lint": "eslint . && prettier . --check --ignore-path ../../.prettierignore", "preview": "vite preview", "test": "vitest run", - "generate": "cargo run -p gen-bindings -- --out-dir src/module_bindings --module-path spacetimedb && prettier --write src/module_bindings", "spacetime:generate": "spacetime generate --lang typescript --out-dir src/module_bindings --module-path spacetimedb", "spacetime:publish:local": "spacetime publish --module-path spacetimedb --server local", - "spacetime:publish": "spacetime publish --module-path spacetimedb --server maincloud" + "spacetime:publish": "spacetime publish --module-path spacetimedb --server maincloud", + "deploy:local": "docker compose -f docker-compose.local.yml up --build", + "deploy:maincloud": "docker compose -f docker-compose.maincloud.yml up --build" }, "dependencies": { "@fortawesome/fontawesome-free": "^7.2.0", diff --git a/src/App.svelte b/src/App.svelte index 0500f7c..da2c6d3 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,21 +1,28 @@ -{#key reconnectKey} - - - - - -{/key} + + handleToggleServerSettings(true)} /> + diff --git a/src/auth/AuthGate.svelte b/src/auth/AuthGate.svelte index 67561f7..21231d9 100644 --- a/src/auth/AuthGate.svelte +++ b/src/auth/AuthGate.svelte @@ -1,16 +1,47 @@
@@ -33,6 +35,18 @@ +
+ + +
+ {#if chat.showCreateServerModal}
{ + const windowEnv = (window as any).__ENV__; + if (windowEnv && windowEnv[key] && windowEnv[key] !== "") { + return windowEnv[key]; + } + if (import.meta.env[key] && import.meta.env[key] !== "") { + return import.meta.env[key]; + } + return defaultValue; +}; + +export const HOST_KEY = "stdb_host"; +export const DB_NAME_KEY = "stdb_db_name"; + +const normalizeHost = (host: string) => { + try { + const url = new URL(host.includes("://") ? host : `wss://${host}`); + return url.origin.replace(/^http/, 'ws'); // Ensure ws/wss + } catch (e) { + return host.trim().replace(/\/+$/, ""); // Fallback + } +}; + +export const TokenStore = { + get: (host: string, dbName: string) => { + const key = `${normalizeHost(host)}/${dbName}/auth_token`; + return localStorage.getItem(key); + }, + set: (host: string, dbName: string, token: string) => { + const key = `${normalizeHost(host)}/${dbName}/auth_token`; + localStorage.setItem(key, token); + }, + clear: (host: string, dbName: string) => { + const key = `${normalizeHost(host)}/${dbName}/auth_token`; + localStorage.removeItem(key); + } +}; + +export const getStdbHost = () => localStorage.getItem(HOST_KEY) || getEnv("VITE_SPACETIMEDB_HOST", "wss://maincloud.spacetimedb.com"); +export const getStdbDbName = () => localStorage.getItem(DB_NAME_KEY) || getEnv("VITE_SPACETIMEDB_DB_NAME", "my-spacetime-app-jdhdg"); let _connection: DbConnection | null = null; export const getConnection = () => _connection; @@ -15,14 +50,18 @@ class ConnectionManager { #retryCount = 0; #reconnectTimeout: any = null; #onReconnectTrigger: () => void; + #host: string; + #dbName: string; - constructor(onReconnectTrigger: () => void) { + constructor(host: string, dbName: string, onReconnectTrigger: () => void) { + this.#host = host; + this.#dbName = dbName; this.#onReconnectTrigger = onReconnectTrigger; } onConnect = (conn: DbConnection, identity: any, token: string) => { _connection = conn; - localStorage.setItem(TOKEN_KEY, token); + TokenStore.set(this.#host, this.#dbName, token); console.log( "Connected to SpacetimeDB with identity:", identity.toHexString(), @@ -63,15 +102,20 @@ class ConnectionManager { } export const connectionBuilder = (onReconnectTrigger: () => void) => { - const manager = new ConnectionManager(onReconnectTrigger); + const host = getStdbHost(); + const dbName = getStdbDbName(); + + console.log(`Connecting to SpacetimeDB: ${host}/${dbName}`); + + const manager = new ConnectionManager(host, dbName, onReconnectTrigger); const builder = DbConnection.builder() - .withUri(HOST) - .withDatabaseName(DB_NAME) + .withUri(host) + .withDatabaseName(dbName) .onConnect(manager.onConnect) .onDisconnect(manager.onDisconnect) .onConnectError(manager.onConnectError); - const storedToken = localStorage.getItem(TOKEN_KEY); + const storedToken = TokenStore.get(host, dbName); if (auth.isAuthenticated && auth.user?.id_token) { console.log("SpacetimeDBWrapper: Connecting with OIDC token");