-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[security solution][Endpoint] Refactor host isolation exceptions list page to now use <ArtifactListPage/>
component
#129099
Changes from 32 commits
e7cc3b3
937877c
7ee5470
956b5f0
48398ba
0ed0abb
12ac093
24c1f00
cb56284
b696e92
36bcdff
ef65a42
84e0aff
7840222
5db499a
71854b0
7453c4a
c924f27
5812b67
53a325d
722b8d7
b688d50
b4bf73b
3ac4b84
bb375e1
2ae32df
e7518f5
ec07ea2
acc0983
3ca47ed
7529671
344b2d0
6ce6b00
f3d33f3
3ff6457
1163df0
739f5ba
176c48d
1f6ca0a
53ae8de
a2d1999
5dba32d
4d853ea
eb307c2
2d5864c
b16c443
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
// FIXME: Remove references to `querystring` | ||
// eslint-disable-next-line import/no-nodejs-modules | ||
import querystring from 'querystring'; | ||
import { ArtifactListPageUrlParams } from '../../components/artifact_list_page'; | ||
import { | ||
isDefaultOrMissing, | ||
extractFirstParamValue, | ||
extractPageSizeNumber, | ||
extractPageNumber, | ||
} from './utils'; | ||
import { MANAGEMENT_DEFAULT_PAGE_SIZE } from '../constants'; | ||
import { appendSearch } from '../../../common/components/link_to/helpers'; | ||
|
||
const SHOW_PARAM_ALLOWED_VALUES: ReadonlyArray<Required<ArtifactListPageUrlParams>['show']> = [ | ||
'edit', | ||
'create', | ||
]; | ||
|
||
/** | ||
* Normalizes the URL search params by dropping any that are either undefined or whose value is | ||
* equal to the default value. | ||
* @param urlSearchParams | ||
*/ | ||
const normalizeArtifactListPageUrlSearchParams = ( | ||
urlSearchParams: Partial<ArtifactListPageUrlParams> = {} | ||
): Partial<ArtifactListPageUrlParams> => { | ||
return { | ||
...(!isDefaultOrMissing(urlSearchParams.page, 1) ? { page: urlSearchParams.page } : {}), | ||
...(!isDefaultOrMissing(urlSearchParams.pageSize, MANAGEMENT_DEFAULT_PAGE_SIZE) | ||
? { pageSize: urlSearchParams.pageSize } | ||
: {}), | ||
...(!isDefaultOrMissing(urlSearchParams.show, undefined) ? { show: urlSearchParams.show } : {}), | ||
...(!isDefaultOrMissing(urlSearchParams.itemId, undefined) | ||
? { itemId: urlSearchParams.itemId } | ||
: {}), | ||
...(!isDefaultOrMissing(urlSearchParams.filter, '') ? { filter: urlSearchParams.filter } : ''), | ||
...(!isDefaultOrMissing(urlSearchParams.includedPolicies, '') | ||
? { includedPolicies: urlSearchParams.includedPolicies } | ||
: ''), | ||
}; | ||
}; | ||
|
||
export const extractArtifactListPageUrlSearchParams = ( | ||
query: querystring.ParsedUrlQuery | ||
): ArtifactListPageUrlParams => { | ||
const showParamValue = extractFirstParamValue(query, 'show') as ArtifactListPageUrlParams['show']; | ||
|
||
return { | ||
page: extractPageNumber(query), | ||
pageSize: extractPageSizeNumber(query), | ||
includedPolicies: extractFirstParamValue(query, 'includedPolicies'), | ||
show: | ||
showParamValue && SHOW_PARAM_ALLOWED_VALUES.includes(showParamValue) | ||
? showParamValue | ||
: undefined, | ||
itemId: extractFirstParamValue(query, 'itemId'), | ||
}; | ||
}; | ||
|
||
export const getArtifactListPageUrlPath = ( | ||
/** The path to the desired page that is using the `<ArtifactListPage>` component */ | ||
path: string, | ||
/** An optional set of url search params. These will be normalized prior to being appended to the url path */ | ||
searchParams: Partial<ArtifactListPageUrlParams> = {} | ||
): string => { | ||
return `${path}${appendSearch( | ||
querystring.stringify(normalizeArtifactListPageUrlSearchParams(searchParams)) | ||
)}`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './utils'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: This directory ( |
||
export { | ||
getArtifactListPageUrlPath, | ||
extractArtifactListPageUrlSearchParams, | ||
} from './artifact_list_page_routing'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these were copied over from the existing |
||
|
||
// eslint-disable-next-line import/no-nodejs-modules | ||
import querystring from 'querystring'; | ||
import { MANAGEMENT_DEFAULT_PAGE_SIZE, MANAGEMENT_PAGE_SIZE_OPTIONS } from '../constants'; | ||
|
||
/** | ||
* Checks if a given value is either undefined or equal to the default value provided on input | ||
* @param value | ||
* @param defaultValue | ||
*/ | ||
export const isDefaultOrMissing = <T>(value: T | undefined, defaultValue: T) => { | ||
return value === undefined || value === defaultValue; | ||
}; | ||
|
||
/** | ||
* Given an object with url params, and a given key, return back only the first param value (case multiples were defined) | ||
* @param query | ||
* @param key | ||
*/ | ||
export const extractFirstParamValue = ( | ||
query: querystring.ParsedUrlQuery, | ||
key: string | ||
): string | undefined => { | ||
const value = query[key]; | ||
|
||
return Array.isArray(value) ? value[value.length - 1] : value; | ||
}; | ||
|
||
/** | ||
* Extracts the page number from a url query object `page` param and validates it. | ||
* @param query | ||
*/ | ||
export const extractPageNumber = (query: querystring.ParsedUrlQuery): number => { | ||
const pageIndex = Number(extractFirstParamValue(query, 'page')); | ||
|
||
return !Number.isFinite(pageIndex) || pageIndex < 1 ? 1 : pageIndex; | ||
}; | ||
|
||
/** | ||
* Extracts the page size from a url query object `pageSize` param and validates it | ||
* @param query | ||
*/ | ||
export const extractPageSizeNumber = (query: querystring.ParsedUrlQuery): number => { | ||
const pageSize = Number(extractFirstParamValue(query, 'pageSize')); | ||
|
||
return MANAGEMENT_PAGE_SIZE_OPTIONS.includes(pageSize) ? pageSize : MANAGEMENT_DEFAULT_PAGE_SIZE; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice ⭐ Looks like I had the same idea in my PR here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll copy these changes into my own PR or wait for this to merge. After updating to use
ArtifactListPageUrlParams
for types some of these helper methods become obsolete.