samples.tsx 4.0 KB
Newer Older
1 2
// cSpell:words ungrouped

3
import ChartPage, {WithChart} from '~/components/ChartPage';
4 5 6
import {NextI18NextPage, useTranslation} from '~/utils/i18n';
import React, {useCallback, useMemo, useState} from 'react';

P
Peter Pan 已提交
7
import {AsideSection} from '~/components/Aside';
8
import Checkbox from '~/components/Checkbox';
9
import Content from '~/components/Content';
10
import Error from '~/components/Error';
11
import Field from '~/components/Field';
12
import Preloader from '~/components/Preloader';
13
import RunAside from '~/components/RunAside';
14
import SampleChart from '~/components/SamplesPage/SampleChart';
15
import Slider from '~/components/Slider';
16
import Title from '~/components/Title';
17
import {rem} from '~/utils/style';
18
import useTagFilter from '~/hooks/useTagFilter';
19

20 21 22
const chartSize = {
    height: rem(406)
};
23 24 25 26 27 28

type Item = {
    run: string;
    label: string;
};

29
const Samples: NextI18NextPage = () => {
30 31
    const {t} = useTranslation(['samples', 'common']);

P
Peter Pan 已提交
32 33
    const [running, setRunning] = useState(true);

34 35
    const {runs, tags, selectedRuns, onChangeRuns, loadingRuns, loadingTags} = useTagFilter('images', running);

36 37
    const ungroupedSelectedTags = useMemo(
        () =>
38
            tags.reduce<Item[]>((prev, {runs, ...item}) => {
39 40
                Array.prototype.push.apply(
                    prev,
41
                    runs.map(run => ({...item, run: run.label, id: `${item.label}-${run.label}`}))
42 43
                );
                return prev;
44
            }, []),
45
        [tags]
46 47 48
    );

    const [showActualSize, setShowActualSize] = useState(false);
49 50
    const [brightness, setBrightness] = useState(1);
    const [contrast, setContrast] = useState(1);
51

P
Peter Pan 已提交
52
    const aside = useMemo(
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        () =>
            runs.length ? (
                <RunAside
                    runs={runs}
                    selectedRuns={selectedRuns}
                    onChangeRuns={onChangeRuns}
                    running={running}
                    onToggleRunning={setRunning}
                >
                    <AsideSection>
                        <Checkbox value={showActualSize} onChange={setShowActualSize}>
                            {t('samples:show-actual-size')}
                        </Checkbox>
                    </AsideSection>
                    <AsideSection>
                        <Field label={t('samples:brightness')}>
                            <Slider min={0} max={2} step={0.01} value={brightness} onChange={setBrightness} />
                        </Field>
                    </AsideSection>
                    <AsideSection>
                        <Field label={t('samples:contrast')}>
                            <Slider min={0} max={2} step={0.01} value={contrast} onChange={setContrast} />
                        </Field>
                    </AsideSection>
                </RunAside>
            ) : null,
P
Peter Pan 已提交
79
        [t, brightness, contrast, onChangeRuns, running, runs, selectedRuns, showActualSize]
80 81
    );

82 83 84 85 86 87 88 89 90 91 92 93
    const withChart = useCallback<WithChart<Item>>(
        ({run, label}) => (
            <SampleChart
                run={run}
                tag={label}
                fit={!showActualSize}
                running={running}
                brightness={brightness}
                contrast={contrast}
            />
        ),
        [showActualSize, running, brightness, contrast]
94 95 96 97
    );

    return (
        <>
P
Peter Pan 已提交
98 99
            <Preloader url="/runs" />
            <Preloader url="/images/tags" />
100
            <Title>{t('common:samples')}</Title>
P
Peter Pan 已提交
101
            <Content aside={aside} loading={loadingRuns}>
102 103 104 105 106 107 108 109 110 111
                {!loadingRuns && !runs.length ? (
                    <Error />
                ) : (
                    <ChartPage
                        items={ungroupedSelectedTags}
                        chartSize={chartSize}
                        withChart={withChart}
                        loading={loadingRuns || loadingTags}
                    />
                )}
112 113 114 115 116
            </Content>
        </>
    );
};

117 118 119 120
Samples.getInitialProps = () => ({
    namespacesRequired: ['samples', 'common']
});

121
export default Samples;