high-dimensional.tsx 7.0 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.
 */

P
Peter Pan 已提交
17
import Aside, {AsideSection} from '~/components/Aside';
18 19
import type {Dimension, Reduction} from '~/resource/high-dimensional';
import React, {FunctionComponent, useEffect, useMemo, useState} from 'react';
20
import Select, {SelectProps} from '~/components/Select';
21 22 23
import {em, rem} from '~/utils/style';

import Checkbox from '~/components/Checkbox';
24
import Content from '~/components/Content';
25
import Error from '~/components/Error';
26
import Field from '~/components/Field';
27 28
import HighDimensionalChart from '~/components/HighDimensionalPage/HighDimensionalChart';
import Icon from '~/components/Icon';
29
import RadioButton from '~/components/RadioButton';
30
import RadioGroup from '~/components/RadioGroup';
31
import RunningToggle from '~/components/RunningToggle';
32
import SearchInput from '~/components/SearchInput';
33
import type {TagsData} from '~/types';
34 35
import Title from '~/components/Title';
import styled from 'styled-components';
P
Peter Pan 已提交
36
import useQuery from '~/hooks/useQuery';
P
Peter Pan 已提交
37
import {useRunningRequest} from '~/hooks/useRequest';
38
import useSearchValue from '~/hooks/useSearchValue';
39
import {useTranslation} from 'react-i18next';
40 41 42 43

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

44 45 46 47
const StyledSelect = styled<React.FunctionComponent<SelectProps<string>>>(Select)`
    min-width: ${em(160)};
`;

48 49 50 51 52 53 54 55 56 57 58 59
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)};
`;

60 61 62 63 64 65
type Item = {
    run: string;
    tag: string;
    label: string;
};

66
const HighDimensional: FunctionComponent = () => {
67 68
    const {t} = useTranslation(['high-dimensional', 'common']);

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

71
    const {data, error, loading} = useRunningRequest<TagsData>('/embedding/tags', running);
72 73

    const list = useMemo(() => {
74
        if (!data) {
75 76
            return [];
        }
77 78
        return data.runs.reduce<Item[]>(
            (p, run, i) => [...p, ...(data.tags[i]?.map(tag => ({run, tag, label: `${run}/${tag}`})) ?? [])],
79 80
            []
        );
81
    }, [data]);
82 83
    const labelList = useMemo(() => list.map(item => item.label), [list]);

P
Peter Pan 已提交
84
    const query = useQuery();
85 86 87 88 89 90 91
    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]);
92

93 94 95 96
    const selectedItem = useMemo(() => list.find(item => item.label === label) ?? {run: '', tag: '', label: ''}, [
        list,
        label
    ]);
97

98
    const [search, setSearch] = useState('');
99
    const debounceSearch = useSearchValue(search);
100
    const [dimension, setDimension] = useState(dimensions[0] as Dimension);
101
    const [reduction, setReduction] = useState(reductions[0] as Reduction);
102
    const [labelVisibility, setLabelVisibility] = useState(true);
103

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

    const aside = useMemo(
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        () =>
            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 已提交
156
        [t, bottom, dimension, label, labelList, labelVisibility, reduction, search]
157 158 159 160 161
    );

    return (
        <>
            <Title>{t('common:high-dimensional')}</Title>
162
            <Content aside={aside} loading={loading}>
163 164 165
                {!loading && !list.length ? (
                    <Error />
                ) : error ? (
166 167 168 169
                    <div>{t('common:error')}</div>
                ) : loading ? null : (
                    <HighDimensionalChart
                        dimension={dimension}
170
                        keyword={debounceSearch}
171 172
                        run={selectedItem.run}
                        tag={selectedItem.tag}
173 174 175 176 177
                        running={running}
                        labelVisibility={labelVisibility}
                        reduction={reduction}
                    />
                )}
178 179 180 181 182 183
            </Content>
        </>
    );
};

export default HighDimensional;