Skip to content

Commit

Permalink
Merge pull request #189 from flatironinstitute/move-server-choice-state
Browse files Browse the repository at this point in the history
Move server choice state up for better consistency
  • Loading branch information
WardBrian authored Aug 1, 2024
2 parents 9633134 + 6787696 commit 1bdcce3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -21,6 +23,15 @@ const CompilationServerConnectionControl: FunctionComponent<
const [stanWasmServerUrl, setStanWasmServerUrl] = useState<string>(
localStorage.getItem("stanWasmServerUrl") || publicUrl,
);

const [serverType, setServerType] = useState<ServerType>(
stanWasmServerUrl === publicUrl
? "public"
: stanWasmServerUrl === localUrl
? "local"
: "custom",
);

const { isConnected, retryConnection } = useIsConnected(stanWasmServerUrl);
useEffect(() => {
localStorage.setItem("stanWasmServerUrl", stanWasmServerUrl);
Expand All @@ -36,12 +47,6 @@ const CompilationServerConnectionControl: FunctionComponent<
retryConnection();
}, [retryConnection]);

const serverLabel =
stanWasmServerUrl === publicUrl
? "public"
: stanWasmServerUrl === localUrl
? "local"
: "custom";
return (
<>
<IconButton onClick={openDialog} color="inherit" size="small">
Expand All @@ -53,7 +58,7 @@ const CompilationServerConnectionControl: FunctionComponent<
&nbsp;
<Typography color="white" fontSize={12}>
{isConnected ? "connected to " : "not connected to "}
{serverLabel}
{serverType}
</Typography>
</IconButton>
<CloseableDialog
Expand All @@ -67,6 +72,8 @@ const CompilationServerConnectionControl: FunctionComponent<
setStanWasmServerUrl={setStanWasmServerUrl}
isConnected={isConnected}
onRetry={handleRetry}
choice={serverType}
setChoice={setServerType}
/>
</CloseableDialog>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<ServerType>("public");

> = ({
stanWasmServerUrl,
setStanWasmServerUrl,
isConnected,
onRetry,
choice,
setChoice,
}) => {
const makeChoice = useCallback(
(_: unknown, choice: string) => {
if (choice === "public") {
Expand All @@ -37,7 +46,7 @@ const ConfigureCompilationServerDialog: FunctionComponent<
}
setChoice(choice);
},
[setStanWasmServerUrl],
[setChoice, setStanWasmServerUrl],
);

return (
Expand Down

0 comments on commit 1bdcce3

Please sign in to comment.