App.tsx 4.5 KB
Newer Older
P
Peter Pan 已提交
1
import React, {FunctionComponent, Suspense, useCallback, useEffect, useMemo, useState} from 'react';
2
import {Redirect, Route, BrowserRouter as Router, Switch, useLocation} from 'react-router-dom';
P
Peter Pan 已提交
3
import {THEME, matchMedia} from '~/utils/theme';
4
import {headerHeight, position, size, zIndexes} from '~/utils/style';
5 6

import BodyLoading from '~/components/BodyLoading';
P
Peter Pan 已提交
7 8
import ErrorBoundary from '~/components/ErrorBoundary';
import ErrorPage from '~/pages/error';
9 10 11 12
import {Helmet} from 'react-helmet';
import NProgress from 'nprogress';
import Navbar from '~/components/Navbar';
import {SWRConfig} from 'swr';
P
Peter Pan 已提交
13
import {ToastContainer} from 'react-toastify';
P
Peter Pan 已提交
14
import {actions} from '~/store';
15 16 17 18
import {fetcher} from '~/utils/fetch';
import init from '@visualdl/wasm';
import routes from '~/routes';
import styled from 'styled-components';
P
Peter Pan 已提交
19
import {useDispatch} from 'react-redux';
20 21
import {useTranslation} from 'react-i18next';

22
const BASE_URI: string = import.meta.env.SNOWPACK_PUBLIC_BASE_URI;
23 24 25 26 27 28 29
const PUBLIC_PATH: string = import.meta.env.SNOWPACK_PUBLIC_PATH;

const Main = styled.main`
    padding-top: ${headerHeight};
`;

const Header = styled.header`
30
    z-index: ${zIndexes.header};
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

    ${size(headerHeight, '100%')}
    ${position('fixed', 0, 0, null, 0)}
`;

const defaultRoute = routes.find(route => route.default);
const routers = routes.reduce<Omit<typeof routes[number], 'children'>[]>((m, route) => {
    if (route.children) {
        m.push(...route.children);
    } else {
        m.push(route);
    }
    return m;
}, []);

const Progress: FunctionComponent = () => {
    useEffect(() => {
        NProgress.start();
        return () => {
            NProgress.done();
        };
    }, []);

    return null;
};

const Telemetry: FunctionComponent = () => {
    const location = useLocation();
    useEffect(() => {
P
Peter Pan 已提交
60
        window._hmt.push(['_trackPageview', BASE_URI + location.pathname]);
61 62 63 64 65
    }, [location.pathname]);
    return null;
};

const App: FunctionComponent = () => {
P
Peter Pan 已提交
66
    const {t, i18n} = useTranslation('errors');
67 68 69 70 71 72 73 74 75 76 77 78 79

    const dir = useMemo(() => (i18n.language ? i18n.dir(i18n.language) : ''), [i18n]);

    const [inited, setInited] = useState(false);
    useEffect(() => {
        (async () => {
            if (!inited) {
                await init(`${PUBLIC_PATH}/wasm/visualdl.wasm`);
                setInited(true);
            }
        })();
    }, [inited]);

P
Peter Pan 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    const dispatch = useDispatch();

    const toggleTheme = useCallback(
        (e: MediaQueryListEvent) => dispatch(actions.theme.setTheme(e.matches ? 'dark' : 'light')),
        [dispatch]
    );

    useEffect(() => {
        if (!THEME) {
            matchMedia.addEventListener('change', toggleTheme);
            return () => {
                matchMedia.removeEventListener('change', toggleTheme);
            };
        }
    }, [toggleTheme]);

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    return (
        <div className="app">
            <Helmet defaultTitle="VisualDL" titleTemplate="%s - VisualDL">
                <html lang={i18n.language} dir={dir} />
            </Helmet>
            <SWRConfig
                value={{
                    fetcher,
                    revalidateOnFocus: false,
                    revalidateOnReconnect: false
                }}
            >
                {!inited ? (
                    <BodyLoading />
                ) : (
                    <Main>
112
                        <Router basename={BASE_URI || '/'}>
113 114 115 116
                            <Telemetry />
                            <Header>
                                <Navbar />
                            </Header>
P
Peter Pan 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
                            <ErrorBoundary fallback={<ErrorPage />}>
                                <Suspense fallback={<Progress />}>
                                    <Switch>
                                        <Redirect exact from="/" to={defaultRoute?.path ?? '/index'} />
                                        {routers.map(route => (
                                            <Route key={route.id} path={route.path} component={route.component} />
                                        ))}
                                        <Route path="*">
                                            <ErrorPage title={t('errors:page-not-found')} />
                                        </Route>
                                    </Switch>
                                </Suspense>
                            </ErrorBoundary>
130 131 132
                        </Router>
                    </Main>
                )}
P
Peter Pan 已提交
133
                <ToastContainer />
134 135 136 137 138 139
            </SWRConfig>
        </div>
    );
};

export default App;