shrub/pkg/grid/src/app.tsx

94 lines
2.6 KiB
TypeScript
Raw Normal View History

import React, { useCallback, 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';
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';
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-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();
useEffect(() => {
const query = new URLSearchParams(location.search);
if (query.has('grid-note')) {
const redir = getNoteRedirect(query.get('grid-note')!);
push(redir);
}
}, [location.search]);
const theme = useTheme();
const updateThemeClass = useCallback(
(e: MediaQueryListEvent) => {
if ((e.matches && theme === 'auto') || theme === 'dark') {
document.body.classList.add('dark');
useLocalState.setState({ currentTheme: 'dark' });
} else {
document.body.classList.remove('dark');
useLocalState.setState({ currentTheme: 'light' });
}
},
[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-09-17 03:45:06 +03:00
useEffect(() => {}, []);
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();
useContactState.getState().initialize(api);
2021-09-14 18:50:39 +03:00
useHarkStore.getState().initialize(api);
2021-08-23 07:58:29 +03:00
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-08-19 06:08:05 +03:00
<BrowserRouter basename={base}>
<AppRoutes />
</BrowserRouter>
);
}