Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,74 +1,48 @@
import type {ReactNode} from 'react';
import {useCallback, useMemo} from 'react';
import type {Location} from 'history';
import {parseAsString, useQueryStates} from 'nuqs';

import {updateNullableLocation} from 'sentry/utils/url/updateNullableLocation';
import {useLocation} from 'sentry/utils/useLocation';
import {useNavigate} from 'sentry/utils/useNavigate';
import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
import {QueryParamsContextProvider} from 'sentry/views/explore/queryParams/context';
import {getQueryFromLocation} from 'sentry/views/explore/queryParams/query';
import {ReadableQueryParams} from 'sentry/views/explore/queryParams/readableQueryParams';
import {
getIdFromLocation,
getTitleFromLocation,
ID_KEY,
TITLE_KEY,
} from 'sentry/views/explore/queryParams/savedQuery';
import type {WritableQueryParams} from 'sentry/views/explore/queryParams/writableQueryParams';

const REPLAY_QUERY_KEY = 'query';

function getReadableQueryParamsFromLocation(location: Location): ReadableQueryParams {
const query = getQueryFromLocation(location, REPLAY_QUERY_KEY) ?? '';
const id = getIdFromLocation(location, ID_KEY);
const title = getTitleFromLocation(location, TITLE_KEY);

return new ReadableQueryParams({
extrapolate: false,
mode: Mode.SAMPLES,
query,
cursor: '',
fields: [],
sortBys: [],
aggregateCursor: '',
aggregateFields: [],
aggregateSortBys: [],
id,
title,
});
}

function getTargetWithReadableQueryParams(
location: Location,
writableQueryParams: WritableQueryParams
): Location {
const target: Location = {...location, query: {...location.query}};

updateNullableLocation(target, REPLAY_QUERY_KEY, writableQueryParams.query);

return target;
}

interface ReplayQueryParamsProviderProps {
children: ReactNode;
}

export function ReplayQueryParamsProvider({children}: ReplayQueryParamsProviderProps) {
const location = useLocation();
const navigate = useNavigate();
const [queryParams, setNuqsParams] = useQueryStates({
query: parseAsString.withDefault(''),
id: parseAsString,
title: parseAsString,
});

const readableQueryParams = useMemo(
() => getReadableQueryParamsFromLocation(location),
[location]
() =>
new ReadableQueryParams({
extrapolate: false,
mode: Mode.SAMPLES,
query: queryParams.query,
cursor: '',
fields: [],
sortBys: [],
aggregateCursor: '',
aggregateFields: [],
aggregateSortBys: [],
id: queryParams.id ?? undefined,
title: queryParams.title ?? undefined,
}),
[queryParams.query, queryParams.id, queryParams.title]
);

const setWritableQueryParams = useCallback(
(writableQueryParams: WritableQueryParams) => {
const target = getTargetWithReadableQueryParams(location, writableQueryParams);
navigate(target);
setNuqsParams({
query: writableQueryParams.query ?? null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Undefined query coerced to null changes preservation semantics

Low Severity

The expression writableQueryParams.query ?? null collapses undefined into null. With the old updateNullableLocation, passing undefined was a no-op (the current query param was preserved), while null removed it from the URL. With nuqs, null also removes the param, and undefined leaves it as-is. The ?? null coercion means any caller passing a WritableQueryParams without a query field will now inadvertently clear the query from the URL instead of preserving it. No current replay component triggers this path, but it breaks the semantic contract of the old code.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4c98b10. Configure here.

});
},
[location, navigate]
[setNuqsParams]
);

return (
Expand Down
Loading