2021-09-01 21:57:12 +03:00
|
|
|
import React, { useCallback, 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-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 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-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();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const query = new URLSearchParams(location.search);
|
|
|
|
if (query.has('grid-note')) {
|
|
|
|
const redir = getNoteRedirect(query.get('grid-note')!);
|
|
|
|
push(redir);
|
|
|
|
}
|
|
|
|
}, [location.search]);
|
2021-09-15 00:15:29 +03:00
|
|
|
const theme = useTheme();
|
2021-09-01 21:57:12 +03:00
|
|
|
|
|
|
|
const updateThemeClass = useCallback(
|
|
|
|
(e: MediaQueryListEvent) => {
|
2021-09-15 00:32:27 +03:00
|
|
|
if ((e.matches && theme === 'auto') || theme === 'dark') {
|
2021-09-01 21:57:12 +03:00
|
|
|
document.body.classList.add('dark');
|
2021-09-15 00:15:29 +03:00
|
|
|
useLocalState.setState({ currentTheme: 'dark' });
|
2021-09-01 21:57:12 +03:00
|
|
|
} else {
|
|
|
|
document.body.classList.remove('dark');
|
2021-09-15 00:15:29 +03:00
|
|
|
useLocalState.setState({ currentTheme: 'light' });
|
2021-09-01 21:57:12 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[theme]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const query = window.matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
|
|
|
|
query.addEventListener('change', updateThemeClass);
|
|
|
|
updateThemeClass({ matches: query.matches } as MediaQueryListEvent);
|
|
|
|
return () => {
|
|
|
|
query.removeEventListener('change', updateThemeClass);
|
|
|
|
};
|
|
|
|
}, []);
|
2021-08-14 02:11:16 +03:00
|
|
|
|
2021-09-17 03:45:06 +03:00
|
|
|
useEffect(() => {}, []);
|
|
|
|
|
2021-08-14 02:11:16 +03:00
|
|
|
useEffect(() => {
|
|
|
|
window.name = 'grid';
|
|
|
|
|
2021-08-23 07:58:29 +03:00
|
|
|
const { fetchAllies, fetchCharges } = useDocketState.getState();
|
|
|
|
fetchCharges();
|
|
|
|
fetchAllies();
|
2021-09-08 06:04:03 +03:00
|
|
|
const { fetchVats, fetchLag } = useKilnState.getState();
|
|
|
|
fetchVats();
|
|
|
|
fetchLag();
|
2021-09-04 04:18:22 +03:00
|
|
|
useContactState.getState().initialize(api);
|
2021-09-14 18:50:39 +03:00
|
|
|
useHarkStore.getState().initialize(api);
|
2021-08-23 07:58:29 +03:00
|
|
|
|
2021-08-14 02:11:16 +03:00
|
|
|
Mousetrap.bind(['command+/', 'ctrl+/'], () => {
|
|
|
|
push('/leap/search');
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
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-08-19 06:08:05 +03:00
|
|
|
<BrowserRouter basename={base}>
|
|
|
|
<AppRoutes />
|
|
|
|
</BrowserRouter>
|
2021-08-14 02:11:16 +03:00
|
|
|
);
|
|
|
|
}
|