2021-09-14 03:09:32 +03:00
|
|
|
import React, { useEffect } from 'react';
|
2021-08-14 02:11:16 +03:00
|
|
|
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';
|
2021-08-14 02:11:16 +03:00
|
|
|
import { Grid } from './pages/Grid';
|
2021-08-23 07:58:29 +03:00
|
|
|
import useDocketState from './state/docket';
|
2021-09-10 03:22:43 +03:00
|
|
|
import { PermalinkRoutes } from './pages/PermalinkRoutes';
|
2021-09-08 06:04:03 +03:00
|
|
|
import useKilnState from './state/kiln';
|
2021-09-04 04:18:22 +03:00
|
|
|
import useContactState from './state/contact';
|
|
|
|
import api from './state/api';
|
2021-09-14 03:09:32 +03:00
|
|
|
import { useMedia } from './logic/useMedia';
|
2021-09-14 18:50:39 +03:00
|
|
|
import { useHarkStore } from './state/hark';
|
2021-09-15 00:15:29 +03:00
|
|
|
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-08-14 02:11:16 +03:00
|
|
|
|
2021-09-17 03:45:06 +03:00
|
|
|
const getNoteRedirect = (path: string) => {
|
|
|
|
if (path.startsWith('/desk/')) {
|
|
|
|
const [, , desk] = path.split('/');
|
|
|
|
return `/app/${desk}`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2021-08-14 02:11:16 +03:00
|
|
|
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(() => {
|
2021-09-17 05:57:56 +03:00
|
|
|
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);
|
|
|
|
}
|
2021-09-17 05:57:56 +03:00
|
|
|
}, [search]);
|
2021-09-23 22:35:55 +03:00
|
|
|
|
2021-09-15 00:15:29 +03:00
|
|
|
const theme = useTheme();
|
2021-09-14 03:09:32 +03:00
|
|
|
const isDarkMode = useMedia('(prefers-color-scheme: dark)');
|
2021-09-01 21:57:12 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-09-16 04:41:02 +03:00
|
|
|
if ((isDarkMode && theme === 'auto') || theme === 'dark') {
|
2021-09-14 03:09:32 +03:00
|
|
|
document.body.classList.add('dark');
|
2021-09-16 04:41:02 +03:00
|
|
|
useLocalState.setState({ currentTheme: 'dark' });
|
2021-09-14 03:09:32 +03:00
|
|
|
} else {
|
|
|
|
document.body.classList.remove('dark');
|
2021-09-16 04:41:02 +03:00
|
|
|
useLocalState.setState({ currentTheme: 'light' });
|
2021-09-14 03:09:32 +03:00
|
|
|
}
|
|
|
|
}, [isDarkMode, theme]);
|
2021-08-14 02:11:16 +03:00
|
|
|
|
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-08-14 02:11:16 +03:00
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
[]
|
|
|
|
);
|
2021-08-14 02:11:16 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Switch>
|
2021-09-08 06:04:03 +03:00
|
|
|
<Route path="/perma" component={PermalinkRoutes} />
|
2021-08-14 02:11:16 +03:00
|
|
|
<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>
|
2021-08-14 02:11:16 +03:00
|
|
|
);
|
|
|
|
}
|