Skip to content

Commit a675bb0

Browse files
author
oatkiller
committed
cleanup the url handling code
1 parent 4752552 commit a675bb0

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

x-pack/plugins/security_solution/public/resolver/view/use_resolver_query_params.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66

77
import { useCallback, useMemo, useEffect } from 'react';
8-
// eslint-disable-next-line import/no-nodejs-modules
9-
import querystring from 'querystring';
108
import { useSelector } from 'react-redux';
119
import { useHistory, useLocation } from 'react-router-dom';
1210
import * as selectors from '../store/selectors';
@@ -23,56 +21,59 @@ export function useResolverQueryParams() {
2321
const idKey: string = `resolver-${resolverComponentInstanceID}-id`;
2422
const eventKey: string = `resolver-${resolverComponentInstanceID}-event`;
2523
const pushToQueryParams = useCallback(
26-
(newCrumbs: CrumbInfo) => {
27-
// Construct a new set of parameters from the current set (minus empty parameters)
28-
// by assigning the new set of parameters provided in `newCrumbs`
29-
const crumbsToPass = {
30-
...querystring.parse(urlSearch.slice(1)),
31-
[idKey]: newCrumbs.crumbId,
32-
[eventKey]: newCrumbs.crumbEvent,
33-
};
24+
(queryStringState: CrumbInfo) => {
25+
const urlSearchParams = new URLSearchParams(urlSearch);
3426

35-
// If either was passed in as empty, remove it from the record
36-
if (newCrumbs.crumbId === '') {
37-
delete crumbsToPass[idKey];
27+
urlSearchParams.set(idKey, queryStringState.crumbId);
28+
urlSearchParams.set(eventKey, queryStringState.crumbEvent);
29+
30+
// If either was passed in as empty, remove it
31+
if (queryStringState.crumbId === '') {
32+
urlSearchParams.delete(idKey);
3833
}
39-
if (newCrumbs.crumbEvent === '') {
40-
delete crumbsToPass[eventKey];
34+
if (queryStringState.crumbEvent === '') {
35+
urlSearchParams.delete(eventKey);
4136
}
4237

43-
const relativeURL = { search: querystring.stringify(crumbsToPass) };
38+
const relativeURL = { search: urlSearchParams.toString() };
4439
// We probably don't want to nuke the user's history with a huge
4540
// trail of these, thus `.replace` instead of `.push`
4641
return history.replace(relativeURL);
4742
},
4843
[history, urlSearch, idKey, eventKey]
4944
);
5045
const queryParams: CrumbInfo = useMemo(() => {
51-
const parsed = querystring.parse(urlSearch.slice(1));
52-
const crumbEvent = parsed[eventKey];
53-
const crumbId = parsed[idKey];
54-
function valueForParam(param: string | string[]): string {
55-
if (Array.isArray(param)) {
56-
return param[0] || '';
57-
}
58-
return param || '';
59-
}
46+
const urlSearchParams = new URLSearchParams(urlSearch);
6047
return {
61-
crumbEvent: valueForParam(crumbEvent),
62-
crumbId: valueForParam(crumbId),
48+
// Use `''` for backwards compatibility with deprecated code.
49+
crumbEvent: urlSearchParams.get(eventKey) ?? '',
50+
crumbId: urlSearchParams.get(idKey) ?? '',
6351
};
6452
}, [urlSearch, idKey, eventKey]);
6553

6654
useEffect(() => {
55+
/**
56+
* Keep track of the old query string keys so we can remove them.
57+
*/
6758
const oldIdKey = idKey;
6859
const oldEventKey = eventKey;
60+
/**
61+
* When `idKey` or `eventKey` changes (such as when the `resolverComponentInstanceID` has changed) or when the component unmounts, remove any state from the query string.
62+
*/
6963
return () => {
70-
const crumbsToPass = {
71-
...querystring.parse(history.location.search.slice(1)),
72-
};
73-
delete crumbsToPass[oldIdKey];
74-
delete crumbsToPass[oldEventKey];
75-
const relativeURL = { search: querystring.stringify(crumbsToPass) };
64+
/**
65+
* This effect must not be invalidated when `search` changes.
66+
* Use the current location.search via the `history` object.
67+
* `history` doesn't change so this is effectively like accessing `search` via a ref.
68+
*/
69+
const urlSearchParams = new URLSearchParams(history.location.search);
70+
71+
/**
72+
* Remove old keys from the url
73+
*/
74+
urlSearchParams.delete(oldIdKey);
75+
urlSearchParams.delete(oldEventKey);
76+
const relativeURL = { search: urlSearchParams.toString() };
7677
history.replace(relativeURL);
7778
};
7879
}, [idKey, eventKey, history]);

0 commit comments

Comments
 (0)