graph.tsx 11.5 KB
Newer Older
P
Peter Pan 已提交
1
import Aside, {AsideSection} from '~/components/Aside';
2
import type {Documentation, OpenedResult, Properties, SearchItem, SearchResult} from '~/resource/graph/types';
P
Peter Pan 已提交
3
import GraphComponent, {GraphRef} from '~/components/GraphPage/Graph';
4
import React, {FunctionComponent, useCallback, useEffect, useMemo, useRef, useState} from 'react';
5
import Select, {SelectProps} from '~/components/Select';
6
import {primaryColor, rem, size} from '~/utils/style';
7

P
Peter Pan 已提交
8
import type {BlobResponse} from '~/utils/fetch';
P
Peter Pan 已提交
9 10
import Button from '~/components/Button';
import Checkbox from '~/components/Checkbox';
11 12
import Content from '~/components/Content';
import Field from '~/components/Field';
13
import HashLoader from 'react-spinners/HashLoader';
P
Peter Pan 已提交
14 15 16
import ModelPropertiesDialog from '~/components/GraphPage/ModelPropertiesDialog';
import NodeDocumentationSidebar from '~/components/GraphPage/NodeDocumentationSidebar';
import NodePropertiesSidebar from '~/components/GraphPage/NodePropertiesSidebar';
P
Peter Pan 已提交
17 18
import RadioButton from '~/components/RadioButton';
import RadioGroup from '~/components/RadioGroup';
P
Peter Pan 已提交
19
import Search from '~/components/GraphPage/Search';
20
import Title from '~/components/Title';
P
Peter Pan 已提交
21
import Uploader from '~/components/GraphPage/Uploader';
22
import styled from 'styled-components';
23
import useGlobalState from '~/hooks/useGlobalState';
24
import useRequest from '~/hooks/useRequest';
25
import {useTranslation} from 'react-i18next';
26

P
Peter Pan 已提交
27
const FullWidthButton = styled(Button)`
28 29 30
    width: 100%;
`;

31 32 33 34
const FullWidthSelect = styled<React.FunctionComponent<SelectProps<NonNullable<OpenedResult['selected']>>>>(Select)`
    width: 100%;
`;

P
Peter Pan 已提交
35
const ExportButtonWrapper = styled.div`
36
    display: flex;
P
Peter Pan 已提交
37
    justify-content: space-between;
38

P
Peter Pan 已提交
39 40
    > * {
        flex: 1 1 auto;
41

P
Peter Pan 已提交
42 43
        &:not(:last-child) {
            margin-right: ${rem(20)};
44
        }
45 46 47
    }
`;

P
Peter Pan 已提交
48 49 50 51 52
// TODO: better way to auto fit height
const SearchSection = styled(AsideSection)`
    max-height: calc(100% - ${rem(40)});
    display: flex;
    flex-direction: column;
53

P
Peter Pan 已提交
54 55 56 57
    &:not(:last-child) {
        padding-bottom: 0;
    }
`;
58

