From 6787696484ae3da2b763702eee4392bda0d66195 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Wed, 31 Jul 2024 21:03:26 +0000 Subject: [PATCH] Move server choice state up for better consistency --- .../CompilationServerConnectionControl.tsx | 21 ++++++++++------ .../ConfigureCompilationServerDialog.tsx | 25 +++++++++++++------ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/gui/src/app/CompilationServerConnectionControl/CompilationServerConnectionControl.tsx b/gui/src/app/CompilationServerConnectionControl/CompilationServerConnectionControl.tsx index 91b0987..f1a2620 100644 --- a/gui/src/app/CompilationServerConnectionControl/CompilationServerConnectionControl.tsx +++ b/gui/src/app/CompilationServerConnectionControl/CompilationServerConnectionControl.tsx @@ -11,6 +11,8 @@ import Typography from "@mui/material/Typography"; export const publicUrl = "https://trom-stan-wasm-server.magland.org"; export const localUrl = "http://localhost:8083"; +export type ServerType = "public" | "local" | "custom"; + type CompilationServerConnectionControlProps = { // none }; @@ -21,6 +23,15 @@ const CompilationServerConnectionControl: FunctionComponent< const [stanWasmServerUrl, setStanWasmServerUrl] = useState( localStorage.getItem("stanWasmServerUrl") || publicUrl, ); + + const [serverType, setServerType] = useState( + stanWasmServerUrl === publicUrl + ? "public" + : stanWasmServerUrl === localUrl + ? "local" + : "custom", + ); + const { isConnected, retryConnection } = useIsConnected(stanWasmServerUrl); useEffect(() => { localStorage.setItem("stanWasmServerUrl", stanWasmServerUrl); @@ -36,12 +47,6 @@ const CompilationServerConnectionControl: FunctionComponent< retryConnection(); }, [retryConnection]); - const serverLabel = - stanWasmServerUrl === publicUrl - ? "public" - : stanWasmServerUrl === localUrl - ? "local" - : "custom"; return ( <> @@ -53,7 +58,7 @@ const CompilationServerConnectionControl: FunctionComponent<   {isConnected ? "connected to " : "not connected to "} - {serverLabel} + {serverType} diff --git a/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx b/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx index 61e8cf8..a170bb1 100644 --- a/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx +++ b/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx @@ -1,5 +1,9 @@ -import { FunctionComponent, useCallback, useState } from "react"; -import { localUrl, publicUrl } from "./CompilationServerConnectionControl"; +import { FunctionComponent, useCallback } from "react"; +import { + localUrl, + publicUrl, + ServerType, +} from "./CompilationServerConnectionControl"; import FormControl from "@mui/material/FormControl"; import Divider from "@mui/material/Divider"; import FormLabel from "@mui/material/FormLabel"; @@ -10,20 +14,25 @@ import TextField from "@mui/material/TextField"; import IconButton from "@mui/material/IconButton"; import { Refresh } from "@mui/icons-material"; -type ServerType = "public" | "local" | "custom"; - type ConfigureCompilationServerDialogProps = { stanWasmServerUrl: string; setStanWasmServerUrl: (url: string) => void; isConnected: boolean; onRetry: () => void; + choice: ServerType; + setChoice: (choice: ServerType) => void; }; const ConfigureCompilationServerDialog: FunctionComponent< ConfigureCompilationServerDialogProps -> = ({ stanWasmServerUrl, setStanWasmServerUrl, isConnected, onRetry }) => { - const [choice, setChoice] = useState("public"); - +> = ({ + stanWasmServerUrl, + setStanWasmServerUrl, + isConnected, + onRetry, + choice, + setChoice, +}) => { const makeChoice = useCallback( (_: unknown, choice: string) => { if (choice === "public") { @@ -37,7 +46,7 @@ const ConfigureCompilationServerDialog: FunctionComponent< } setChoice(choice); }, - [setStanWasmServerUrl], + [setChoice, setStanWasmServerUrl], ); return (