-
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.
Move resolver query param logic into shared hook
- Loading branch information
1 parent
4b99029
commit 1206284
Showing
7 changed files
with
100 additions
and
80 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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
/* | ||
* 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 { useHistory, useLocation } from 'react-router-dom'; | ||
import { CrumbInfo } from './panels/panel_content_utilities'; | ||
|
||
export function useResolverQueryParams(documentLocation: string) { | ||
/** | ||
* 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 uniqueCrumbIdKey = `${documentLocation}CrumbId`; | ||
const uniqueCrumbEventKey = `${documentLocation}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 (crumbsToPass.uniqueCrumbIdKey === '') { | ||
delete crumbsToPass.uniqueCrumbIdKey; | ||
} | ||
if (crumbsToPass.uniqueCrumbEventKey === '') { | ||
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 newParams = { | ||
[uniqueCrumbIdKey]: '', | ||
[uniqueCrumbEventKey]: '', | ||
...querystring.parse(urlSearch.slice(1)), | ||
}; | ||
newParams.crumbId = newParams[uniqueCrumbIdKey]; | ||
newParams.crumbEvent = newParams[uniqueCrumbEventKey]; | ||
return newParams; | ||
}, [urlSearch, uniqueCrumbIdKey, uniqueCrumbEventKey]); | ||
|
||
return { | ||
pushToQueryParams, | ||
queryParams, | ||
}; | ||
} |
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