summaryrefslogtreecommitdiff
path: root/buildscripts/libdeps/graph_visualizer_web_stack/src/NodeList.js
blob: 17c73a3cc28f461f03903c03a2e164b8b47747c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
176
177
178
179
import React from "react";

import { connect } from "react-redux";
import { getNodes } from "./redux/store";
import { setFindNode } from "./redux/findNode";

import DataGrid from "./DataGrid";
import LoadingBar from "./LoadingBar";
import TextField from "@material-ui/core/TextField";

import { setNodes, updateCheckbox, updateSelected } from "./redux/nodes";
import { setNodeInfos } from "./redux/nodeInfo";
import { setGraphData } from "./redux/graphData";
import { setLinks } from "./redux/links";
import { setLinksTrans } from "./redux/linksTrans";
import { setLoading } from "./redux/loading";
import { setListSearchTerm } from "./redux/listSearchTerm";
import { Button, Autocomplete, Grid } from "@material-ui/core";

const {REACT_APP_API_URL} = process.env;

const columns = [
  { dataKey: "check", label: "Selected", width: 70 },
  { dataKey: "name", label: "Name", width: 200 },
  { id: "ID", dataKey: "node", label: "Node", width: 200 },
];

const NodeList = ({ selectedGraph, nodes, searchedNodes, loading, setFindNode, setNodeInfos, setNodes, setLinks, setLinksTrans, setLoading, setListSearchTerm, updateCheckbox, updateSelected, setGraphData, showTransitive}) => {
  const [searchPath, setSearchPath] = React.useState('');

  React.useEffect(() => {
    let gitHash = selectedGraph;
    if (gitHash) {
      fetch(REACT_APP_API_URL + '/api/graphs/' + gitHash + '/nodes')
        .then(response => response.json())
        .then(data => {
          setNodes(data.nodes.map((node, index) => {
            return {
              id: index,
              node: node,
              name: node.substring(node.lastIndexOf('/') + 1),
              check: "checkbox",
              selected: false,
            };
          }));
          setLoading(false);
        });
      setSearchPath(null);
      setListSearchTerm('');
    }
  }, [selectedGraph]);

  function newGraphData() {
    let gitHash = selectedGraph;
    if (gitHash) {
      let postData = {
          "selected_nodes": nodes.filter(node => node.selected == true).map(node => node.node),
          "transitive_edges": showTransitive
      };
      fetch(REACT_APP_API_URL + '/api/graphs/' + gitHash + '/d3', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(postData)
      })
        .then(response => response.json())
        .then(data => {
          setGraphData(data.graphData);
          setLinks(data.graphData.links);
          setLinksTrans(data.graphData.links_trans);
        });
      fetch(REACT_APP_API_URL + '/api/graphs/' + gitHash + '/nodes/details', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(postData)
      })
        .then(response => response.json())
        .then(data => {
          setNodeInfos(data.nodeInfos);
        });
    }
  }

  function nodePaths() {
    const paths = nodes.map(node => node.node.substring(0, node.node.lastIndexOf('/') + 1));
    return [...new Set(paths)];
  }

  function handleRowClick(event) {
    setFindNode(event.target.textContent);
  }

  function handleSelectAll(event) {
    searchedNodes.forEach(node => {
      updateCheckbox({ node: node.id, value: "flip" });
      updateSelected({ index: node.id, value: true });
    });
    newGraphData();
  }

  function handleDeselectAll(event) {
    searchedNodes.forEach(node => {
      updateCheckbox({ node: node.id, value: "flip" });
      updateSelected({ index: node.id, value: false });
    });
    newGraphData();
  }

  function handleSearchTermChange(event, newTerm) {
    if (newTerm == null) {
      setSearchPath('');
      setListSearchTerm('');
    } else {
      setSearchPath(newTerm);
      setListSearchTerm(newTerm);
    }
  }

  return (
    <LoadingBar loading={loading} height={"95%"}>
      <Grid container spacing={2}>
        <Grid item xs={12}/>
        <Grid item xs={12}>
          <Autocomplete
            fullWidth
            freeSolo
            ListboxProps={{ style: { maxHeight: "9rem" } }}
            value={searchPath}
            onInputChange={handleSearchTermChange}
            onChange={handleSearchTermChange}
            options={nodePaths()}
            renderInput={(params) => <TextField {...params}
              label="Search by Path or Name"
              variant="outlined"
              />}
          />
        </Grid>
        <Grid item xs={12}>
          <Grid
            container
            direction="row"
            justifyContent="center"
            spacing={4}
          >
            <Grid item>
              <Button
                variant="contained"
                onClick={handleSelectAll}
              >
                Select All
              </Button>
            </Grid>
            <Grid item>
              <Button
                variant="contained"
                onClick={handleDeselectAll}
              >
                Deselect All
              </Button>
            </Grid>
          </Grid>
        </Grid>
        <Grid item xs={12}/>
      </Grid>
      <DataGrid
        rows={nodes}
        columns={columns}
        rowHeight={30}
        headerHeight={35}
        onNodeClicked={handleRowClick}
      />
    </LoadingBar>
  );
};

export default connect(getNodes, { setFindNode, setNodes, setNodeInfos, setLinks, setLinksTrans, setLoading, setListSearchTerm, updateCheckbox, updateSelected, setGraphData })(NodeList);