high-dimensional.tsx 6.4 KB
Newer Older
P
Peter Pan 已提交
1
import Aside, {AsideSection} from '~/components/Aside';
2 3
import type {Dimension, Reduction} from '~/resource/high-dimensional';
import React, {FunctionComponent, useEffect, useMemo, useState} from 'react';
4
import Select, {SelectProps} from '~/components/Select';
5 6 7
import {em, rem} from '~/utils/style';

import Checkbox from '~/components/Checkbox';
8
import Content from '~/components/Content';
9
import Error from '~/components/Error';
10
import Field from '~/components/Field';
11 12
import HighDimensionalChart from '~/components/HighDimensionalPage/HighDimensionalChart';
import Icon from '~/components/Icon';
13
import RadioButton from '~/components/RadioButton';
14
import RadioGroup from '~/components/RadioGroup';
15
import RunningToggle from '~/components/RunningToggle';
16
import SearchInput from '~/components/SearchInput';
17
import type {TagsData} from '~/types';
18 19
import Title from '~/components/Title';
import styled from 'styled-components';
P
Peter Pan 已提交
20
import useQuery from '~/hooks/useQuery';
P
Peter Pan 已提交
21
import {useRunningRequest} from '~/hooks/useRequest';
22
import useSearchValue from '~/hooks/useSearchValue';
23
import {useTranslation} from 'react-i18next';
24 25 26 27

const dimensions = ['2d', '3d'];
const reductions = ['pca', 'tsne'];

28 29 30 31
const StyledSelect = styled<React.FunctionComponent<SelectProps<string>>>(Select)`
    min-width: ${em(160)};
`;

32 33 34 35 36 37 38 39 40 41 42 43
const StyledIcon = styled(Icon)`
    margin-right: ${em(4)};
    vertical-align: middle;
`;

const AsideTitle = styled.div`
    font-size: ${rem(16)};
    line-height: ${rem(16)};
    font-weight: 700;
    margin-bottom: ${rem(10)};
`;

44 45 46 47 48 49
type Item = {
    run: string;
    tag: string;
    label: string;
};

50
const HighDimensional: FunctionComponent = () => {
51 52
    const {t} = useTranslation(['high-dimensional', 'common']);

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

55
    const {data, error, loading} = useRunningRequest<TagsData>('/embedding/tags', running);
56 57

    const list = useMemo(() => {
58
        if (!data) {
59 60
            return [];
        }
61 62
        return data.runs.reduce<Item[]>(
            (p, run, i) => [...p, ...(data.tags[i]?.map(tag => ({run, tag, label: `${run}/${tag}`})) ?? [])],
63 64
            []
        );
65
    }, [data]);
66 67
    const labelList = useMemo(() => list.map(item => item.label), [list]);

P
Peter Pan 已提交
68
    const query = useQuery();
69 70 71 72 73 74 75
    const selectedLabel = useMemo(() => {
        const run = Array.isArray(query.run) ? query.run[0] : query.run;
        return (run && list.find(item => item.run === run)?.label) ?? list[0]?.label;
    }, [query.run, list]);

    const [label, setLabel] = useState(selectedLabel);
    useEffect(() => setLabel(selectedLabel), [selectedLabel]);
76

77 78 79 80
    const selectedItem = useMemo(() => list.find(item => item.label === label) ?? {run: '', tag: '', label: ''}, [
        list,
        label
    ]);
81

82
    const [search, setSearch] = useState('');
83
    const debounceSearch = useSearchValue(search);
84
    const [dimension, setDimension] = useState(dimensions[0] as Dimension);
85
    const [reduction, setReduction] = useState(reductions[0] as Reduction);
86
    const [labelVisibility, setLabelVisibility] = useState(true);
87

P
Peter Pan 已提交
88 89 90
    const bottom = useMemo(() => <RunningToggle running={running} onToggle={setRunning} />, [running, setRunning]);

    const aside = useMemo(
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        () =>
            labelList.length ? (
                <Aside bottom={bottom}>
                    <AsideSection>
                        <AsideTitle>{t('common:select-runs')}</AsideTitle>
                        <StyledSelect list={labelList} value={label} onChange={setLabel} />
                    </AsideSection>
                    <AsideSection>
                        <Field>
                            <SearchInput placeholder={t('common:search')} value={search} onChange={setSearch} />
                        </Field>
                        <Field>
                            <Checkbox value={labelVisibility} onChange={setLabelVisibility}>
                                {t('high-dimensional:display-all-label')}
                            </Checkbox>
                        </Field>
                    </AsideSection>
                    <AsideSection>
                        <AsideTitle>
                            <StyledIcon type="dimension" />
                            {t('high-dimensional:dimension')}
                        </AsideTitle>
                        <Field>
                            <RadioGroup value={dimension} onChange={value => setDimension(value)}>
                                {dimensions.map(item => (
                                    <RadioButton key={item} value={item}>
                                        {t(item)}
                                    </RadioButton>
                                ))}
                            </RadioGroup>
                        </Field>
                    </AsideSection>
                    <AsideSection>
                        <AsideTitle>
                            <StyledIcon type="reduction" />
                            {t('high-dimensional:reduction-method')}
                        </AsideTitle>
                        <Field>
                            <RadioGroup value={reduction} onChange={value => setReduction(value)}>
                                {reductions.map(item => (
                                    <RadioButton key={item} value={item}>
                                        {t(item)}
                                    </RadioButton>
                                ))}
                            </RadioGroup>
                        </Field>
                    </AsideSection>
                </Aside>
            ) : null,
P
Peter Pan 已提交
140
        [t, bottom, dimension, label, labelList, labelVisibility, reduction, search]
141 142 143 144 145
    );

    return (
        <>
            <Title>{t('common:high-dimensional')}</Title>
146
            <Content aside={aside} loading={loading}>
147 148 149
                {!loading && !list.length ? (
                    <Error />
                ) : error ? (
150 151 152 153
                    <div>{t('common:error')}</div>
                ) : loading ? null : (
                    <HighDimensionalChart
                        dimension={dimension}
154
                        keyword={debounceSearch}
155 156
                        run={selectedItem.run}
                        tag={selectedItem.tag}
157 158 159 160 161
                        running={running}
                        labelVisibility={labelVisibility}
                        reduction={reduction}
                    />
                )}
162 163 164 165 166 167
            </Content>
        </>
    );
};

export default HighDimensional;