index.tsx 2.3 KB
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
import React, {FunctionComponent, useEffect} from 'react';
18
import {headerHeight, primaryColor, rem, size} from '~/utils/style';
P
Peter Pan 已提交
19
import {useHistory, useLocation} from 'react-router-dom';
20

P
Peter Pan 已提交
21
import Error from '~/components/Error';
P
Peter Pan 已提交
22 23 24
import HashLoader from 'react-spinners/HashLoader';
import styled from 'styled-components';
import useNavItems from '~/hooks/useNavItems';
25
import {useTranslation} from 'react-i18next';
P
Peter Pan 已提交
26

P
Peter Pan 已提交
27
const CenterWrapper = styled.div`
P
Peter Pan 已提交
28 29
    ${size(`calc(100vh - ${headerHeight})`, '100vw')}
    overscroll-behavior: none;
P
Peter Pan 已提交
30 31 32
`;

const Loading = styled.div`
33
    font-size: ${rem(16)};
P
Peter Pan 已提交
34
    height: 100%;
35
    line-height: ${rem(60)};
P
Peter Pan 已提交
36 37 38 39
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
P
Peter Pan 已提交
40
`;
41

42
const IndexPage: FunctionComponent = () => {
P
Peter Pan 已提交
43
    const [navItems, loading] = useNavItems();
44
    const history = useHistory();
P
Peter Pan 已提交
45

46 47
    const {t} = useTranslation('common');

P
Peter Pan 已提交
48 49
    const location = useLocation();

50
    useEffect(() => {
P
Peter Pan 已提交
51 52
        if (navItems.length) {
            if (navItems[0].path) {
P
Peter Pan 已提交
53
                history.replace(navItems[0].path + location.search);
P
Peter Pan 已提交
54
            } else if (navItems[0].children?.length && navItems[0].children[0].path) {
P
Peter Pan 已提交
55
                history.replace(navItems[0].children[0].path + location.search);
P
Peter Pan 已提交
56
            }
P
Peter Pan 已提交
57
        }
P
Peter Pan 已提交
58
    }, [navItems, history, location.search]);
59

P
Peter Pan 已提交
60
    return (
P
Peter Pan 已提交
61 62 63 64 65 66 67 68 69 70
        <CenterWrapper>
            {loading || navItems.length ? (
                <Loading>
                    <HashLoader size="60px" color={primaryColor} />
                    <span>{t('common:loading')}</span>
                </Loading>
            ) : (
                <Error />
            )}
        </CenterWrapper>
P
Peter Pan 已提交
71
    );
72 73
};

74
export default IndexPage;