Skip to content

Commit 978068f

Browse files
authored
fix(replay): Fixes type error if same param is in url (#68851)
There are edge cases where getParamValue() will have a return type of string[] due to `location.query[key]` which caused the type error. This edge case happens when the same key is in the URL more than once, which could happen if someone pastes the url twice. We are expecting a string from this function, so if `location.query[key]` is an array, we will just return the first value in the array. Fixes JAVASCRIPT-2SFK
1 parent 23daa9e commit 978068f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

static/app/utils/useUrlParams.spec.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import useUrlParams from './useUrlParams';
88
jest.mock('react-router');
99
jest.mock('sentry/utils/useLocation');
1010

11-
type Query = {limit: string; page: string};
11+
type Query = {array: string[]; limit: string; page: string};
1212

1313
describe('useUrlParams', () => {
1414
beforeEach(() => {
1515
jest.mocked(browserHistory.getCurrentLocation).mockReturnValue({
1616
query: {
1717
page: '3',
1818
limit: '50',
19+
array: ['first', 'second'],
1920
},
2021
} as Location<Query>);
2122
});
@@ -25,6 +26,7 @@ describe('useUrlParams', () => {
2526

2627
expect(result.current.getParamValue('page')).toBe('3');
2728
expect(result.current.getParamValue('limit')).toBe('50');
29+
expect(result.current.getParamValue('array')).toBe('first');
2830
expect(result.current.getParamValue('foo')).toBeUndefined();
2931
});
3032

@@ -54,6 +56,7 @@ describe('useUrlParams', () => {
5456

5557
expect(browserHistory.push).toHaveBeenCalledWith({
5658
query: {
59+
array: ['first', 'second'],
5760
page: '4',
5861
limit: '50',
5962
},
@@ -69,6 +72,7 @@ describe('useUrlParams', () => {
6972

7073
expect(browserHistory.push).toHaveBeenCalledWith({
7174
query: {
75+
array: ['first', 'second'],
7276
page: '4',
7377
limit: '50',
7478
},

static/app/utils/useUrlParams.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function useUrlParams(defaultKey?: string, defaultValue?: string) {
2020
const getParamValue = useCallback(
2121
(key: string) => {
2222
const location = browserHistory.getCurrentLocation();
23-
return location.query[key] ?? defaultValue;
23+
// location.query.key can return string[] but we expect a singular value from this function, so we return the first string (this is picked arbitrarily) if it's string[]
24+
return Array.isArray(location.query[key])
25+
? location.query[key]?.at(0) ?? defaultValue
26+
: location.query[key] ?? defaultValue;
2427
},
2528
[defaultValue]
2629
);

0 commit comments

Comments
 (0)