Skip to content

Commit

Permalink
dashboard: use search param for network, fixes #181
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-gray committed Apr 2, 2024
1 parent 0e88b5b commit fd563e1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
9 changes: 6 additions & 3 deletions dashboard/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CssBaseline } from '@mui/material';
import { HashRouter as Router } from 'react-router-dom';
import CustomThemeProvider from './components/CustomThemeProvider';
import Main from './components/Main';
import { NetworkContextProvider } from './contexts/NetworkContext';
Expand All @@ -9,9 +10,11 @@ function App() {
<SettingsContextProvider>
<CustomThemeProvider>
<CssBaseline />
<NetworkContextProvider>
<Main />
</NetworkContextProvider>
<Router>
<NetworkContextProvider>
<Main />
</NetworkContextProvider>
</Router>
</CustomThemeProvider>
</SettingsContextProvider>
);
Expand Down
18 changes: 12 additions & 6 deletions dashboard/src/components/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { GitHub } from '@mui/icons-material';
import { AppBar, Box, Button, Hidden, IconButton, Toolbar, Typography } from '@mui/material';
import { Route, HashRouter as Router, Switch, NavLink } from 'react-router-dom';
import { NavLink, Route, Switch } from 'react-router-dom';
import useChainHeartbeats from '../hooks/useChainHeartbeats';
import useHeartbeats from '../hooks/useHeartbeats';
import useLatestRelease from '../hooks/useLatestRelease';
import WormholeStatsIcon from '../icons/WormholeStatsIcon';
import Alerts from './Alerts';
import Home from './Home';
import Metrics from './Metrics';
import NetworkSelector from './NetworkSelector';
import Settings from './Settings';
import Metrics from './Metrics';

function NavButton(props: any) {
// fix for Invalid value for prop `navigate` on <a> tag
const { navigate, ...rest } = props;
return <Button {...rest} />;
}

