graph.tsx 12.2 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
import type {Documentation, OpenedResult, Properties, SearchItem, SearchResult} from '~/resource/graph/types';
P
Peter Pan 已提交
19
import GraphComponent, {GraphRef} from '~/components/GraphPage/Graph';
20
import React, {FunctionComponent, useCallback, useEffect, useMemo, useRef, useState} from 'react';
21
import Select, {SelectProps} from '~/components/Select';
P
Peter Pan 已提交
22
import {actions, selectors} from '~/store';
23
import {primaryColor, rem, size} from '~/utils/style';
P
Peter Pan 已提交
24
import {useDispatch, useSelector} from 'react-redux';
25

P
Peter Pan 已提交
26
import type {BlobResponse} from '~/utils/fetch';
P
Peter Pan 已提交
27 28
import Button from '~/components/Button';
import Checkbox from '~/components/Checkbox';
29 30
import Content from '~/components/Content';
import Field from '~/components/Field';
31
import HashLoader from 'react-spinners/HashLoader';
P
Peter Pan 已提交
32 33 34
import ModelPropertiesDialog from '~/components/GraphPage/ModelPropertiesDialog';
import NodeDocumentationSidebar from '~/components/GraphPage/NodeDocumentationSidebar';
import NodePropertiesSidebar from '~/components/GraphPage/NodePropertiesSidebar';
P
Peter Pan 已提交
35 36
import RadioButton from '~/components/RadioButton';
import RadioGroup from '~/components/RadioGroup';
P
Peter Pan 已提交
37
import Search from '~/components/GraphPage/Search';
38
import Title from '~/components/Title';
P
Peter Pan 已提交
39
import Uploader from '~/components/GraphPage/Uploader';
40
import styled from 'styled-components';
41
import useRequest from '~/hooks/useRequest';
42
import {useTranslation} from 'react-i18next';
43

P
Peter Pan 已提交
44
const FullWidthButton = styled(Button)`
45 46 47
    width: 100%;
`;

48 49 50 51
const FullWidthSelect = styled<React.FunctionComponent<SelectProps<NonNullable<OpenedResult['selected']>>>>(Select)`
    width: 100%;
`;

P
Peter Pan 已提交
52
const ExportButtonWrapper = styled.div`
53
    display: flex;
P
Peter Pan 已提交
54
    justify-content: space-between;
55

P
Peter Pan 已提交
56 57
    > * {
        flex: 1 1 auto;
58

P
Peter Pan 已提交
59 60
        &:not(:last-child) {
            margin-right: ${rem(20)};
61
        }
62 63 64
    }
`;

P
Peter Pan 已提交
65 66 67 68 69
// TODO: better way to auto fit height
const SearchSection = styled(AsideSection)`
    max-height: calc(100% - ${rem(40)});
    display: flex;
    flex-direction: column;
70

P
Peter Pan 已提交
71 72 73 74
    &:not(:last-child) {
        padding-bottom: 0;
    }
`;
75

76 77 78 79 80 81 82 83 84 85 86 87
const Loading = styled.div`
    ${size('100%', '100%')}
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overscroll-behavior: none;
    cursor: progress;
    font-size: ${rem(16)};
    line-height: ${rem(60)};
`;

