-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Add hook for reading/writing resolver query params (
#70809) * Move resolver query param logic into shared hook * Store document location in state * Rename documentLocation to resolverComponentInstanceID * Use undefined for initial resolverComponentID value * Update type for initial state of component id
- Loading branch information
1 parent
169397c
commit 0f143a3
Showing
14 changed files
with
131 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
x-pack/plugins/security_solution/public/resolver/view/use_resolver_query_params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useCallback, useMemo } from 'react'; | ||
// eslint-disable-next-line import/no-nodejs-modules | ||
import querystring from 'querystring'; | ||
import { useSelector } from 'react-redux'; | ||
import { useHistory, useLocation } from 'react-router-dom'; | ||
import * as selectors from '../store/selectors'; | ||
import { CrumbInfo } from './panels/panel_content_utilities'; | ||
|
||
export function useResolverQueryParams() { | ||
/** | ||
* This updates the breadcrumb nav and the panel view. It's supplied to each | ||
* panel content view to allow them to dispatch transitions to each other. | ||
*/ | ||
const history = useHistory(); | ||
const urlSearch = useLocation().search; | ||
const resolverComponentInstanceID = useSelector(selectors.resolverComponentInstanceID); | ||
const uniqueCrumbIdKey: string = `${resolverComponentInstanceID}CrumbId`; | ||
const uniqueCrumbEventKey: string = `${resolverComponentInstanceID}CrumbEvent`; | ||
const pushToQueryParams = useCallback( | ||
(newCrumbs: CrumbInfo) => { | ||
// Construct a new set of params from the current set (minus empty params) | ||
// by assigning the new set of params provided in `newCrumbs` | ||
const crumbsToPass = { | ||
...querystring.parse(urlSearch.slice(1)), | ||
[uniqueCrumbIdKey]: newCrumbs.crumbId, | ||
[uniqueCrumbEventKey]: newCrumbs.crumbEvent, | ||
}; | ||
|
||
// If either was passed in as empty, remove it from the record | ||
if (newCrumbs.crumbId === '') { | ||
delete crumbsToPass[uniqueCrumbIdKey]; | ||
} | ||
if (newCrumbs.crumbEvent === '') { | ||
delete crumbsToPass[uniqueCrumbEventKey]; | ||
} | ||
|
||
const relativeURL = { search: querystring.stringify(crumbsToPass) }; | ||
// We probably don't want to nuke the user's history with a huge | ||
// trail of these, thus `.replace` instead of `.push` | ||
return history.replace(relativeURL); | ||
}, | ||
[history, urlSearch, uniqueCrumbIdKey, uniqueCrumbEventKey] | ||
); | ||
const queryParams: CrumbInfo = useMemo(() => { | ||
const parsed = querystring.parse(urlSearch.slice(1)); | ||
const crumbEvent = parsed[uniqueCrumbEventKey]; | ||
const crumbId = parsed[uniqueCrumbIdKey]; | ||
return { | ||
crumbEvent: Array.isArray(crumbEvent) ? crumbEvent[0] : crumbEvent, | ||
crumbId: Array.isArray(crumbId) ? crumbId[0] : crumbId, | ||
}; | ||
}, [urlSearch, uniqueCrumbIdKey, uniqueCrumbEventKey]); | ||
|
||
return { | ||
pushToQueryParams, | ||
queryParams, | ||
}; | ||
} |
Oops, something went wrong.