histogram.tsx 2.7 KB
Newer Older
P
Peter Pan 已提交
1 2
import ChartPage, {WithChart} from '~/components/ChartPage';
import {Modes, modes} from '~/resource/histogram';
3
import React, {FunctionComponent, useCallback, useMemo, useState} from 'react';
P
Peter Pan 已提交
4 5 6 7 8 9 10 11 12

import {AsideSection} from '~/components/Aside';
import Content from '~/components/Content';
import Error from '~/components/Error';
import Field from '~/components/Field';
import HistogramChart from '~/components/HistogramPage/HistogramChart';
import RadioButton from '~/components/RadioButton';
import RadioGroup from '~/components/RadioGroup';
import RunAside from '~/components/RunAside';
13
import type {TagWithSingleRun} from '~/types';
P
Peter Pan 已提交
14 15
import Title from '~/components/Title';
import useTagFilter from '~/hooks/useTagFilter';
16
import {useTranslation} from 'react-i18next';
P
Peter Pan 已提交
17

18
const Histogram: FunctionComponent = () => {
P
Peter Pan 已提交
19 20 21 22
    const {t} = useTranslation(['histogram', 'common']);

    const [running, setRunning] = useState(true);

23
    const {runs, tagsWithSingleRun, selectedRuns, onChangeRuns, loading} = useTagFilter('histogram', running);
P
Peter Pan 已提交
24 25 26 27 28 29 30 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 60

    const [mode, setMode] = useState<Modes>(Modes.Offset);

    const aside = useMemo(
        () =>
            runs.length ? (
                <RunAside
                    runs={runs}
                    selectedRuns={selectedRuns}
                    onChangeRuns={onChangeRuns}
                    running={running}
                    onToggleRunning={setRunning}
                >
                    <AsideSection>
                        <Field label={t('histogram:mode')}>
                            <RadioGroup value={mode} onChange={setMode}>
                                {modes.map(value => (
                                    <RadioButton key={value} value={value}>
                                        {t(`histogram:mode-value.${value}`)}
                                    </RadioButton>
                                ))}
                            </RadioGroup>
                        </Field>
                    </AsideSection>
                </RunAside>
            ) : null,
        [t, mode, onChangeRuns, running, runs, selectedRuns]
    );

    const withChart = useCallback<WithChart<TagWithSingleRun>>(
        ({label, run, ...args}) => <HistogramChart run={run} tag={label} {...args} mode={mode} running={running} />,
        [running, mode]
    );

    return (
        <>
            <Title>{t('common:histogram')}</Title>
61 62
            <Content aside={aside} loading={loading}>
                {!loading && !runs.length ? (
P
Peter Pan 已提交
63 64
                    <Error />
                ) : (
65
                    <ChartPage items={tagsWithSingleRun} withChart={withChart} loading={loading} />
P
Peter Pan 已提交
66 67 68 69 70 71 72
                )}
            </Content>
        </>
    );
};

export default Histogram;