88
const Graph: FunctionComponent = () => {
P
Peter Pan 已提交
89
    const {t} = useTranslation(['graph', 'common']);
90

P
Peter Pan 已提交
91 92
    const storeDispatch = useDispatch();
    const storeModel = useSelector(selectors.graph.model);
93

P
Peter Pan 已提交
94 95
    const graph = useRef<GraphRef>(null);
    const file = useRef<HTMLInputElement>(null);
P
Peter Pan 已提交
96
    const [files, setFiles] = useState<FileList | File[] | null>(storeModel);
P
Peter Pan 已提交
97 98 99 100
    const onClickFile = useCallback(() => {
        if (file.current) {
            file.current.value = '';
            file.current.click();
101
        }
P
Peter Pan 已提交
102
    }, []);
103 104 105 106
    const onChangeFile = useCallback(
        (e: React.ChangeEvent<HTMLInputElement>) => {
            const target = e.target;
            if (target && target.files && target.files.length) {
P
Peter Pan 已提交
107
                storeDispatch(actions.graph.setModel(target.files));
108 109 110
                setFiles(target.files);
            }
        },
P
Peter Pan 已提交
111
        [storeDispatch]
112 113
    );

P
Peter Pan 已提交
114
    const {data, loading} = useRequest<BlobResponse>(files ? null : '/graph/graph');
115

116 117 118 119 120
    useEffect(() => {
        if (data?.data.size) {
            setFiles([new File([data.data], data.filename || 'unknwon_model')]);
        }
    }, [data]);
P
Peter Pan 已提交
121

122 123 124 125 126 127 128 129 130 131 132
    const [modelGraphs, setModelGraphs] = useState<OpenedResult['graphs']>([]);
    const [selectedGraph, setSelectedGraph] = useState<NonNullable<OpenedResult['selected']>>('');
    const setOpenedModel = useCallback((data: OpenedResult) => {
        setModelGraphs(data.graphs);
        setSelectedGraph(data.selected || '');
    }, []);
    const changeGraph = useCallback((name: string) => {
        setSelectedGraph(name);
        graph.current?.changeGraph(name);
    }, []);

P
Peter Pan 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    const [search, setSearch] = useState('');
    const [searching, setSearching] = useState(false);
    const [searchResult, setSearchResult] = useState<SearchResult>({text: '', result: []});
    const onSearch = useCallback((value: string) => {
        setSearch(value);
        graph.current?.search(value);
    }, []);
    const onSelect = useCallback((item: SearchItem) => {
        setSearch(item.name);
        graph.current?.select(item);
    }, []);

    const [showAttributes, setShowAttributes] = useState(false);
    const [showInitializers, setShowInitializers] = useState(true);
    const [showNames, setShowNames] = useState(false);
P
Peter Pan 已提交
148
    const [horizontal, setHorizontal] = useState(false);
P
Peter Pan 已提交
149 150 151 152 153

    const [modelData, setModelData] = useState<Properties | null>(null);
    const [nodeData, setNodeData] = useState<Properties | null>(null);
    const [nodeDocumentation, setNodeDocumentation] = useState<Documentation | null>(null);

154 155 156 157
    useEffect(() => {
        setSearch('');
        setSearchResult({text: '', result: []});
    }, [files, showAttributes, showInitializers, showNames]);
P
Peter Pan 已提交
158 159 160 161 162

    const bottom = useMemo(
        () =>
            searching ? null : (
                <FullWidthButton type="primary" rounded onClick={onClickFile}>
P
Peter Pan 已提交
163
                    {t('graph:change-model')}
P
Peter Pan 已提交
164
                </FullWidthButton>
165
            ),
P
Peter Pan 已提交
166
        [t, onClickFile, searching]
167 168
    );

P
Peter Pan 已提交
169 170 171
    const [rendered, setRendered] = useState(false);

    const aside = useMemo(() => {
172
        if (!rendered || loading) {
173 174
            return null;
        }
P
Peter Pan 已提交
175 176 177 178 179 180
        if (nodeDocumentation) {
            return (
                <Aside width={rem(360)}>
                    <NodeDocumentationSidebar data={nodeDocumentation} onClose={() => setNodeDocumentation(null)} />
                </Aside>
            );
181
        }
P
Peter Pan 已提交
182 183 184 185 186 187 188 189 190 191
        if (nodeData) {
            return (
                <Aside width={rem(360)}>
                    <NodePropertiesSidebar
                        data={nodeData}
                        onClose={() => setNodeData(null)}
                        showNodeDodumentation={() => graph.current?.showNodeDocumentation(nodeData)}
                    />
                </Aside>
            );
192 193
        }
        return (
P
Peter Pan 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            <Aside bottom={bottom}>
                <SearchSection>
                    <Search
                        text={search}
                        data={searchResult}
                        onChange={onSearch}
                        onSelect={onSelect}
                        onActive={() => setSearching(true)}
                        onDeactive={() => setSearching(false)}
                    />
                </SearchSection>
                {!searching && (
                    <>
                        <AsideSection>
                            <FullWidthButton onClick={() => graph.current?.showModelProperties()}>
P
Peter Pan 已提交
209
                                {t('graph:model-properties')}
P
Peter Pan 已提交
210 211
                            </FullWidthButton>
                        </AsideSection>
212 213 214 215 216 217 218
                        {modelGraphs.length > 1 && (
                            <AsideSection>
                                <Field label={t('graph:subgraph')}>
                                    <FullWidthSelect list={modelGraphs} value={selectedGraph} onChange={changeGraph} />
                                </Field>
                            </AsideSection>
                        )}
P
Peter Pan 已提交
219
                        <AsideSection>
P
Peter Pan 已提交
220
                            <Field label={t('graph:display-data')}>
P
Peter Pan 已提交
221 222
                                <div>
                                    <Checkbox value={showAttributes} onChange={setShowAttributes}>
P
Peter Pan 已提交
223
                                        {t('graph:show-attributes')}
P
Peter Pan 已提交
224 225 226 227
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showInitializers} onChange={setShowInitializers}>
P
Peter Pan 已提交
228
                                        {t('graph:show-initializers')}
P
Peter Pan 已提交
229 230 231 232
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showNames} onChange={setShowNames}>
P
Peter Pan 已提交
233
                                        {t('graph:show-node-names')}
P
Peter Pan 已提交
234 235 236 237
                                    </Checkbox>
                                </div>
                            </Field>
                        </AsideSection>
P
Peter Pan 已提交
238 239 240 241 242 243 244 245
                        <AsideSection>
                            <Field label={t('graph:direction')}>
                                <RadioGroup value={horizontal} onChange={setHorizontal}>
                                    <RadioButton value={false}>{t('graph:vertical')}</RadioButton>
                                    <RadioButton value={true}>{t('graph:horizontal')}</RadioButton>
                                </RadioGroup>
                            </Field>
                        </AsideSection>
P
Peter Pan 已提交
246
                        <AsideSection>
P
Peter Pan 已提交
247
                            <Field label={t('graph:export-file')}>
P
Peter Pan 已提交
248 249
                                <ExportButtonWrapper>
                                    <Button onClick={() => graph.current?.export('png')}>
P
Peter Pan 已提交
250
                                        {t('graph:export-png')}
P
Peter Pan 已提交
251 252
                                    </Button>
                                    <Button onClick={() => graph.current?.export('svg')}>
P
Peter Pan 已提交
253
                                        {t('graph:export-svg')}
P
Peter Pan 已提交
254 255 256 257 258 259 260
                                    </Button>
                                </ExportButtonWrapper>
                            </Field>
                        </AsideSection>
                    </>
                )}
            </Aside>
261
        );
P
Peter Pan 已提交
262 263 264 265 266 267
    }, [
        t,
        bottom,
        search,
        searching,
        searchResult,
268 269 270
        modelGraphs,
        selectedGraph,
        changeGraph,
P
Peter Pan 已提交
271 272 273 274 275
        onSearch,
        onSelect,
        showAttributes,
        showInitializers,
        showNames,
P
Peter Pan 已提交
276
        horizontal,
P
Peter Pan 已提交
277
        rendered,
278
        loading,
P
Peter Pan 已提交
279 280 281 282 283
        nodeData,
        nodeDocumentation
    ]);

    const uploader = useMemo(() => <Uploader onClickUpload={onClickFile} onDropFiles={setFiles} />, [onClickFile]);
