prometheus/web/ui/react-app/src/App.tsx
Levi Harrison eb8ca06885
React UI: Add Starting Screen to Individual Pages (#8909)
* Fix/removed forwarding

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Added global 'wasReady' and 'wasUnexpected'

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Eslint fixes

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Added withStartingIndicator wrapper

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Fixed condition

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Removed unused import

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Moved withStartingIndicator calls to pages index

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Fixed withStartingIndicator tests

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Fixed eslint (maybe?)

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Trailing comma

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Added prettier ignore

Signed-off-by: Levi Harrison <git@leviharrison.dev>

* Fix eslint (pt. 2)

Signed-off-by: Levi Harrison <git@leviharrison.dev>
2021-06-15 22:37:16 +02:00

97 lines
3 KiB
TypeScript
Executable file

import React, { FC } from 'react';
import Navigation from './Navbar';
import { Container } from 'reactstrap';
import { Router, Redirect } from '@reach/router';
import useMedia from 'use-media';
import {
AlertsPage,
ConfigPage,
FlagsPage,
RulesPage,
ServiceDiscoveryPage,
StatusPage,
TargetsPage,
TSDBStatusPage,
PanelListPage,
} from './pages';
import { PathPrefixContext } from './contexts/PathPrefixContext';
import { ThemeContext, themeName, themeSetting } from './contexts/ThemeContext';
import { Theme, themeLocalStorageKey } from './Theme';
import { useLocalStorage } from './hooks/useLocalStorage';
interface AppProps {
consolesLink: string | null;
}
const App: FC<AppProps> = ({ consolesLink }) => {
// This dynamically/generically determines the pathPrefix by stripping the first known
// endpoint suffix from the window location path. It works out of the box for both direct
// hosting and reverse proxy deployments with no additional configurations required.
let basePath = window.location.pathname;
const paths = [
'/graph',
'/alerts',
'/status',
'/tsdb-status',
'/flags',
'/config',
'/rules',
'/targets',
'/service-discovery',
];
if (basePath.endsWith('/')) {
basePath = basePath.slice(0, -1);
}
if (basePath.length > 1) {
for (let i = 0; i < paths.length; i++) {
if (basePath.endsWith(paths[i])) {
basePath = basePath.slice(0, basePath.length - paths[i].length);
break;
}
}
}
const [userTheme, setUserTheme] = useLocalStorage<themeSetting>(themeLocalStorageKey, 'auto');
const browserHasThemes = useMedia('(prefers-color-scheme)');
const browserWantsDarkTheme = useMedia('(prefers-color-scheme: dark)');
let theme: themeName;
if (userTheme !== 'auto') {
theme = userTheme;
} else {
theme = browserHasThemes ? (browserWantsDarkTheme ? 'dark' : 'light') : 'light';
}
return (
<ThemeContext.Provider
value={{ theme: theme, userPreference: userTheme, setTheme: (t: themeSetting) => setUserTheme(t) }}
>
<Theme />
<PathPrefixContext.Provider value={basePath}>
<Navigation consolesLink={consolesLink} />
<Container fluid style={{ paddingTop: 70 }}>
<Router basepath={`${basePath}`}>
<Redirect from="/" to={`graph`} noThrow />
{/*
NOTE: Any route added here needs to also be added to the list of
React-handled router paths ("reactRouterPaths") in /web/web.go.
*/}
<PanelListPage path="/graph" />
<AlertsPage path="/alerts" />
<ConfigPage path="/config" />
<FlagsPage path="/flags" />
<RulesPage path="/rules" />
<ServiceDiscoveryPage path="/service-discovery" />
<StatusPage path="/status" />
<TSDBStatusPage path="/tsdb-status" />
<TargetsPage path="/targets" />
</Router>
</Container>
</PathPrefixContext.Provider>
</ThemeContext.Provider>
);
};
export default App;