-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnames-browse.tsx
202 lines (173 loc) · 6.34 KB
/
names-browse.tsx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useEffect, useState, useCallback } from 'react';
import { useAppSelector as useSelector } from '../../hooks';
import { makeStyles } from '@material-ui/core/styles';
import ProjectHeader from 'etna-js/components/project-header';
import { selectRulesByName } from '../../selectors/rules';
import { fetchNamesWithRuleAndRegexFromMagma } from '../../utils/names';
import { createFnConcurrencyWrapper } from '../../utils/async';
import { useDispatch } from '../../utils/redux';
import { setMagmaNamesListRequest } from '../../actions/names';
import { fetchAndAddRulesFromMagma } from '../../actions/rules';
import { selectMagmaNamesListsByRuleName } from '../../selectors/names';
import NamesToolbar from '../names-toolbar/toolbar';
import ExportButton from '../names-toolbar/export-button';
import DeleteIdentifiersButton from '../names-toolbar/delete-identifiers-button';
import Counts, { Count } from '../names-create/counts';
import Table from '../table';
const useStyles = makeStyles((theme) => {
const gridSize = '70';
const gridSizeSm = '95';
return {
projectAndToolbarContainer: {
display: 'flex',
borderBottom: '1px solid #eee',
'& > :first-child, & > :last-child': {
flex: '1'
},
'& > .placeholder': {
visibility: 'hidden',
},
},
countsList: {
marginTop: '0.5em',
textAlign: 'center'
},
selectedCounts: {
display: 'inline-block',
'& .count, & .separator': {
display: 'inline-block',
},
'& .separator': {
margin: '0 0.5em'
},
'& .count-not-ready': {
color: 'red',
},
},
tableContainer: {
display: 'flex',
justifyContent: 'center',
margin: '5.5em 0',
},
table: {
width: `${gridSize}vw`,
height: `${gridSize}vh`,
[theme.breakpoints.down('sm')]: {
width: `${gridSizeSm}vw`,
},
},
};
});
export interface Name {
name: string
ruleName: string
author: string
createdAt: string
}
const NamesBrowse = ({ project_name }: { project_name: string }) => {
const classes = useStyles();
const dispatch = useDispatch();
const [selection, setSelection] = useState<Name[]>([]);
const [rowData, setRowData] = useState<Name[]>([]);
const allRules = Object.keys(useSelector(selectRulesByName));
const magmaNamesListsByRuleName = useSelector(selectMagmaNamesListsByRuleName);
useEffect(() => {
async function addRulesFromMagma() {
dispatch(await fetchAndAddRulesFromMagma(project_name));
}
addRulesFromMagma();
}, []);
const fetchAllNames = useCallback(() => {
const fetchNamesForRule = createFnConcurrencyWrapper(fetchNamesWithRuleAndRegexFromMagma, 4);
async function fetchNamesFromMagma(ruleName: string) {
try {
const response = await fetchNamesForRule(project_name, ruleName);
dispatch(setMagmaNamesListRequest(
{ status: 'success', response },
ruleName,
));
} catch (error) {
dispatch(setMagmaNamesListRequest(
{ status: 'error', statusMessage: String(error) },
ruleName,
));
}
}
allRules.forEach(ruleName => fetchNamesFromMagma(ruleName));
}, [allRules]);
useEffect( () => {
fetchAllNames()
}, [allRules.length]);
useEffect(() => {
const newRowData: Name[] = [];
for (const [ruleName, namesListRequest] of Object.entries(magmaNamesListsByRuleName)) {
if (namesListRequest.status != 'success' || namesListRequest.response == undefined) {
continue;
}
for (const magmaName of namesListRequest.response) {
newRowData.push({
name: magmaName.identifier,
ruleName,
author: magmaName.author,
createdAt: magmaName.name_created_at,
});
}
}
setRowData(newRowData);
}, [magmaNamesListsByRuleName]);
const renderCounts = () => {
const counts: Count[] = [{
name: 'selected',
description: 'selected',
value: selection.length,
hideAtZero: true,
}];
return (
<div className={classes.countsList}>
<Counts
counts={counts}
className={classes.selectedCounts}
/>
</div>
);
};
return (
<React.Fragment>
<div className={classes.projectAndToolbarContainer}>
<ProjectHeader project_name={project_name} />
<NamesToolbar
buttons={[
<ExportButton
small={true}
data={selection.length ? selection : rowData}
buttonText={`Export${selection.length ? ' Selection' : ''}`}
/>,
<DeleteIdentifiersButton
small={true}
data={selection}
refresh={ () => {
fetchAllNames();
setSelection([]);
} }
project_name={project_name}
buttonText={`Delete${selection.length ? ' Selection' : ''}`}
/>
]}
/>
<ProjectHeader project_name={project_name} className="placeholder" />
</div>
{renderCounts()}
<div className={classes.tableContainer}>
<Table
rows={rowData}
columns={['name', 'ruleName', 'author', 'createdAt']}
selectable={true}
onSelectionChanged={setSelection}
className={classes.table}
dataTypeLabel='name'
/>
</div>
</React.Fragment>
);
};
export default NamesBrowse;