-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
oatkiller
committed
Aug 13, 2020
1 parent
6e1511e
commit 7f1f453
Showing
4 changed files
with
81 additions
and
4 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
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/security_solution/public/resolver/view/use_query_string_keys.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,21 @@ | ||
/* | ||
* 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 { useSelector } from 'react-redux'; | ||
import * as selectors from '../store/selectors'; | ||
|
||
/** | ||
* Get the query string keys used by this Resolver instance. | ||
*/ | ||
export function useQueryStringKeys(): { idKey: string; eventKey: string } { | ||
const resolverComponentInstanceID = useSelector(selectors.resolverComponentInstanceID); | ||
const idKey: string = `resolver-${resolverComponentInstanceID}-id`; | ||
const eventKey: string = `resolver-${resolverComponentInstanceID}-event`; | ||
return { | ||
idKey, | ||
eventKey, | ||
}; | ||
} |
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
55 changes: 55 additions & 0 deletions
55
x-pack/plugins/security_solution/public/resolver/view/use_resolver_query_params_cleaner.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,55 @@ | ||
/* | ||
* 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 { useRef, useEffect } from 'react'; | ||
import { useLocation, useHistory } from 'react-router-dom'; | ||
|
||
import { useQueryStringKeys } from './use_query_string_keys'; | ||
/** | ||
* Cleanup any query string keys that were added by this Resolver instance. | ||
* This works by having a React effect that just has behavior in the 'cleanup' function. | ||
*/ | ||
export function useResolverQueryParamCleaner() { | ||
/** | ||
* Keep a reference to the current search value. This is used in the cleanup function. | ||
* This value of useLocation().search isn't used directly since that would change and | ||
* we only want the cleanup to run on unmount or when the resolverComponentInstanceID | ||
* changes. | ||
*/ | ||
const searchRef = useRef<string>(); | ||
searchRef.current = useLocation().search; | ||
|
||
const history = useHistory(); | ||
|
||
const { idKey, eventKey } = useQueryStringKeys(); | ||
|
||
useEffect(() => { | ||
/** | ||
* Keep track of the old query string keys so we can remove them. | ||
*/ | ||
const oldIdKey = idKey; | ||
const oldEventKey = eventKey; | ||
/** | ||
* When `idKey` or `eventKey` changes (such as when the `resolverComponentInstanceID` has changed) or when the component unmounts, remove any state from the query string. | ||
*/ | ||
return () => { | ||
/** | ||
* This effect must not be invalidated when `search` changes. | ||
* Use the current location.search via the `history` object. | ||
* `history` doesn't change so this is effectively like accessing `search` via a ref. | ||
*/ | ||
const urlSearchParams = new URLSearchParams(searchRef.current); | ||
|
||
/** | ||
* Remove old keys from the url | ||
*/ | ||
urlSearchParams.delete(oldIdKey); | ||
urlSearchParams.delete(oldEventKey); | ||
const relativeURL = { search: urlSearchParams.toString() }; | ||
history.replace(relativeURL); | ||
}; | ||
}, [idKey, eventKey, history]); | ||
} |