-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSwitchableProcessBar.tsx
124 lines (112 loc) · 3.61 KB
/
SwitchableProcessBar.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
// 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 { MenuItem, Select, SelectChangeEvent } from '@mui/material';
import MuiBox from '@mui/material/Box';
import { SxProps } from '@mui/system';
import React, { useState } from 'react';
import { text as fullText } from '../../../shared/text';
import { OpossumColors } from '../../shared-styles';
import { useProgressData } from '../../state/variables/use-progress-data';
import { useShowClassifications } from '../../state/variables/use-show-classifications';
import { useShowCriticality } from '../../state/variables/use-show-criticality';
import { SelectedProgressBar } from '../../types/types';
import { ProgressBar } from '../ProgressBar/ProgressBar';
const classes = {
container: {
flex: 1,
display: 'flex',
marginLeft: '12px',
marginRight: '12px',
gap: '4px',
marginBottom: '4px',
marginTop: '4px',
},
select: {
width: '150px',
backgroundColor: OpossumColors.lightestBlue,
minHeight: 'unset !important',
},
progressBar: {
flex: 1,
},
} satisfies SxProps;
const text = fullText.topBar.switchableProgressBar;
interface ProgressBarSwitchConfiguration {
label: string;
active: boolean;
}
export const SwitchableProcessBar: React.FC = () => {
const showClassifications = useShowClassifications();
const showCriticality = useShowCriticality();
const switchableProgressBarConfiguration: Record<
SelectedProgressBar,
ProgressBarSwitchConfiguration
> = {
attribution: {
label: text.attributionProgressBar.selectLabel,
active: true,
},
criticality: {
label: text.criticalSignalsBar.selectLabel,
active: showCriticality,
},
classification: {
label: text.classificationProgressBar.selectLabel,
active: showClassifications,
},
};
const [currentProgressBar, setcurrentProgressBar] =
useState<SelectedProgressBar>('attribution');
const [progressData] = useProgressData();
const handleProgressBarChange = (
event: SelectChangeEvent<SelectedProgressBar>,
): void => {
setcurrentProgressBar(event.target.value as SelectedProgressBar);
};
const effectiveCurrentProgressBar = switchableProgressBarConfiguration[
currentProgressBar
].active
? currentProgressBar
: 'attribution';
const activeProgressBarConfigurations = Object.fromEntries(
Object.entries(switchableProgressBarConfiguration).filter(
([_, progressBarSwitchConfiguration]) =>
progressBarSwitchConfiguration.active,
),
);
const moreThanOneActiveProgressBars =
Object.keys(activeProgressBarConfigurations).length > 1;
if (!progressData) {
return <MuiBox flex={1} />;
}
return (
<MuiBox sx={classes.container}>
<ProgressBar
sx={classes.progressBar}
progressBarData={progressData}
selectedProgressBar={effectiveCurrentProgressBar}
/>
{moreThanOneActiveProgressBars && (
<Select<SelectedProgressBar>
size={'small'}
onChange={handleProgressBarChange}
sx={classes.select}
value={effectiveCurrentProgressBar}
aria-label={text.selectAriaLabel}
>
{Object.entries(activeProgressBarConfigurations).map(
([key, progressBarSwitchConfiguration]) => {
return (
<MenuItem key={key} value={key}>
{progressBarSwitchConfiguration.label}
</MenuItem>
);
},
)}
</Select>
)}
</MuiBox>
);
};