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

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 17
import ModelPropertiesDialog from '~/components/GraphsPage/ModelPropertiesDialog';
import NodeDocumentationSidebar from '~/components/GraphsPage/NodeDocumentationSidebar';
import NodePropertiesSidebar from '~/components/GraphsPage/NodePropertiesSidebar';
import Search from '~/components/GraphsPage/Search';
18
import Title from '~/components/Title';
P
Peter Pan 已提交
19
import Uploader from '~/components/GraphsPage/Uploader';
20
import styled from 'styled-components';
21
import useRequest from '~/hooks/useRequest';
22

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

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

P
Peter Pan 已提交
31 32
    > * {
        flex: 1 1 auto;
33

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

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

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

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

P
Peter Pan 已提交
63 64
const Graphs: NextI18NextPage = () => {
    const {t} = useTranslation(['graphs', 'common']);
65

66 67
    const {data, loading} = useRequest<BlobResponse>('/graphs/graph', blobFetcher);

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

    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);

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

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

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

P
Peter Pan 已提交
124 125 126
    const [rendered, setRendered] = useState(false);

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

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

221 222 223
    return (
        <>
            <Title>{t('common:graphs')}</Title>
P
Peter Pan 已提交
224 225
            <ModelPropertiesDialog data={modelData} onClose={() => setModelData(null)} />
            <Content aside={aside}>
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
                {loading ? (
                    <Loading>
                        <HashLoader size="60px" color={primaryColor} />
                    </Loading>
                ) : (
                    <Graph
                        ref={graph}
                        files={files}
                        uploader={uploader}
                        showAttributes={showAttributes}
                        showInitializers={showInitializers}
                        showNames={showNames}
                        onRendered={() => setRendered(true)}
                        onSearch={data => setSearchResult(data)}
                        onShowModelProperties={data => setModelData(data)}
                        onShowNodeProperties={data => {
                            setNodeData(data);
                            setNodeDocumentation(null);
                        }}
                        onShowNodeDocumentation={data => setNodeDocumentation(data)}
                    />
                )}
P
Peter Pan 已提交
248 249 250 251 252 253 254 255 256
                <input
                    ref={file}
                    type="file"
                    multiple={false}
                    onChange={onChangeFile}
                    style={{
                        display: 'none'
                    }}
                />
257 258 259 260 261
            </Content>
        </>
    );
};

262 263 264 265
Graphs.getInitialProps = () => ({
    namespacesRequired: ['graphs', 'common']
});

266
export default Graphs;