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

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

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

P
Peter Pan 已提交
29
const ExportButtonWrapper = styled.div`
30
    display: flex;
P
Peter Pan 已提交
31
    justify-content: space-between;
32

P
Peter Pan 已提交
33 34
    > * {
        flex: 1 1 auto;
35

P
Peter Pan 已提交
36 37
        &:not(:last-child) {
            margin-right: ${rem(20)};
38
        }
39 40 41
    }
`;

P
Peter Pan 已提交
42 43 44 45 46
// TODO: better way to auto fit height
const SearchSection = styled(AsideSection)`
    max-height: calc(100% - ${rem(40)});
    display: flex;
    flex-direction: column;
47

P
Peter Pan 已提交
48 49 50 51
    &:not(:last-child) {
        padding-bottom: 0;
    }
`;
52

53 54 55 56 57 58 59 60 61 62 63 64
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)};
`;

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

P
Peter Pan 已提交
68
    const {data, loading} = useRequest<BlobResponse>('/graph/graph', blobFetcher);
69

P
Peter Pan 已提交
70 71
    const graph = useRef<GraphRef>(null);
    const file = useRef<HTMLInputElement>(null);
72
    const [files, setFiles] = useState<FileList | File[] | null>(null);
P
Peter Pan 已提交
73 74 75 76
    const onClickFile = useCallback(() => {
        if (file.current) {
            file.current.value = '';
            file.current.click();
77
        }
P
Peter Pan 已提交
78 79 80 81 82
    }, []);
    const onChangeFile = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
        const target = e.target;
        if (target && target.files && target.files.length) {
            setFiles(target.files);
83
        }
P
Peter Pan 已提交
84
    }, []);
85 86 87 88 89
    useEffect(() => {
        if (data?.data.size) {
            setFiles([new File([data.data], data.filename || 'unknwon_model')]);
        }
    }, [data]);
P
Peter Pan 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    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 已提交
106
    const [horizontal, setHorizontal] = useState(false);
P
Peter Pan 已提交
107 108 109 110 111

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

112 113 114 115
    useEffect(() => {
        setSearch('');
        setSearchResult({text: '', result: []});
    }, [files, showAttributes, showInitializers, showNames]);
P
Peter Pan 已提交
116 117 118 119 120

    const bottom = useMemo(
        () =>
            searching ? null : (
                <FullWidthButton type="primary" rounded onClick={onClickFile}>
P
Peter Pan 已提交
121
                    {t('graph:change-model')}
P
Peter Pan 已提交
122
                </FullWidthButton>
123
            ),
P
Peter Pan 已提交
124
        [t, onClickFile, searching]
125 126
    );

P
Peter Pan 已提交
127 128 129
    const [rendered, setRendered] = useState(false);

    const aside = useMemo(() => {
130
        if (!rendered || loading) {
131 132
            return null;
        }
P
Peter Pan 已提交
133 134 135 136 137 138
        if (nodeDocumentation) {
            return (
                <Aside width={rem(360)}>
                    <NodeDocumentationSidebar data={nodeDocumentation} onClose={() => setNodeDocumentation(null)} />
                </Aside>
            );
139
        }
P
Peter Pan 已提交
140 141 142 143 144 145 146 147 148 149
        if (nodeData) {
            return (
                <Aside width={rem(360)}>
                    <NodePropertiesSidebar
                        data={nodeData}
                        onClose={() => setNodeData(null)}
                        showNodeDodumentation={() => graph.current?.showNodeDocumentation(nodeData)}
                    />
                </Aside>
            );
150 151
        }
        return (
P
Peter Pan 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            <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 已提交
167
                                {t('graph:model-properties')}
P
Peter Pan 已提交
168 169 170
                            </FullWidthButton>
                        </AsideSection>
                        <AsideSection>
P
Peter Pan 已提交
171
                            <Field label={t('graph:display-data')}>
P
Peter Pan 已提交
172 173
                                <div>
                                    <Checkbox value={showAttributes} onChange={setShowAttributes}>
P
Peter Pan 已提交
174
                                        {t('graph:show-attributes')}
P
Peter Pan 已提交
175 176 177 178
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showInitializers} onChange={setShowInitializers}>
P
Peter Pan 已提交
179
                                        {t('graph:show-initializers')}
P
Peter Pan 已提交
180 181 182 183
                                    </Checkbox>
                                </div>
                                <div>
                                    <Checkbox value={showNames} onChange={setShowNames}>
P
Peter Pan 已提交
184
                                        {t('graph:show-node-names')}
P
Peter Pan 已提交
185 186 187 188
                                    </Checkbox>
                                </div>
                            </Field>
                        </AsideSection>
P
Peter Pan 已提交
189 190 191 192 193 194 195 196
                        <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 已提交
197
                        <AsideSection>
P
Peter Pan 已提交
198
                            <Field label={t('graph:export-file')}>
P
Peter Pan 已提交
199 200
                                <ExportButtonWrapper>
                                    <Button onClick={() => graph.current?.export('png')}>
P
Peter Pan 已提交
201
                                        {t('graph:export-png')}
P
Peter Pan 已提交
202 203
                                    </Button>
                                    <Button onClick={() => graph.current?.export('svg')}>
P
Peter Pan 已提交
204
                                        {t('graph:export-svg')}
P
Peter Pan 已提交
205 206 207 208 209 210 211
                                    </Button>
                                </ExportButtonWrapper>
                            </Field>
                        </AsideSection>
                    </>
                )}
            </Aside>
212
        );
P
Peter Pan 已提交
213 214 215 216 217 218 219 220 221 222 223
    }, [
        t,
        bottom,
        search,
        searching,
        searchResult,
        onSearch,
        onSelect,
        showAttributes,
        showInitializers,
        showNames,
P
Peter Pan 已提交
224
        horizontal,
P
Peter Pan 已提交
225
        rendered,
226
        loading,
P
Peter Pan 已提交
227 228 229 230 231
        nodeData,
        nodeDocumentation
    ]);

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

233 234
    return (
        <>
P
Peter Pan 已提交
235
            <Title>{t('common:graph')}</Title>
P
Peter Pan 已提交
236 237
            <ModelPropertiesDialog data={modelData} onClose={() => setModelData(null)} />
            <Content aside={aside}>
238 239 240 241 242
                {loading ? (
                    <Loading>
                        <HashLoader size="60px" color={primaryColor} />
                    </Loading>
                ) : (
P
Peter Pan 已提交
243
                    <GraphComponent
244 245 246 247 248 249
                        ref={graph}
                        files={files}
                        uploader={uploader}
                        showAttributes={showAttributes}
                        showInitializers={showInitializers}
                        showNames={showNames}
P
Peter Pan 已提交
250
                        horizontal={horizontal}
251 252 253 254 255 256 257 258 259 260
                        onRendered={() => setRendered(true)}
                        onSearch={data => setSearchResult(data)}
                        onShowModelProperties={data => setModelData(data)}
                        onShowNodeProperties={data => {
                            setNodeData(data);
                            setNodeDocumentation(null);
                        }}
                        onShowNodeDocumentation={data => setNodeDocumentation(data)}
                    />
                )}
P
Peter Pan 已提交
261 262 263 264 265 266 267 268 269
                <input
                    ref={file}
                    type="file"
                    multiple={false}
                    onChange={onChangeFile}
                    style={{
                        display: 'none'
                    }}
                />
270 271 272 273 274
            </Content>
        </>
    );
};

P
Peter Pan 已提交
275
export default Graph;