function Main() {
const heartbeats = useHeartbeats();
const chainIdsToHeartbeats = useChainHeartbeats(heartbeats);
const latestRelease = useLatestRelease();
return (
<Router>
<>
<AppBar position="static">
<Toolbar variant="dense">
<NavLink
to="/"
exact
component={Button}
component={NavButton}
color="inherit"
activeStyle={{ borderBottom: '2px solid', paddingBottom: 4 }}
style={{ marginLeft: -8, textTransform: 'none', borderRadius: 0, minWidth: 0 }}
Expand All @@ -39,7 +45,7 @@ function Main() {
<NavLink
to="/metrics"
exact
component={Button}
component={NavButton}
color="inherit"
activeStyle={{ borderBottom: '2px solid', paddingBottom: 4 }}
style={{ paddingRight: 8, marginLeft: 8, textTransform: 'none', borderRadius: 0 }}
Expand Down Expand Up @@ -79,7 +85,7 @@ function Main() {
/>
</Route>
</Switch>
</Router>
</>
);
}
export default Main;
39 changes: 28 additions & 11 deletions dashboard/src/contexts/NetworkContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactNode, useContext, useEffect, useMemo, useState } from 'react';
import { useSettingsContext } from './SettingsContext';
import React, { ReactNode, useCallback, useContext, useEffect, useMemo } from 'react';
import { useHistory, useLocation } from 'react-router-dom';

export type Environment = 'mainnet' | 'testnet' | 'devnet';
export type Network = {
Expand All @@ -12,7 +12,7 @@ export type Network = {

type NetworkContextValue = {
currentNetwork: Network;
setCurrentNetwork: React.Dispatch<React.SetStateAction<Network>>;
setCurrentNetwork: (network: Network) => void;
};

// https://book.wormhole.com/reference/rpcnodes.html
Expand Down Expand Up @@ -107,23 +107,40 @@ export const networkOptions: Network[] = [
];

const defaultNetwork: Network = networkOptions[0];
const urlParamKey = 'endpoint';

const NetworkContext = React.createContext<NetworkContextValue>({
currentNetwork: defaultNetwork,
setCurrentNetwork: () => {},
});

export const NetworkContextProvider = ({ children }: { children: ReactNode }) => {
const {
settings: { defaultEndpoint },
updateDefaultEndpoint,
} = useSettingsContext();
const [currentNetwork, setCurrentNetwork] = useState<Network>(
networkOptions.find((option) => option.endpoint === defaultEndpoint) || defaultNetwork
const { push, replace } = useHistory();
const { search } = useLocation();
const { urlParams, urlNetwork, currentNetwork } = useMemo(() => {
const urlParams = new URLSearchParams(search);
const urlNetwork = urlParams.get(urlParamKey);
const currentNetwork =
networkOptions.find((option) => option.name === urlNetwork) || defaultNetwork;
return { urlParams, urlNetwork, currentNetwork };
}, [search]);
const setCurrentNetwork = useCallback(
(network: Network, shouldReplace?: boolean) => {
if (urlNetwork !== network.name) {
urlParams.set(urlParamKey, network.name);
if (shouldReplace) {
replace({ search: urlParams.toString() });
} else {
push({ search: urlParams.toString() });
}
}
},
[urlNetwork, urlParams, replace, push]
);
useEffect(() => {
updateDefaultEndpoint(currentNetwork.endpoint);
}, [currentNetwork, updateDefaultEndpoint]);
// sync initial / bad param with drop down, this will do nothing when the current network matches
setCurrentNetwork(currentNetwork, true);
}, [currentNetwork, setCurrentNetwork]);
const value = useMemo(
() => ({ currentNetwork, setCurrentNetwork }),
[currentNetwork, setCurrentNetwork]
Expand Down
8 changes: 0 additions & 8 deletions dashboard/src/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type Theme = 'light' | 'dark' | 'auto';
type Settings = {
backgroundUrl?: string;
backgroundOpacity?: number;
defaultEndpoint?: string;
theme: Theme;
showChainName?: boolean;
showAllMisses?: boolean;
Expand All @@ -18,7 +17,6 @@ type SettingsContextValue = {
settings: Settings;
updateBackgroundOpacity(value: number): void;
updateBackgroundUrl(value: string): void;
updateDefaultEndpoint(value: string): void;
updateTheme(value: Theme): void;
updateShowChainName(value: boolean): void;
updateShowAllMisses(value: boolean): void;
Expand Down Expand Up @@ -56,7 +54,6 @@ const SettingsContext = React.createContext<SettingsContextValue>({
settings: initialSettings,
updateBackgroundOpacity: (value: number) => {},
updateBackgroundUrl: (value: string) => {},
updateDefaultEndpoint: (value: string) => {},
updateTheme: (value: Theme) => {},
updateShowChainName: (value: boolean) => {},
updateShowAllMisses: (value: boolean) => {},
Expand All @@ -71,9 +68,6 @@ export const SettingsContextProvider = ({ children }: { children: ReactNode }) =
const updateBackgroundUrl = useCallback((value: string) => {
setSettings((settings) => ({ ...settings, backgroundUrl: value }));
}, []);
const updateDefaultEndpoint = useCallback((value: string) => {
setSettings((settings) => ({ ...settings, defaultEndpoint: value }));
}, []);
const updateTheme = useCallback((value: Theme) => {
setSettings((settings) => ({ ...settings, theme: value }));
}, []);
Expand All @@ -95,7 +89,6 @@ export const SettingsContextProvider = ({ children }: { children: ReactNode }) =
settings,
updateBackgroundOpacity,
updateBackgroundUrl,
updateDefaultEndpoint,
updateTheme,
updateShowChainName,
updateShowAllMisses,
Expand All @@ -105,7 +98,6 @@ export const SettingsContextProvider = ({ children }: { children: ReactNode }) =
settings,
updateBackgroundOpacity,
updateBackgroundUrl,
updateDefaultEndpoint,
updateTheme,
updateShowChainName,
updateShowAllMisses,
Expand Down

0 comments on commit fd563e1

Please sign in to comment.