59 60 61 62 63 64 65 66 67 68 69 70
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)};
`;

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

74
    const [globalState, globalDispatch] = useGlobalState();
75

P
Peter Pan 已提交
76 77
    const graph = useRef<GraphRef>(null);
    const file = useRef<HTMLInputElement>(null);
78
    const [files, setFiles] = useState<FileList | File[] | null>(globalState.model);
P
Peter Pan 已提交
79 80 81 82
    const onClickFile = useCallback(() => {
        if (file.current) {
            file.current.value = '';
            file.current.click();
83
        }
P
Peter Pan 已提交
84
    }, []);
85 86 87 88 89 90 91 92 93 94 95
    const onChangeFile = useCallback(
        (e: React.ChangeEvent<HTMLInputElement>) => {
            const target = e.target;
            if (target && target.files && target.files.length) {
                globalDispatch({model: target.files});
                setFiles(target.files);
            }
        },
        [globalDispatch]
    );

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

98 99 100 101 102
    useEffect(() => {
        if (data?.data.size) {
            setFiles([new File([data.data], data.filename || 'unknwon_model')]);
        }
    }, [data]);
P
Peter Pan 已提交
103

104 105 106 107 108 109 110 111 112 113 114
    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 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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 已提交
130
    const [horizontal, setHorizontal] = useState(false);
P
Peter Pan 已提交
131 132 133 134 135

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

136 137 138 139
    useEffect(() => {
        setSearch('');
        setSearchResult({text: '', result: []});
    }, [files, showAttributes, showInitializers, showNames]);
P
Peter Pan 已提交
140 141 142 143 144

    const bottom = useMemo(
        () =>
            searching ? null : (
                <FullWidthButton type="primary" rounded onClick={onClickFile}>
P
Peter Pan 已提交
145
                    {t('graph:change-model')}
P
Peter Pan 已提交
146
                </FullWidthButton>
147
            ),
P
Peter Pan 已提交
148
        [t, onClickFile, searching]
149 150
    );

P
Peter Pan 已提交
151 152 153
    const [rendered, setRendered] = useState(false);

    const aside = useMemo(() => {
154
        if (!rendered || loading) {
155 156
            return null;
        }
P
Peter Pan 已提交
157 158 159 160 161 162
        if (nodeDocumentation) {
            return (
                <Aside width={rem(360)}>
                    <NodeDocumentationSidebar data={nodeDocumentation} onClose={() => setNodeDocumentation(null)} />
                </Aside>
            );
163
        }
P
Peter Pan 已提交
164 165 166 167 168 169 170 171 172 173
        if (nodeData) {
            return (
                <Aside width={rem(360)}>
                    <NodePropertiesSidebar
                        data={nodeData}
                        onClose={() => setNodeData(null)}
                        showNodeDodumentation={() => graph.current?.showNodeDocumentation(nodeData)}
                    />
                </Aside>
            );
174 175
        }
        return (
P
Peter Pan 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            <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 已提交
191
                                {t('graph:model-properties')}
P
Peter Pan 已提交
192 193
                            </FullWidthButton>
                        </AsideSection>
194 195 196 197 198 199 200
                        {modelGraphs.length > 1 && (
                            <AsideSection>
                                <Field label={t('graph:subgraph')}>
                                    <FullWidthSelect list={modelGraphs} value={selectedGraph} onChange={changeGraph} />
                                </Field>
                            </AsideSection>
                        )}
P
Peter Pan 已提交
201
                        <AsideSection>
P
Peter Pan 已提交
202
                            <Field label={t('graph:display-data')}>
P
Peter Pan 已提交
203 204
                                <div>
                                    <Checkbox value={showAttributes} onChange={setShowAttributes}>
P
Peter Pan 已提交
205
                                        {t('graph:show-attributes')}
P
Peter Pan 已提交
206 207 208 209
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showInitializers} onChange={setShowInitializers}>
P
Peter Pan 已提交
210
                                        {t('graph:show-initializers')}
P
Peter Pan 已提交
211 212 213 214
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showNames} onChange={setShowNames}>
P
Peter Pan 已提交
215
                                        {t('graph:show-node-names')}
P
Peter Pan 已提交
216 217 218 219
                                    </Checkbox>
                                </div>
                            </Field>
                        </AsideSection>
P
Peter Pan 已提交
220 221 222 223 224 225 226 227
                        <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 已提交
228
                        <AsideSection>
P
Peter Pan 已提交
229
                            <Field label={t('graph:export-file')}>
P
Peter Pan 已提交
230 231
                                <ExportButtonWrapper>
                                    <Button onClick={() => graph.current?.export('png')}>
P
Peter Pan 已提交
232
                                        {t('graph:export-png')}
P
Peter Pan 已提交
233 234
                                    </Button>
                                    <Button onClick={() => graph.current?.export('svg')}>
P
Peter Pan 已提交
235
                                        {t('graph:export-svg')}
P
Peter Pan 已提交
236 237 238 239 240 241 242
                                    </Button>
                                </ExportButtonWrapper>
                            </Field>
                        </AsideSection>
                    </>
                )}
            </Aside>
243
        );
P
Peter Pan 已提交
244 245 246 247 248 249
    }, [
        t,
        bottom,
        search,
        searching,
        searchResult,
250 251 252
        modelGraphs,
        selectedGraph,
        changeGraph,
P
Peter Pan 已提交
253 254 255 256 257
        onSearch,
        onSelect,
        showAttributes,
        showInitializers,
        showNames,
P
Peter Pan 已提交
258
        horizontal,
P
Peter Pan 已提交
259
        rendered,
260
        loading,
P
Peter Pan 已提交
261 262 263 264 265
        nodeData,
        nodeDocumentation
    ]);

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

267 268
    return (
        <>
P
Peter Pan 已提交
269
            <Title>{t('common:graph')}</Title>
P
Peter Pan 已提交
270 271
            <ModelPropertiesDialog data={modelData} onClose={() => setModelData(null)} />
            <Content aside={aside}>
272 273 274 275 276
                {loading ? (
                    <Loading>
                        <HashLoader size="60px" color={primaryColor} />
                    </Loading>
                ) : (
P
Peter Pan 已提交
277
                    <GraphComponent
278 279 280 281 282 283
                        ref={graph}
                        files={files}
                        uploader={uploader}
                        showAttributes={showAttributes}
                        showInitializers={showInitializers}
                        showNames={showNames}
P
Peter Pan 已提交
284
                        horizontal={horizontal}
285
                        onRendered={() => setRendered(true)}
286
                        onOpened={setOpenedModel}
287 288 289 290 291 292 293 294 295
                        onSearch={data => setSearchResult(data)}
                        onShowModelProperties={data => setModelData(data)}
                        onShowNodeProperties={data => {
                            setNodeData(data);
                            setNodeDocumentation(null);
                        }}
                        onShowNodeDocumentation={data => setNodeDocumentation(data)}
                    />
                )}
P
Peter Pan 已提交
296 297 298 299 300 301 302 303 304
                <input
                    ref={file}
                    type="file"
                    multiple={false}
                    onChange={onChangeFile}
                    style={{
                        display: 'none'
                    }}
                />
305 306 307 308 309
            </Content>
        </>
    );
};

P
Peter Pan 已提交
310
export default Graph;