-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(replay): Fixes type error if same param is in url #68851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Bundle ReportChanges will decrease total bundle size by 33.53kB ⬇️
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is valid behavior, we shouldn't force this in the hook, but it should be handled in the hook consumers
We currently have the return type of the hook set to |
static/app/utils/useUrlParams.tsx
Outdated
@@ -20,7 +20,9 @@ function useUrlParams(defaultKey?: string, defaultValue?: string) { | |||
const getParamValue = useCallback( | |||
(key: string) => { | |||
const location = browserHistory.getCurrentLocation(); | |||
return location.query[key] ?? defaultValue; | |||
return Array.isArray(location.query[key]) | |||
? location.query[key]?.[0] ?? defaultValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? location.query[key]?.[0] ?? defaultValue | |
? location.query[key]?.at(0) ?? defaultValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because of noUncheckedIndexedAccess
OK, let's keep it as-is in that case since they are already expecting a singular value (the function name reinforces this). If they need an array, they can later add on a different getter. Let's add a comment here about why we're doing this and why first value over last (even if it's we are arbitrarily picking one, it's plenty helpful), and also a test. |
return Array.isArray(location.query[key]) | ||
? location.query[key]?.at(0) ?? defaultValue | ||
: location.query[key] ?? defaultValue; |
There was a problem hiding this comment.
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;
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