forked from guacsec/trustify-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvisory-context.tsx
More file actions
135 lines (124 loc) · 3.42 KB
/
advisory-context.tsx
File metadata and controls
135 lines (124 loc) · 3.42 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
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
import React from "react";
import type { AxiosError } from "axios";
import {
FILTER_TEXT_CATEGORY_KEY,
TablePersistenceKeyPrefixes,
} from "@app/Constants";
import type { AdvisorySummary } from "@app/client";
import { FilterType } from "@app/components/FilterToolbar";
import {
type ITableControls,
getHubRequestParams,
useTableControlProps,
useTableControlState,
} from "@app/hooks/table-controls";
import { useSelectionState } from "@app/hooks/useSelectionState";
import { useFetchAdvisories } from "@app/queries/advisories";
interface IAdvisorySearchContext {
tableControls: ITableControls<
AdvisorySummary,
| "identifier"
| "title"
| "severity"
| "type"
| "modified"
| "vulnerabilities",
"identifier" | "severity" | "modified",
"" | "average_severity" | "modified",
string
>;
totalItemCount: number;
isFetching: boolean;
fetchError: AxiosError | null;
}
const contextDefaultValue = {} as IAdvisorySearchContext;
export const AdvisorySearchContext =
React.createContext<IAdvisorySearchContext>(contextDefaultValue);
interface IAdvisoryProvider {
children: React.ReactNode;
}
export const AdvisorySearchProvider: React.FunctionComponent<
IAdvisoryProvider
> = ({ children }) => {
const tableControlState = useTableControlState({
tableName: "advisory",
persistenceKeyPrefix: TablePersistenceKeyPrefixes.advisories,
persistTo: "urlParams",
columnNames: {
identifier: "ID",
title: "Title",
severity: "Aggregated Severity",
type: "Type",
modified: "Revision",
vulnerabilities: "Vulnerabilities",
},
isPaginationEnabled: true,
isSortEnabled: true,
sortableColumns: ["identifier", "severity", "modified"],
initialSort: {
columnKey: "modified",
direction: "desc",
},
isFilterEnabled: true,
filterCategories: [
{
categoryKey: FILTER_TEXT_CATEGORY_KEY,
title: "Filter text",
placeholderText: "Search",
type: FilterType.search,
},
{
categoryKey: "average_severity",
title: "Severity",
placeholderText: "Severity",
type: FilterType.multiselect,
selectOptions: [
{ value: "null", label: "Unknown" },
{ value: "none", label: "None" },
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High" },
{ value: "critical", label: "Critical" },
],
},
{
categoryKey: "modified",
title: "Revision",
type: FilterType.dateRange,
},
],
isExpansionEnabled: false,
});
const {
result: { data: advisories, total: totalItemCount },
isFetching,
fetchError,
} = useFetchAdvisories(
getHubRequestParams({
...tableControlState,
hubSortFieldKeys: {
identifier: "identifier",
severity: "average_score",
modified: "modified",
},
}),
);
const tableControls = useTableControlProps({
...tableControlState,
idProperty: "identifier",
currentPageItems: advisories,
totalItemCount,
isLoading: isFetching,
selectionState: useSelectionState({
items: advisories,
isEqual: (a, b) => a.identifier === b.identifier,
}),
});
return (
<AdvisorySearchContext.Provider
value={{ totalItemCount, isFetching, fetchError, tableControls }}
>
{children}
</AdvisorySearchContext.Provider>
);
};