-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProjectStatisticsPopup.tsx
287 lines (266 loc) · 9.38 KB
/
ProjectStatisticsPopup.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import { createTheme, Grid2 as MuiGrid, useTheme } from '@mui/material';
import { ThemeProvider } from '@mui/material/styles';
import MuiTab from '@mui/material/Tab';
import MuiTabs from '@mui/material/Tabs';
import MuiTypography from '@mui/material/Typography';
import MuiBox from '@mui/system/Box';
import { PropsWithChildren, useState } from 'react';
import { Criticality } from '../../../shared/shared-types';
import { text } from '../../../shared/text';
import { criticalityColor } from '../../shared-styles';
import { closePopup } from '../../state/actions/view-actions/view-actions';
import { useAppDispatch, useAppSelector } from '../../state/hooks';
import {
getClassifications,
getExternalAttributionSources,
getManualAttributions,
getUnresolvedExternalAttributions,
} from '../../state/selectors/resource-selectors';
import { useShowClassifications } from '../../state/variables/use-show-classifications';
import { useShowCriticality } from '../../state/variables/use-show-criticality';
import { useUserSetting } from '../../state/variables/use-user-setting';
import { AttributionCountPerSourcePerLicenseTable } from '../AttributionCountPerSourcePerLicenseTable/AttributionCountPerSourcePerLicenseTable';
import { BarChart } from '../BarChart/BarChart';
import { Checkbox } from '../Checkbox/Checkbox';
import { NotificationPopup } from '../NotificationPopup/NotificationPopup';
import { PieChart } from '../PieChart/PieChart';
import { ChartCard } from './ProjectStatisticsPopup.style';
import {
aggregateAttributionPropertiesFromAttributions,
aggregateLicensesAndSourcesFromAttributions,
CRITICALITY_LABEL,
getCriticalSignalsCount,
getIncompleteAttributionsCount,
getMostFrequentLicenses,
getSignalCountByClassification,
} from './ProjectStatisticsPopup.util';
const CRITICALITY_COLORS = {
[CRITICALITY_LABEL[Criticality.High]]: criticalityColor[Criticality.High],
[CRITICALITY_LABEL[Criticality.Medium]]: criticalityColor[Criticality.Medium],
[CRITICALITY_LABEL[Criticality.None]]: criticalityColor[Criticality.None],
};
export const ProjectStatisticsPopup: React.FC = () => {
const dispatch = useAppDispatch();
const manualAttributions = useAppSelector(getManualAttributions);
const attributionSources = useAppSelector(getExternalAttributionSources);
const classifications = useAppSelector(getClassifications);
const unresolvedExternalAttribution = useAppSelector(
getUnresolvedExternalAttributions,
);
const {
licenseCounts,
licenseNamesWithCriticality,
licenseNamesWithClassification,
} = aggregateLicensesAndSourcesFromAttributions(
unresolvedExternalAttribution,
attributionSources,
);
const mostFrequentLicenseCountData = getMostFrequentLicenses(licenseCounts);
const criticalSignalsCount = getCriticalSignalsCount(
licenseCounts,
licenseNamesWithCriticality,
);
const signalCountByClassification = getSignalCountByClassification(
licenseCounts,
licenseNamesWithClassification,
classifications,
);
const attributionBarChartData =
aggregateAttributionPropertiesFromAttributions(manualAttributions);
const incompleteAttributionsData =
getIncompleteAttributionsCount(manualAttributions);
function close(): void {
dispatch(closePopup());
}
const [showProjectStatistics, setShowProjectStatistics, hydrated] =
useUserSetting({ defaultValue: true, key: 'showProjectStatistics' });
const [selectedTab, setSelectedTab] = useState(0);
const showClassifications = useShowClassifications();
const showCriticality = useShowCriticality();
return (
<NotificationPopup
content={
<MuiBox
sx={{ display: 'flex', flexDirection: 'column', height: '100%' }}
>
<MuiTabs
value={selectedTab}
onChange={(_, tab) => setSelectedTab(tab)}
sx={{
marginBottom: '12px',
borderBottom: 1,
borderColor: 'divider',
}}
>
<MuiTab label={text.projectStatisticsPopup.tabs.overview} />
<MuiTab label={text.projectStatisticsPopup.tabs.details} />
</MuiTabs>
<TabPanel tabIndex={0} selectedTab={selectedTab}>
<ChartGrid>
<ChartGridItem testId={'attributionBarChart'}>
<MuiTypography variant="subtitle1">
{
text.projectStatisticsPopup.charts.attributionProperties
.title
}
</MuiTypography>
<BarChart data={attributionBarChartData} />
</ChartGridItem>
<ChartGridItem
shouldRender={mostFrequentLicenseCountData.length > 0}
testId={'mostFrequentLicenseCountPieChart'}
>
<MuiTypography variant="subtitle1">
{
text.projectStatisticsPopup.charts
.mostFrequentLicenseCountPieChart
}
</MuiTypography>
<PieChart segments={mostFrequentLicenseCountData} />
</ChartGridItem>
<ChartGridItem
shouldRender={
criticalSignalsCount.length > 0 && showCriticality
}
testId={'criticalSignalsCountPieChart'}
>
<MuiTypography variant="subtitle1">
{
text.projectStatisticsPopup.charts
.criticalSignalsCountPieChart.title
}
</MuiTypography>
<PieChart
segments={criticalSignalsCount}
colorMap={CRITICALITY_COLORS}
/>
</ChartGridItem>
<ChartGridItem
shouldRender={
signalCountByClassification.length > 0 && showClassifications
}
testId={'signalCountByClassificationPieChart'}
>
<MuiTypography variant="subtitle1">
{
text.projectStatisticsPopup.charts
.signalCountByClassificationPieChart.title
}
</MuiTypography>
<PieChart segments={signalCountByClassification} />
</ChartGridItem>
<ChartGridItem
shouldRender={incompleteAttributionsData.length > 0}
testId={'incompleteAttributionsPieChart'}
>
<MuiTypography variant="subtitle1">
{
text.projectStatisticsPopup.charts
.incompleteAttributionsPieChart.title
}
</MuiTypography>
<PieChart segments={incompleteAttributionsData} />
</ChartGridItem>
</ChartGrid>
</TabPanel>
<TabPanel tabIndex={1} selectedTab={selectedTab}>
<AttributionCountPerSourcePerLicenseTable
licenseCounts={licenseCounts}
licenseNamesWithCriticality={licenseNamesWithCriticality}
licenseNamesWithClassification={licenseNamesWithClassification}
/>
</TabPanel>
</MuiBox>
}
header={text.projectStatisticsPopup.title}
isOpen={true}
width={'min(95vw, max(550px, 85vw))'}
height={'min(95vh, max(550px, 75vh))'}
rightButtonConfig={{
onClick: close,
buttonText: text.buttons.close,
}}
onBackdropClick={close}
onEscapeKeyDown={close}
aria-label={'project statistics'}
customAction={
<Checkbox
checked={showProjectStatistics}
onChange={(event) => setShowProjectStatistics(event.target.checked)}
disabled={!hydrated}
label={text.projectStatisticsPopup.toggleStartupCheckbox}
/>
}
/>
);
};
interface TabPanelProps extends React.PropsWithChildren {
tabIndex: number;
selectedTab: number;
}
const TabPanel: React.FC<TabPanelProps> = (props) => {
return (
<MuiBox
sx={{
...(props.selectedTab !== props.tabIndex ? { display: 'none' } : {}),
flexGrow: 1,
overflowY: 'auto',
}}
>
{props.children}
</MuiBox>
);
};
const ChartGrid: React.FC<PropsWithChildren> = (props) => {
return (
<ThemeProvider
theme={createTheme({
...useTheme(),
breakpoints: {
values: {
xs: 0,
sm: 0,
md: 1200,
lg: 1725,
xl: 2000,
},
},
})}
>
<MuiGrid
height={'100%'}
minHeight={'fit-content'}
container
columns={{ sm: 1, md: 2, lg: 3 }}
spacing={3}
padding={'12px'}
paddingTop={'0px'}
alignContent={'flex-start'}
>
{props.children}
</MuiGrid>
</ThemeProvider>
);
};
interface ChartGridItemProps extends PropsWithChildren {
shouldRender?: boolean;
testId?: string;
}
const ChartGridItem: React.FC<ChartGridItemProps> = (props) => {
return (props.shouldRender ?? true) ? (
<MuiGrid
size={1}
minHeight={'220px'}
minWidth={'440px'}
height={'47%'}
display={'flex'}
data-testid={props.testId}
>
<ChartCard>{props.children}</ChartCard>
</MuiGrid>
) : null;
};