Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion static/app/utils/useUrlParams.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import useUrlParams from './useUrlParams';
jest.mock('react-router');
jest.mock('sentry/utils/useLocation');

type Query = {limit: string; page: string};
type Query = {array: string[]; limit: string; page: string};

describe('useUrlParams', () => {
beforeEach(() => {
jest.mocked(browserHistory.getCurrentLocation).mockReturnValue({
query: {
page: '3',
limit: '50',
array: ['first', 'second'],
},
} as Location<Query>);
});
Expand All @@ -25,6 +26,7 @@ describe('useUrlParams', () => {

expect(result.current.getParamValue('page')).toBe('3');
expect(result.current.getParamValue('limit')).toBe('50');
expect(result.current.getParamValue('array')).toBe('first');
expect(result.current.getParamValue('foo')).toBeUndefined();
});

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

expect(browserHistory.push).toHaveBeenCalledWith({
query: {
array: ['first', 'second'],
page: '4',
limit: '50',
},
Expand All @@ -69,6 +72,7 @@ describe('useUrlParams', () => {

expect(browserHistory.push).toHaveBeenCalledWith({
query: {
array: ['first', 'second'],
page: '4',
limit: '50',
},
Expand Down
5 changes: 4 additions & 1 deletion static/app/utils/useUrlParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ function useUrlParams(defaultKey?: string, defaultValue?: string) {
const getParamValue = useCallback(
(key: string) => {
const location = browserHistory.getCurrentLocation();
return location.query[key] ?? defaultValue;
// 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[]
return Array.isArray(location.query[key])
? location.query[key]?.at(0) ?? defaultValue
: location.query[key] ?? defaultValue;
Comment on lines +24 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk what's easier to read

      return (
        Array.isArray(location.query[key])
          ? location.query[key]?.at(0)
          : location.query[key]
        ) ?? defaultValue;

},
[defaultValue]
);
Expand Down