forked from konflux-ci/konflux-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConformaTable.tsx
More file actions
101 lines (92 loc) · 3.17 KB
/
ConformaTable.tsx
File metadata and controls
101 lines (92 loc) · 3.17 KB
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
import * as React from 'react';
import { SortByDirection } from '@patternfly/react-table';
import { Table } from '~/shared';
import { CONFORMA_RESULT_STATUS, UIConformaData } from '~/types/conforma';
import { ConformaExpandedRowContent } from './ConformaExpandedRowContent';
import getConformaHeader from './ConformaHeader';
import { WrappedConformaRow } from './ConformaRow';
import './ConformaTable.scss';
type ConformaTableProps = {
crResult: UIConformaData[];
};
const STATUS_SORT_ORDER = [
CONFORMA_RESULT_STATUS.violations,
CONFORMA_RESULT_STATUS.warnings,
CONFORMA_RESULT_STATUS.successes,
];
const COLUMN_ORDER = [undefined, 'title', 'status', 'msg', 'component'];
export const getSortColumnFuntion = (key: string, activeSortDirection: string) => {
switch (key) {
case 'status':
return (a: UIConformaData, b: UIConformaData) => {
const aValue = STATUS_SORT_ORDER.indexOf(a[key]);
const bValue = STATUS_SORT_ORDER.indexOf(b[key]);
if (aValue < bValue) {
return activeSortDirection === 'asc' ? -1 : 1;
} else if (aValue > bValue) {
return activeSortDirection === 'asc' ? 1 : -1;
}
return 0;
};
default:
return (a: UIConformaData, b: UIConformaData) => {
const aValue = a[key];
const bValue = b[key];
if (typeof aValue === 'string' && typeof bValue === 'string') {
// String sort
if (activeSortDirection === 'asc') {
return aValue.localeCompare(bValue);
}
return bValue.localeCompare(aValue);
}
};
}
};
export const ConformaTable: React.FC<React.PropsWithChildren<ConformaTableProps>> = ({
crResult,
}) => {
const [activeSortIndex, setActiveSortIndex] = React.useState<number | null>(2);
const [activeSortDirection, setActiveSortDirection] = React.useState<
SortByDirection.asc | SortByDirection.desc | null
>(SortByDirection.asc);
const ConformaHeader = React.useMemo(
() =>
getConformaHeader(activeSortIndex, activeSortDirection, (_, index, direction) => {
setActiveSortIndex(index);
setActiveSortDirection(direction);
}),
[activeSortDirection, activeSortIndex],
);
const sortedCRResult = React.useMemo(() => {
return crResult
? crResult.sort(getSortColumnFuntion(COLUMN_ORDER[activeSortIndex], activeSortDirection))
: undefined;
}, [activeSortDirection, activeSortIndex, crResult]);
return sortedCRResult ? (
<div className="pf-v5-c-table pf-m-compact pf-m-grid-md">
<Table
virtualize
data={sortedCRResult}
aria-label="conforma-table"
Header={ConformaHeader}
ExpandedContent={(props) => {
const obj = props.obj as UIConformaData;
return <ConformaExpandedRowContent {...props} obj={obj} />;
}}
Row={(props) => {
const obj = props.obj as UIConformaData;
return (
<WrappedConformaRow
{...props}
obj={obj}
customData={{ sortedConformaResult: sortedCRResult }}
/>
);
}}
loaded
customData={{ sortedCRResult }}
expand
/>
</div>
) : null;
};