index.tsx 1.7 KB
Newer Older
1
import React, {FunctionComponent, useEffect} from 'react';
2
import {headerHeight, primaryColor, rem, size} from '~/utils/style';
P
Peter Pan 已提交
3
import {useHistory, useLocation} from 'react-router-dom';
4

P
Peter Pan 已提交
5
import Error from '~/components/Error';
P
Peter Pan 已提交
6 7 8
import HashLoader from 'react-spinners/HashLoader';
import styled from 'styled-components';
import useNavItems from '~/hooks/useNavItems';
9
import {useTranslation} from 'react-i18next';
P
Peter Pan 已提交
10

P
Peter Pan 已提交
11
const CenterWrapper = styled.div`
P
Peter Pan 已提交
12 13
    ${size(`calc(100vh - ${headerHeight})`, '100vw')}
    display: flex;
14
    flex-direction: column;
P
Peter Pan 已提交
15 16 17
    justify-content: center;
    align-items: center;
    overscroll-behavior: none;
P
Peter Pan 已提交
18 19 20
`;

const Loading = styled.div`
21 22
    font-size: ${rem(16)};
    line-height: ${rem(60)};
P
Peter Pan 已提交
23
`;
24

25
const IndexPage: FunctionComponent = () => {
P
Peter Pan 已提交
26
    const [navItems, loading] = useNavItems();
27
    const history = useHistory();
P
Peter Pan 已提交
28

29 30
    const {t} = useTranslation('common');

P
Peter Pan 已提交
31 32
    const location = useLocation();

33
    useEffect(() => {
P
Peter Pan 已提交
34 35
        if (navItems.length) {
            if (navItems[0].path) {
P
Peter Pan 已提交
36
                history.replace(navItems[0].path + location.search);
P
Peter Pan 已提交
37
            } else if (navItems[0].children?.length && navItems[0].children[0].path) {
P
Peter Pan 已提交
38
                history.replace(navItems[0].children[0].path + location.search);
P
Peter Pan 已提交
39
            }
P
Peter Pan 已提交
40
        }
P
Peter Pan 已提交
41
    }, [navItems, history, location.search]);
42

P
Peter Pan 已提交
43
    return (
P
Peter Pan 已提交
44 45 46 47 48 49 50 51 52 53
        <CenterWrapper>
            {loading || navItems.length ? (
                <Loading>
                    <HashLoader size="60px" color={primaryColor} />
                    <span>{t('common:loading')}</span>
                </Loading>
            ) : (
                <Error />
            )}
        </CenterWrapper>
P
Peter Pan 已提交
54
    );
55 56
};

57
export default IndexPage;