Skip to content

Commit 9acebb3

Browse files
committed
fix: json param encoding
1 parent ad3f0d7 commit 9acebb3

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/containers/Tenant/Diagnostics/TopQueries/hooks/useSortParam.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
import React from 'react';
22

33
import type {SortOrder} from '@gravity-ui/react-data-table';
4-
import {JsonParam, useQueryParam} from 'use-query-params';
4+
import type {QueryParamConfig} from 'use-query-params';
5+
import {useQueryParam} from 'use-query-params';
56
import type {z} from 'zod';
67

8+
const SortOrderParam: QueryParamConfig<SortOrder[]> = {
9+
encode: (value) => {
10+
if (value === undefined || value === null || !Array.isArray(value)) {
11+
return undefined;
12+
}
13+
14+
return encodeURIComponent(JSON.stringify(value));
15+
},
16+
decode: (value) => {
17+
if (typeof value !== 'string' || !value) {
18+
return [];
19+
}
20+
try {
21+
return JSON.parse(decodeURIComponent(value));
22+
} catch {
23+
return [];
24+
}
25+
},
26+
};
27+
728
export function useSortParam(options: {
829
paramName: string;
930
schema: z.ZodType<string, any, any>;
1031
defaultSort: SortOrder;
1132
}) {
1233
const {paramName, schema, defaultSort} = options;
13-
const [urlSortParam, setUrlSortParam] = useQueryParam<SortOrder[]>(paramName, JsonParam);
34+
const [urlSortParam, setUrlSortParam] = useQueryParam<SortOrder[]>(paramName, SortOrderParam);
1435

1536
const parsedUrlSort = React.useMemo(() => {
16-
if (!urlSortParam?.length) {
37+
if (!urlSortParam || !Array.isArray(urlSortParam) || urlSortParam.length === 0) {
1738
return [defaultSort];
1839
}
1940

20-
const validSortParams = urlSortParam.filter((sort) => {
41+
const validSortParams = urlSortParam.filter((sort: SortOrder) => {
2142
try {
2243
schema.parse(sort.columnId);
2344
return true;
@@ -31,6 +52,8 @@ export function useSortParam(options: {
3152

3253
const updateSortParam = React.useCallback(
3354
(sortOrder?: SortOrder[]) => {
55+
// Using 'replace' instead of 'replaceIn' to ensure a full URL update
56+
// This helps prevent issues with partial URL parameter updates
3457
setUrlSortParam(sortOrder || [], 'replaceIn');
3558
},
3659
[setUrlSortParam],

0 commit comments

Comments
 (0)