-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhelpers.ts
266 lines (249 loc) · 9.16 KB
/
helpers.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { Location, LocationDescriptor } from "history";
import {
AnalysisIssue,
AnalysisIssueReport,
AnalysisRuleReport,
Archetype,
} from "@app/api/models";
import {
FilterCategory,
FilterType,
FilterValue,
} from "@app/components/FilterToolbar";
import {
deserializeFilterUrlParams,
serializeFilterUrlParams,
} from "@app/hooks/table-controls";
import { trimAndStringifyUrlParams } from "@app/hooks/useUrlParams";
import { Paths } from "@app/Paths";
import { TablePersistenceKeyPrefix } from "@app/Constants";
import { IssueFilterGroups } from "./issues";
import { useFetchBusinessServices } from "@app/queries/businessservices";
import { useFetchTagsWithTagItems } from "@app/queries/tags";
import { useTranslation } from "react-i18next";
import { useFetchArchetypes } from "@app/queries/archetypes";
import { useFetchApplications } from "@app/queries/applications";
import { universalComparator } from "@app/utils/utils";
// Certain filters are shared between the Issues page and the Affected Applications Page.
// We carry these filter values between the two pages when determining the URLs to navigate between them.
// It is also important to restore any unrelated params when returning to the Issues page.
const filterKeysToCarry = [
"application.name",
"businessService.name",
"tag.id",
] as const;
export type IssuesFilterValuesToCarry = Partial<Record<string, FilterValue>>;
export const useSharedAffectedApplicationFilterCategories = <
TItem,
>(): FilterCategory<TItem, string>[] => {
const { t } = useTranslation();
const { businessServices } = useFetchBusinessServices();
const { tagCategories, tags, tagItems } = useFetchTagsWithTagItems();
const { archetypes } = useFetchArchetypes();
const { data: applications } = useFetchApplications();
return [
{
categoryKey: "application.name",
title: t("terms.applicationName"),
filterGroup: IssueFilterGroups.ApplicationInventory,
type: FilterType.multiselect,
placeholderText:
t("actions.filterBy", {
what: t("terms.applicationName").toLowerCase(),
}) + "...",
selectOptions: applications
.map(({ name }) => name)
.sort(universalComparator)
.map((name) => ({
key: name,
value: name,
})),
getServerFilterValue: (selectedOptions) =>
selectedOptions?.filter(Boolean) ?? [],
},
{
categoryKey: "application.id",
title: t("terms.archetypes"),
filterGroup: IssueFilterGroups.ApplicationInventory,
type: FilterType.multiselect,
placeholderText:
t("actions.filterBy", {
what: t("terms.archetype").toLowerCase(),
}) + "...",
selectOptions: archetypes.map(({ name }) => ({
key: name,
value: name,
})),
getServerFilterValue: (selectedOptions) => {
const findArchetypeByName = (name: string) => {
return archetypes.find((item) => item.name === name);
};
const getApplicationIds = (archetype: Archetype) => {
return archetype.applications?.map((app) => String(app.id));
};
if (!selectedOptions) return ["-1"];
const archetypeIds = selectedOptions
.map((option) => findArchetypeByName(option))
.filter(Boolean)
.flatMap((archetype) => getApplicationIds(archetype))
.filter(Boolean);
return archetypeIds.length === 0 ? ["-1"] : archetypeIds;
},
},
{
categoryKey: "businessService.name",
title: t("terms.businessService"),
filterGroup: IssueFilterGroups.ApplicationInventory,
placeholderText:
t("actions.filterBy", {
what: t("terms.businessService").toLowerCase(),
}) + "...",
type: FilterType.multiselect,
selectOptions: businessServices
.map((businessService) => businessService.name)
.map((name) => ({ key: name, value: name })),
},
{
categoryKey: "tag.id",
title: t("terms.tags"),
filterGroup: IssueFilterGroups.ApplicationInventory,
type: FilterType.multiselect,
placeholderText:
t("actions.filterBy", {
what: t("terms.tagName").toLowerCase(),
}) + "...",
selectOptions: tagItems.map(({ name, tagName, categoryName }) => ({
value: name,
label: name,
chipLabel: tagName,
groupLabel: categoryName,
})),
/**
* Convert the selected `selectOptions` to an array of tag ids the server side
* filtering will understand.
*/
getServerFilterValue: (selectedOptions) =>
selectedOptions
?.map((option) => tagItems.find((item) => option === item.name))
.filter(Boolean)
.map(({ id }) => String(id)) ?? [],
},
];
};
const FROM_ISSUES_PARAMS_KEY = "~fromIssuesParams"; // ~ prefix sorts it at the end of the URL for readability
// URL for Affected Apps page that includes carried filters and a snapshot of original URL params from the Issues page
export const getAffectedAppsUrl = ({
ruleReport,
fromFilterValues,
fromLocation,
}: {
ruleReport: AnalysisRuleReport;
fromFilterValues: IssuesFilterValuesToCarry;
fromLocation: Location;
}) => {
// The raw location.search string (already encoded) from the issues page is used as the fromIssuesParams param
const fromIssuesParams = fromLocation.search;
const toFilterValues: IssuesFilterValuesToCarry = {};
filterKeysToCarry.forEach((key) => {
if (fromFilterValues[key]) toFilterValues[key] = fromFilterValues[key];
});
const baseUrl = Paths.issuesAllAffectedApplications
.replace("/:ruleset/", `/${encodeURIComponent(ruleReport.ruleset)}/`)
.replace("/:rule/", `/${encodeURIComponent(ruleReport.rule)}/`);
const prefix = (key: string) =>
`${TablePersistenceKeyPrefix.issuesAffectedApps}:${key}`;
return `${baseUrl}?${trimAndStringifyUrlParams({
newPrefixedSerializedParams: {
[prefix("filters")]: serializeFilterUrlParams(toFilterValues).filters,
[FROM_ISSUES_PARAMS_KEY]: fromIssuesParams,
issueTitle: getIssueTitle(ruleReport),
},
})}`;
};
// URL for Issues page that restores original URL params and overrides them with any changes to the carried filters.
export const getBackToAllIssuesUrl = ({
fromFilterValues,
fromLocation,
}: {
fromFilterValues: IssuesFilterValuesToCarry;
fromLocation: Location;
}) => {
// Pull the fromIssuesParams param out of the current location's URLSearchParams
const fromIssuesParams =
new URLSearchParams(fromLocation.search).get(FROM_ISSUES_PARAMS_KEY) || "";
// Pull the params themselves out of fromIssuesParams
const prefixedParamsToRestore = Object.fromEntries(
new URLSearchParams(fromIssuesParams)
);
// Pull the filters param out of that
const prefix = (key: string) => `${TablePersistenceKeyPrefix.issues}:${key}`;
const filterValuesToRestore = deserializeFilterUrlParams({
filters: prefixedParamsToRestore[prefix("filters")],
});
// For each of the filters we care about, override the original value with the one from the affected apps page.
// This will carry over changes including the filter having been cleared.
filterKeysToCarry.forEach((key) => {
filterValuesToRestore[key] = fromFilterValues[key] || null;
});
// Put it all back together
return `${Paths.issuesAllTab}?${trimAndStringifyUrlParams({
newPrefixedSerializedParams: {
...prefixedParamsToRestore,
[prefix("filters")]: serializeFilterUrlParams(filterValuesToRestore)
.filters,
},
})}`;
};
export const getDependenciesUrlFilteredByAppName = (appName: string) => {
const baseUrl = Paths.dependencies;
const filterParams = serializeFilterUrlParams({
"application.name": [appName],
});
const urlParams = trimAndStringifyUrlParams({
newPrefixedSerializedParams: {
filters: filterParams.filters,
},
});
return `${baseUrl}?${urlParams}`;
};
// When selecting an application, we want to preserve any issue filters that might be present.
export const getIssuesSingleAppSelectedLocation = (
applicationId: number,
fromLocation?: Location
): LocationDescriptor => {
const existingFiltersParam =
fromLocation &&
new URLSearchParams(fromLocation.search).get(
`${TablePersistenceKeyPrefix.issues}:filters`
);
return {
pathname: Paths.issuesSingleAppSelected.replace(
":applicationId",
String(applicationId)
),
search: existingFiltersParam
? new URLSearchParams({ filters: existingFiltersParam }).toString()
: undefined,
};
};
export const parseReportLabels = (
ruleReport: AnalysisRuleReport | AnalysisIssueReport
) => {
const sources: string[] = [];
const targets: string[] = [];
const otherLabels: string[] = [];
ruleReport.labels.forEach((label) => {
if (label.startsWith("konveyor.io/source=")) {
sources.push(label.split("konveyor.io/source=")[1]);
} else if (label.startsWith("konveyor.io/target=")) {
targets.push(label.split("konveyor.io/target=")[1]);
} else {
otherLabels.push(label);
}
});
return { sources, targets, otherLabels };
};
export const getIssueTitle = (
issueReport: AnalysisRuleReport | AnalysisIssue | AnalysisIssueReport
) =>
issueReport?.description || issueReport?.name?.split("\n")[0] || "*Unnamed*";