Skip to content

Commit f37aff8

Browse files
committed
Use normalizeRisk() to enforce risk value stability
Signed-off-by: Scott J Dickerson <[email protected]>
1 parent 84fbbf7 commit f37aff8

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

client/src/app/components/RiskLabel.tsx

+3-30
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,18 @@ import { Label } from "@patternfly/react-core";
55

66
import { RISK_LIST } from "@app/Constants";
77
import { Risk } from "@app/api/models";
8+
import { normalizeRisk } from "@app/utils/type-utils";
89

910
export interface IRiskLabelProps {
1011
risk?: Risk | string;
1112
}
1213

13-
function normalizeToRisk(risk?: Risk | string): Risk | undefined {
14-
let normal: Risk | undefined = undefined;
15-
16-
switch (risk) {
17-
case "green":
18-
normal = "green";
19-
break;
20-
21-
case "yellow":
22-
normal = "yellow";
23-
break;
24-
25-
case "red":
26-
normal = "red";
27-
break;
28-
29-
case "unassessed":
30-
normal = "unassessed";
31-
break;
32-
33-
case "unknown":
34-
normal = "unknown";
35-
break;
36-
}
37-
38-
return normal;
39-
}
40-
4114
export const RiskLabel: React.FC<IRiskLabelProps> = ({
42-
risk = "unknown",
15+
risk,
4316
}: IRiskLabelProps) => {
4417
const { t } = useTranslation();
4518

46-
const asRisk = normalizeToRisk(risk);
19+
const asRisk = normalizeRisk(risk);
4720
const data = !asRisk ? undefined : RISK_LIST[asRisk];
4821

4922
return (

client/src/app/pages/applications/applications-table/applications-table.tsx

+13-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
Modal,
1818
Tooltip,
1919
} from "@patternfly/react-core";
20-
import { PencilAltIcon, TagIcon } from "@patternfly/react-icons";
20+
import {
21+
PencilAltIcon,
22+
TagIcon,
23+
WarningTriangleIcon,
24+
} from "@patternfly/react-icons";
25+
2126
import {
2227
Table,
2328
Thead,
@@ -59,8 +64,8 @@ import {
5964
tasksReadScopes,
6065
tasksWriteScopes,
6166
} from "@app/rbac";
67+
import { normalizeRisk } from "@app/utils/type-utils";
6268
import { checkAccess } from "@app/utils/rbac-utils";
63-
import WarningTriangleIcon from "@patternfly/react-icons/dist/esm/icons/warning-triangle-icon";
6469

6570
// Hooks
6671
import { useLocalTableControls } from "@app/hooks/table-controls";
@@ -486,13 +491,13 @@ export const ApplicationsTable: React.FC = () => {
486491
what: t("terms.risk").toLowerCase(),
487492
}) + "...",
488493
selectOptions: [
489-
{ value: "green", label: "Low" },
490-
{ value: "yellow", label: "Medium" },
491-
{ value: "red", label: "High" },
492-
{ value: "unknown", label: "Unknown" },
493-
{ value: "unassessed", label: "Unassessed" },
494+
{ value: "green", label: t("risks.low") },
495+
{ value: "yellow", label: t("risks.medium") },
496+
{ value: "red", label: t("risks.high") },
497+
{ value: "unknown", label: t("risks.unknown") },
498+
{ value: "unassessed", label: t("risks.unassessed") },
494499
],
495-
getItemValue: (item) => item.risk ?? "",
500+
getItemValue: (item) => normalizeRisk(item.risk) ?? "",
496501
},
497502
],
498503
initialItemsPerPage: 10,

client/src/app/utils/type-utils.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Risk } from "@app/api/models";
2+
13
export type KeyWithValueType<T, V> = {
24
[Key in keyof T]-?: T[Key] extends V ? Key : never;
35
}[keyof T];
@@ -10,3 +12,38 @@ export type DisallowCharacters<
1012
export type DiscriminatedArgs<TBoolDiscriminatorKey extends string, TArgs> =
1113
| ({ [key in TBoolDiscriminatorKey]: true } & TArgs)
1214
| { [key in TBoolDiscriminatorKey]?: false };
15+
16+
/**
17+
* Normalize an optional `Risk` or `string` input and return either the matching
18+
* `Risk` or the `defaultValue` (which defaults to `undefined`).
19+
*/
20+
export function normalizeRisk(
21+
risk?: Risk | string,
22+
defaultValue?: Risk
23+
): Risk | undefined {
24+
let normal: Risk | undefined = defaultValue;
25+
26+
switch (risk) {
27+
case "green":
28+
normal = "green";
29+
break;
30+
31+
case "yellow":
32+
normal = "yellow";
33+
break;
34+
35+
case "red":
36+
normal = "red";
37+
break;
38+
39+
case "unassessed":
40+
normal = "unassessed";
41+
break;
42+
43+
case "unknown":
44+
normal = "unknown";
45+
break;
46+
}
47+
48+
return normal;
49+
}

0 commit comments

Comments
 (0)