2
2
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
3
3
//
4
4
// SPDX-License-Identifier: Apache-2.0
5
- import BarChartIcon from '@mui/icons-material/BarChart' ;
6
5
import SortIcon from '@mui/icons-material/Sort' ;
7
- import SortByAlphaIcon from '@mui/icons-material/SortByAlpha' ;
8
- import WhatshotIcon from '@mui/icons-material/Whatshot' ;
9
6
import MuiBadge from '@mui/material/Badge' ;
10
7
import MuiIconButton from '@mui/material/IconButton' ;
11
8
import MuiTooltip from '@mui/material/Tooltip' ;
12
- import { useMemo , useState } from 'react' ;
9
+ import { useCallback , useEffect , useMemo , useState } from 'react' ;
13
10
14
11
import { text } from '../../../shared/text' ;
15
12
import { UseFilteredData } from '../../state/variables/use-filtered-data' ;
16
- import { ClassificationCIcon } from '../Icons/Icons' ;
17
13
import {
18
14
SelectMenu ,
19
15
SelectMenuOption ,
20
16
SelectMenuProps ,
21
17
} from '../SelectMenu/SelectMenu' ;
22
-
23
- interface SortOptionConfiguration {
24
- label : string ;
25
- icon : React . FC < { color ?: 'action' | 'disabled' } > ;
26
- }
27
-
28
- export type SortOption =
29
- | 'alphabetically'
30
- | 'criticality'
31
- | 'occurrence'
32
- | 'classification' ;
33
-
34
- type SortConfiguration = Record < SortOption , SortOptionConfiguration > ;
35
-
36
- export const SORT_CONFIGURATION : SortConfiguration = {
37
- alphabetically : {
38
- label : text . sortings . name ,
39
- icon : ( { color } : { color ?: 'action' | 'disabled' } ) => (
40
- < SortByAlphaIcon color = { color || 'action' } fontSize = { 'inherit' } />
41
- ) ,
42
- } ,
43
- criticality : {
44
- label : text . sortings . criticality ,
45
- icon : ( { color } : { color ?: 'action' | 'disabled' } ) => (
46
- < WhatshotIcon color = { color || 'warning' } fontSize = { 'inherit' } />
47
- ) ,
48
- } ,
49
- occurrence : {
50
- label : text . sortings . occurrence ,
51
- icon : ( { color } : { color ?: 'action' | 'disabled' } ) => (
52
- < BarChartIcon color = { color || 'info' } fontSize = { 'inherit' } />
53
- ) ,
54
- } ,
55
- classification : {
56
- label : text . sortings . classification ,
57
- icon : ( { color } : { color ?: 'action' | 'disabled' } ) => (
58
- < ClassificationCIcon color = { color || 'warning' } fontSize = { 'inherit' } />
59
- ) ,
60
- } ,
61
- } ;
18
+ import { SortOption , useSortConfiguration } from './useSortingOptions' ;
62
19
63
20
interface Props
64
21
extends Pick < SelectMenuProps , 'anchorArrow' | 'anchorPosition' > {
@@ -74,30 +31,47 @@ export const SortButton: React.FC<Props> = ({
74
31
const [ { sorting, attributions } , setFilteredAttributions ] =
75
32
useFilteredData ( ) ;
76
33
34
+ const sortConfiguration = useSortConfiguration ( ) ;
35
+
36
+ const setSorting = useCallback (
37
+ ( sortOption : SortOption ) => {
38
+ setFilteredAttributions ( ( prev ) => ( {
39
+ ...prev ,
40
+ sorting : sortOption ,
41
+ } ) ) ;
42
+ } ,
43
+ [ setFilteredAttributions ] ,
44
+ ) ;
45
+
46
+ useEffect ( ( ) => {
47
+ const availableKeys = Object . entries ( sortConfiguration )
48
+ . filter ( ( [ _ , entry ] ) => entry . active )
49
+ . map ( ( [ key , _ ] ) => key ) ;
50
+ if ( ! availableKeys . includes ( sorting ) ) {
51
+ setSorting ( 'alphabetically' ) ;
52
+ }
53
+ } , [ sortConfiguration , sorting , setSorting ] ) ;
54
+
77
55
const sortingOptions = useMemo (
78
56
( ) =>
79
- Object . entries ( SORT_CONFIGURATION ) . map < SelectMenuOption > (
80
- ( [ key , entry ] ) => {
57
+ Object . entries ( sortConfiguration )
58
+ . filter ( ( [ _ , entry ] ) => entry . active )
59
+ . map < SelectMenuOption > ( ( [ key , entry ] ) => {
81
60
const Icon = entry . icon ;
82
61
const sortOption : SortOption = key as SortOption ;
83
62
return {
84
63
id : sortOption ,
85
64
label : entry . label ,
86
65
selected : sortOption === sorting ,
87
66
icon : < Icon /> ,
88
- onAdd : ( ) =>
89
- setFilteredAttributions ( ( prev ) => ( {
90
- ...prev ,
91
- sorting : sortOption ,
92
- } ) ) ,
67
+ onAdd : ( ) => setSorting ( sortOption ) ,
93
68
} ;
94
- } ,
95
- ) ,
96
- [ setFilteredAttributions , sorting ] ,
69
+ } ) ,
70
+ [ sorting , sortConfiguration , setSorting ] ,
97
71
) ;
98
72
99
73
const disabled = ! attributions || ! Object . keys ( attributions ) . length ;
100
- const BadgeContent = SORT_CONFIGURATION [ sorting ] . icon ;
74
+ const BadgeContent = sortConfiguration [ sorting ] . icon ;
101
75
102
76
return (
103
77
< >
0 commit comments