284

285 286
    return (
        <>
P
Peter Pan 已提交
287
            <Title>{t('common:graph')}</Title>
P
Peter Pan 已提交
288 289
            <ModelPropertiesDialog data={modelData} onClose={() => setModelData(null)} />
            <Content aside={aside}>
290 291 292 293 294
                {loading ? (
                    <Loading>
                        <HashLoader size="60px" color={primaryColor} />
                    </Loading>
                ) : (
P
Peter Pan 已提交
295
                    <GraphComponent
296 297 298 299 300 301
                        ref={graph}
                        files={files}
                        uploader={uploader}
                        showAttributes={showAttributes}
                        showInitializers={showInitializers}
                        showNames={showNames}
P
Peter Pan 已提交
302
                        horizontal={horizontal}
303
                        onRendered={() => setRendered(true)}
304
                        onOpened={setOpenedModel}
305 306 307 308 309 310 311 312 313
                        onSearch={data => setSearchResult(data)}
                        onShowModelProperties={data => setModelData(data)}
                        onShowNodeProperties={data => {
                            setNodeData(data);
                            setNodeDocumentation(null);
                        }}
                        onShowNodeDocumentation={data => setNodeDocumentation(data)}
                    />
                )}
P
Peter Pan 已提交
314 315 316 317 318 319 320 321 322
                <input
                    ref={file}
                    type="file"
                    multiple={false}
                    onChange={onChangeFile}
                    style={{
                        display: 'none'
                    }}
                />
323 324 325 326 327
            </Content>
        </>
    );
};

P
Peter Pan 已提交
328
export default Graph;