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

P
Peter Pan 已提交
7 8
import Button from '~/components/Button';
import Checkbox from '~/components/Checkbox';
9 10
import Content from '~/components/Content';
import Field from '~/components/Field';
P
Peter Pan 已提交
11 12 13 14
import ModelPropertiesDialog from '~/components/GraphsPage/ModelPropertiesDialog';
import NodeDocumentationSidebar from '~/components/GraphsPage/NodeDocumentationSidebar';
import NodePropertiesSidebar from '~/components/GraphsPage/NodePropertiesSidebar';
import Search from '~/components/GraphsPage/Search';
15
import Title from '~/components/Title';
P
Peter Pan 已提交
16
import Uploader from '~/components/GraphsPage/Uploader';
17 18
import {rem} from '~/utils/style';
import styled from 'styled-components';
19

P
Peter Pan 已提交
20
const FullWidthButton = styled(Button)`
21 22 23
    width: 100%;
`;

P
Peter Pan 已提交
24
const ExportButtonWrapper = styled.div`
25
    display: flex;
P
Peter Pan 已提交
26
    justify-content: space-between;
27

P
Peter Pan 已提交
28 29
    > * {
        flex: 1 1 auto;
30

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

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

P
Peter Pan 已提交
43 44 45 46
    &:not(:last-child) {
        padding-bottom: 0;
    }
`;
47

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

P
Peter Pan 已提交
51 52 53 54 55 56 57
    const graph = useRef<GraphRef>(null);
    const file = useRef<HTMLInputElement>(null);
    const [files, setFiles] = useState<FileList | null>(null);
    const onClickFile = useCallback(() => {
        if (file.current) {
            file.current.value = '';
            file.current.click();
58
        }
P
Peter Pan 已提交
59 60 61 62 63
    }, []);
    const onChangeFile = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
        const target = e.target;
        if (target && target.files && target.files.length) {
            setFiles(target.files);
64
        }
P
Peter Pan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    }, []);

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

    useEffect(() => setSearch(''), [showAttributes, showInitializers, showNames]);

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

P
Peter Pan 已提交
99 100 101 102
    const [rendered, setRendered] = useState(false);

    const aside = useMemo(() => {
        if (!rendered) {
103 104
            return null;
        }
P
Peter Pan 已提交
105 106 107 108 109 110
        if (nodeDocumentation) {
            return (
                <Aside width={rem(360)}>
                    <NodeDocumentationSidebar data={nodeDocumentation} onClose={() => setNodeDocumentation(null)} />
                </Aside>
            );
111
        }
P
Peter Pan 已提交
112 113 114 115 116 117 118 119 120 121
        if (nodeData) {
            return (
                <Aside width={rem(360)}>
                    <NodePropertiesSidebar
                        data={nodeData}
                        onClose={() => setNodeData(null)}
                        showNodeDodumentation={() => graph.current?.showNodeDocumentation(nodeData)}
                    />
                </Aside>
            );
122 123
        }
        return (
P
Peter Pan 已提交
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            <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>
176
        );
P
Peter Pan 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }, [
        t,
        bottom,
        search,
        searching,
        searchResult,
        onSearch,
        onSelect,
        showAttributes,
        showInitializers,
        showNames,
        rendered,
        nodeData,
        nodeDocumentation
    ]);

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

195 196 197
    return (
        <>
            <Title>{t('common:graphs')}</Title>
P
Peter Pan 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            <ModelPropertiesDialog data={modelData} onClose={() => setModelData(null)} />
            <Content aside={aside}>
                <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)}
                />
                <input
                    ref={file}
                    type="file"
                    multiple={false}
                    onChange={onChangeFile}
                    style={{
                        display: 'none'
                    }}
                />
225 226 227 228 229
            </Content>
        </>
    );
};

230 231 232 233
Graphs.getInitialProps = () => ({
    namespacesRequired: ['graphs', 'common']
});

234
export default Graphs;