Skip to content

Commit 939d768

Browse files
AbhiPrasadTkDodo
authored andcommitted
ref(settings): Use nuqs to manage project key query state (#102082)
A follow up to #102010 (comment) that converts the query param logic to use `nuqs`. Much nicer! --------- Co-authored-by: Dominik Dorfmeister <dominik.dorfmeister@sentry.io>
1 parent e41923c commit 939d768

File tree

1 file changed

+11
-27
lines changed
  • static/app/views/settings/project/projectKeys/credentials

1 file changed

+11
-27
lines changed

static/app/views/settings/project/projectKeys/credentials/index.tsx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import {Fragment, useMemo} from 'react';
22
import styled from '@emotion/styled';
3+
import {parseAsBoolean, parseAsStringLiteral, useQueryState} from 'nuqs';
34

45
import {ExternalLink, Link} from 'sentry/components/core/link';
56
import {TabList, Tabs} from 'sentry/components/core/tabs';
67
import FieldGroup from 'sentry/components/forms/fieldGroup';
78
import TextCopyInput from 'sentry/components/textCopyInput';
89
import {t, tct} from 'sentry/locale';
910
import type {ProjectKey} from 'sentry/types/project';
10-
import {decodeScalar} from 'sentry/utils/queryString';
1111
import {useLocation} from 'sentry/utils/useLocation';
12-
import {useNavigate} from 'sentry/utils/useNavigate';
1312
import {OtlpTab} from 'sentry/views/settings/project/projectKeys/credentials/otlp';
1413
import {VercelTab} from 'sentry/views/settings/project/projectKeys/credentials/vercel';
1514

@@ -180,9 +179,7 @@ function ProjectKeyCredentials({
180179
showUnreal = true,
181180
}: Props) {
182181
const location = useLocation();
183-
const navigate = useNavigate();
184182

185-
// Calculate available tabs based on props
186183
const availableTabs = useMemo<TabConfig[]>(() => {
187184
const tabs: TabConfig[] = [
188185
{
@@ -229,29 +226,17 @@ function ProjectKeyCredentials({
229226
showProjectId,
230227
]);
231228

232-
// Get showDeprecatedDsn from query params
233-
const showDeprecatedDsn = decodeScalar(location?.query?.showDeprecated) === 'true';
229+
const [showDeprecatedDsn] = useQueryState('showDeprecated', parseAsBoolean);
234230

235-
// Get current tab from query params, defaulting to first available
236-
const getCurrentTab = (): TabValue => {
237-
const queryTab = decodeScalar(location?.query?.tab);
238-
const validTabs = availableTabs.map(tab => tab.key);
239-
return validTabs.includes(queryTab as TabValue)
240-
? (queryTab as TabValue)
241-
: (availableTabs[0]?.key ?? 'otlp');
242-
};
243-
244-
const activeTab = getCurrentTab();
231+
const tabParser = useMemo(
232+
() => ({
233+
...parseAsStringLiteral(availableTabs.map(tab => tab.key)),
234+
defaultValue: availableTabs[0]?.key ?? 'otlp',
235+
}),
236+
[availableTabs]
237+
);
245238

246-
const handleTabChange = (newTab: TabValue) => {
247-
navigate({
248-
pathname: location.pathname,
249-
query: {
250-
...location.query,
251-
tab: newTab,
252-
},
253-
});
254-
};
239+
const [activeTab, setActiveTab] = useQueryState('tab', tabParser);
255240

256241
const renderTabContent = () => {
257242
switch (activeTab) {
@@ -309,7 +294,6 @@ function ProjectKeyCredentials({
309294
link: showDsn ? (
310295
<Link
311296
to={{
312-
...location,
313297
query: {
314298
...location.query,
315299
showDeprecated: showDeprecatedDsn ? undefined : 'true',
@@ -365,7 +349,7 @@ function ProjectKeyCredentials({
365349

366350
{availableTabs.length > 0 && (
367351
<Fragment>
368-
<Tabs value={activeTab} onChange={handleTabChange}>
352+
<Tabs value={activeTab} onChange={setActiveTab}>
369353
<TabList>
370354
{availableTabs.map(tab => (
371355
<TabList.Item key={tab.key}>{tab.label}</TabList.Item>

0 commit comments

Comments
 (0)