shrub/pkg/grid/src/app.tsx

96 lines
2.7 KiB
TypeScript
Raw Normal View History

import React, { useEffect } from 'react';
import Mousetrap from 'mousetrap';
2021-09-17 03:45:06 +03:00
import { BrowserRouter, Switch, Route, useHistory, useLocation } from 'react-router-dom';
2021-09-23 22:35:55 +03:00
import { ErrorBoundary } from 'react-error-boundary';
import { Grid } from './pages/Grid';
2021-08-23 07:58:29 +03:00
import useDocketState from './state/docket';
import { PermalinkRoutes } from './pages/PermalinkRoutes';
2021-09-08 06:04:03 +03:00
import useKilnState from './state/kiln';
import useContactState from './state/contact';
import api from './state/api';
import { useMedia } from './logic/useMedia';
2021-09-14 18:50:39 +03:00
import { useHarkStore } from './state/hark';
import { useTheme } from './state/settings';
import { useLocalState } from './state/local';
2021-09-23 22:35:55 +03:00
import { ErrorAlert } from './components/ErrorAlert';
import { useErrorHandler } from './logic/useErrorHandler';
2021-09-17 03:45:06 +03:00
const getNoteRedirect = (path: string) => {
if (path.startsWith('/desk/')) {
const [, , desk] = path.split('/');
return `/app/${desk}`;
}
return '';
};
const AppRoutes = () => {
const { push } = useHistory();
2021-09-17 03:45:06 +03:00
const { search } = useLocation();
2021-09-23 22:35:55 +03:00
const handleError = useErrorHandler();
2021-09-17 03:45:06 +03:00
useEffect(() => {
const query = new URLSearchParams(search);
2021-09-17 03:45:06 +03:00
if (query.has('grid-note')) {
const redir = getNoteRedirect(query.get('grid-note')!);
push(redir);
}
}, [search]);
2021-09-23 22:35:55 +03:00
const theme = useTheme();
const isDarkMode = useMedia('(prefers-color-scheme: dark)');
useEffect(() => {
if ((isDarkMode && theme === 'auto') || theme === 'dark') {
document.body.classList.add('dark');
useLocalState.setState({ currentTheme: 'dark' });
} else {
document.body.classList.remove('dark');
useLocalState.setState({ currentTheme: 'light' });
}
}, [isDarkMode, theme]);
2021-09-17 03:45:06 +03:00
useEffect(() => {}, []);
2021-09-23 22:35:55 +03:00
useEffect(
handleError(() => {
window.name = 'grid';
const { fetchDefaultAlly, fetchAllies, fetchCharges } = useDocketState.getState();
fetchDefaultAlly();
fetchCharges();
fetchAllies();
2021-09-23 22:35:55 +03:00
const { fetchVats, fetchLag } = useKilnState.getState();
fetchVats();
fetchLag();
2021-08-23 07:58:29 +03:00
2021-09-23 22:35:55 +03:00
useContactState.getState().initialize(api);
useHarkStore.getState().initialize(api);
Mousetrap.bind(['command+/', 'ctrl+/'], () => {
push('/leap/search');
});
}),
[]
);
return (
<Switch>
2021-09-08 06:04:03 +03:00
<Route path="/perma" component={PermalinkRoutes} />
<Route path={['/leap/:menu', '/']} component={Grid} />
</Switch>
);
};
export function App() {
const base = import.meta.env.MODE === 'mock' ? undefined : '/apps/grid';
return (
2021-09-23 22:35:55 +03:00
<ErrorBoundary FallbackComponent={ErrorAlert} onReset={() => window.location.reload()}>
<BrowserRouter basename={base}>
<AppRoutes />
</BrowserRouter>
</ErrorBoundary>
);
}