diff --git a/apps/desktop/src/main.tsx b/apps/desktop/src/main.tsx index ab44425..9c31c20 100644 --- a/apps/desktop/src/main.tsx +++ b/apps/desktop/src/main.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react" +import { useEffect, useMemo, useState } from "react" import { createRoot } from "react-dom/client" import { invoke } from "@tauri-apps/api/core" import { Store } from "@tauri-apps/plugin-store" @@ -149,6 +149,29 @@ function App() { })() }, []) + useEffect(() => { + if (!store || !config) return + const normalizedAppUrl = normalizeUrl(config.appUrl, appUrl) + const normalizedApiUrl = normalizeUrl(config.apiBaseUrl, apiBaseUrl) + const shouldForceRemote = import.meta.env.MODE === "production" + const nextAppUrl = shouldForceRemote && normalizedAppUrl.includes("localhost") ? appUrl : normalizedAppUrl + const nextApiUrl = shouldForceRemote && normalizedApiUrl.includes("localhost") ? apiBaseUrl : normalizedApiUrl + if (nextAppUrl !== config.appUrl || nextApiUrl !== config.apiBaseUrl) { + const updatedConfig = { ...config, appUrl: nextAppUrl, apiBaseUrl: nextApiUrl } + setConfig(updatedConfig) + writeConfig(store, updatedConfig).catch((err) => console.error("Falha ao atualizar configuração", err)) + } + }, [store, config]) + + const resolvedAppUrl = useMemo(() => { + if (!config?.appUrl) return appUrl + const normalized = normalizeUrl(config.appUrl, appUrl) + if (import.meta.env.MODE === "production" && normalized.includes("localhost")) { + return appUrl + } + return normalized + }, [config?.appUrl, appUrl]) + async function register() { if (!profile) return if (!provisioningSecret.trim()) { setError("Informe o código de provisionamento."); return } @@ -197,7 +220,7 @@ function App() { async function openSystem() { if (!token || !config) return - const url = `${config.appUrl}/machines/handshake?token=${encodeURIComponent(token)}` + const url = `${resolvedAppUrl}/machines/handshake?token=${encodeURIComponent(token)}` try { // open in default browser; fallback to in-webview const { openUrl } = await import("@tauri-apps/plugin-opener")