Skip to content

Commit 464f83f

Browse files
committed
Update DNS actions to use new dynamic actions
1 parent c6cb717 commit 464f83f

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

static/app/components/commandPalette/useDsnLookupActions.spec.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,31 @@ describe('useDsnLookupActions', () => {
9696
);
9797
});
9898

99-
it('returns empty array for non-DSN query', () => {
99+
it('returns empty array for non-DSN query', async () => {
100100
const mockApi = MockApiClient.addMockResponse({
101101
url: `/organizations/${org.slug}/dsn-lookup/`,
102102
body: {},
103103
});
104104

105105
renderWithProvider('some random text');
106106

107-
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
107+
await waitFor(() => {
108+
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
109+
});
108110
expect(mockApi).not.toHaveBeenCalled();
109111
});
110112

111-
it('returns empty array for empty query', () => {
113+
it('returns empty array for empty query', async () => {
112114
const mockApi = MockApiClient.addMockResponse({
113115
url: `/organizations/${org.slug}/dsn-lookup/`,
114116
body: {},
115117
});
116118

117119
renderWithProvider('');
118120

119-
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
121+
await waitFor(() => {
122+
expect(screen.queryAllByRole('listitem')).toHaveLength(0);
123+
});
120124
expect(mockApi).not.toHaveBeenCalled();
121125
});
122126
});
Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {useMemo} from 'react';
1+
import {useCallback} from 'react';
22

3-
import {useCommandPaletteQueryState} from 'sentry/components/commandPalette/context';
3+
import {makeCommandPaletteLink} from 'sentry/components/commandPalette/makeCommandPaletteAction';
44
import type {CommandPaletteAction} from 'sentry/components/commandPalette/types';
5-
import {useCommandPaletteActions} from 'sentry/components/commandPalette/useCommandPaletteActions';
5+
import {useDynamicCommandPaletteAction} from 'sentry/components/commandPalette/useDynamicCommandPaletteAction';
66
import {
77
DSN_PATTERN,
88
getDsnNavTargets,
99
} from 'sentry/components/search/sources/dsnLookupUtils';
1010
import type {DsnLookupResponse} from 'sentry/components/search/sources/dsnLookupUtils';
1111
import {IconIssues, IconList, IconSettings} from 'sentry/icons';
1212
import {getApiUrl} from 'sentry/utils/api/getApiUrl';
13-
import {useApiQuery} from 'sentry/utils/queryClient';
13+
import {useApi} from 'sentry/utils/useApi';
1414
import {useOrganization} from 'sentry/utils/useOrganization';
1515

1616
const ICONS: React.ReactElement[] = [
@@ -20,40 +20,36 @@ const ICONS: React.ReactElement[] = [
2020
];
2121

2222
export function useDsnLookupActions(): void {
23-
const {query} = useCommandPaletteQueryState();
23+
const api = useApi();
2424
const organization = useOrganization({allowNull: true});
2525
const hasDsnLookup = organization?.features?.includes('cmd-k-dsn-lookup') ?? false;
26-
const isDsn = DSN_PATTERN.test(query);
27-
28-
const {data} = useApiQuery<DsnLookupResponse>(
29-
[
30-
getApiUrl('/organizations/$organizationIdOrSlug/dsn-lookup/', {
31-
path: {organizationIdOrSlug: organization?.slug ?? ''},
32-
}),
33-
{query: {dsn: query}},
34-
],
35-
{
36-
staleTime: 30_000,
37-
enabled: isDsn && !!organization && hasDsnLookup,
38-
}
26+
27+
const queryAction = useCallback(
28+
async (query: string): Promise<CommandPaletteAction[]> => {
29+
if (!DSN_PATTERN.test(query) || !organization || !hasDsnLookup) {
30+
return [];
31+
}
32+
33+
const url = getApiUrl('/organizations/$organizationIdOrSlug/dsn-lookup/', {
34+
path: {organizationIdOrSlug: organization.slug},
35+
});
36+
37+
const data = await api.requestPromise(url, {query: {dsn: query}});
38+
39+
return getDsnNavTargets(data as DsnLookupResponse).map((target, i) =>
40+
makeCommandPaletteLink({
41+
display: {
42+
label: target.label,
43+
details: target.description,
44+
icon: ICONS[i],
45+
},
46+
groupingKey: 'search-result',
47+
to: target.to,
48+
})
49+
);
50+
},
51+
[api, organization, hasDsnLookup]
3952
);
4053

41-
const actions: CommandPaletteAction[] = useMemo(() => {
42-
if (!isDsn || !data) {
43-
return [];
44-
}
45-
46-
return getDsnNavTargets(data).map((target, i) => ({
47-
type: 'navigate' as const,
48-
to: target.to,
49-
display: {
50-
label: target.label,
51-
details: target.description,
52-
icon: ICONS[i],
53-
},
54-
groupingKey: 'search-result' as const,
55-
}));
56-
}, [isDsn, data]);
57-
58-
useCommandPaletteActions(actions);
54+
useDynamicCommandPaletteAction(queryAction);
5955
}

0 commit comments

Comments
 (0)