Skip to content

Commit 68c6d4a

Browse files
authored
chore: rename useQuery hook to useURLSearchParams (#1111)
1 parent c840f7b commit 68c6d4a

File tree

6 files changed

+20
-26
lines changed

6 files changed

+20
-26
lines changed

‎src/components/CheckForm/useCheckType.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { useLocation, useParams } from 'react-router-dom-v5-compat';
1+
import { useParams } from 'react-router-dom-v5-compat';
22

33
import { Check, CheckFormPageParams, CheckType } from 'types';
44
import { getCheckType } from 'utils';
55
import { CHECK_TYPE_OPTIONS, useCheckTypeOptions } from 'hooks/useCheckTypeOptions';
6+
import { useURLSearchParams } from 'hooks/useURLSearchParams';
67

78
export function useFormCheckType(existingCheck?: Check) {
89
const { checkTypeGroup } = useParams<CheckFormPageParams>();
910
const options = useCheckTypeOptions();
1011
const fallback = options.filter((option) => option.group === checkTypeGroup);
11-
const { search } = useLocation();
12+
const urlSearchParams = useURLSearchParams();
1213

1314
if (existingCheck) {
1415
return getCheckType(existingCheck.settings);
1516
}
1617

17-
const searchParams = new URLSearchParams(search);
18+
const checkType = urlSearchParams.get('checkType') as CheckType;
1819

19-
return (searchParams.get('checkType') as CheckType) || fallback[0]?.value || options[0].value;
20+
return checkType || fallback[0]?.value || options[0].value;
2021
}
2122

2223
export function useFormCheckTypeGroup(check?: Check) {

‎src/components/SceneRedirecter.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { LoadingPlaceholder } from '@grafana/ui';
55
import { ROUTES } from 'routing/types';
66
import { generateRoutePath } from 'routing/utils';
77
import { useChecks } from 'data/useChecks';
8-
import { useQuery } from 'hooks/useQuery';
8+
import { useURLSearchParams } from 'hooks/useURLSearchParams';
99

1010
export function SceneRedirecter() {
11-
const queryParams = useQuery();
12-
const job = queryParams.get('var-job');
13-
const instance = queryParams.get('var-instance');
11+
const urlSearchParams = useURLSearchParams();
12+
const job = urlSearchParams.get('var-job');
13+
const instance = urlSearchParams.get('var-instance');
1414
const { data, isLoading } = useChecks();
1515

1616
if (isLoading) {

‎src/hooks/useQueryParametersState.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback, useMemo } from 'react';
22
import { useLocation, useNavigate } from 'react-router-dom-v5-compat';
33

4-
import { useSearchParams } from './useSearchParams';
4+
import { useURLSearchParams } from 'hooks/useURLSearchParams';
55

66
enum HistoryStrategy {
77
Push = 'push',
@@ -25,9 +25,9 @@ export const useQueryParametersState = <ValueType>({
2525
}: QueryParametersStateProps<ValueType>): [ValueType, (value: ValueType | null) => void] => {
2626
const navigate = useNavigate();
2727
const location = useLocation();
28-
const queryParams = useSearchParams();
28+
const urlSearchParams = useURLSearchParams();
2929

30-
const existingValue = queryParams.get(key);
30+
const existingValue = urlSearchParams.get(key);
3131

3232
const parsedExistingValue = useMemo(() => {
3333
return existingValue ? decode(existingValue) : null;

‎src/hooks/useSearchParams.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎src/hooks/useQuery.ts renamed to ‎src/hooks/useURLSearchParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useMemo } from 'react';
22
import { useLocation } from 'react-router-dom-v5-compat';
33

4-
export function useQuery() {
4+
export function useURLSearchParams() {
55
const { search } = useLocation();
66

77
return useMemo(() => new URLSearchParams(search), [search]);

‎src/routing/InitialisedRouter.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getUserPermissions } from 'data/permissions';
1010
import { useFeatureFlagContext } from 'hooks/useFeatureFlagContext';
1111
import { useLimits } from 'hooks/useLimits';
1212
import { QueryParamMap, useNavigation } from 'hooks/useNavigation';
13-
import { useQuery } from 'hooks/useQuery';
13+
import { useURLSearchParams } from 'hooks/useURLSearchParams';
1414
import { SceneRedirecter } from 'components/SceneRedirecter';
1515
import { AlertingPage } from 'page/AlertingPage';
1616
import { CheckList } from 'page/CheckList';
@@ -32,23 +32,23 @@ import { SceneHomepage } from 'page/SceneHomepage';
3232
import { UnauthorizedPage } from 'page/UnauthorizedPage';
3333

3434
export const InitialisedRouter = () => {
35-
const queryParams = useQuery();
35+
const urlSearchParams = useURLSearchParams();
3636
const navigate = useNavigation();
3737
const { isFeatureEnabled } = useFeatureFlagContext();
3838

39-
const page = queryParams.get('page');
39+
const page = urlSearchParams.get('page');
4040
useLimits();
4141

4242
useEffect(() => {
4343
if (page) {
44-
queryParams.delete('page');
45-
const params = queryParams.toString();
44+
urlSearchParams.delete('page');
45+
const params = urlSearchParams.toString();
4646
const path = `${page}${params ? '?' : ''}${params}`;
4747
const translated: QueryParamMap = {};
48-
queryParams.forEach((value, name) => (translated[name] = value));
48+
urlSearchParams.forEach((value, name) => (translated[name] = value));
4949
navigate(path, translated);
5050
}
51-
}, [page, navigate, queryParams]);
51+
}, [page, navigate, urlSearchParams]);
5252

5353
const { canWriteChecks, canReadChecks, canReadProbes } = getUserPermissions();
5454

0 commit comments

Comments
 (0)