Skip to content

Commit 718f63f

Browse files
authored
feat(custom-views): Create FE hooks for group search view endpoint (#75188)
This PR creates the `useApiQuery` and `useMutation` hooks to call the GET and PUT groupsearchview endpoints, respectively. The endpoints can be found [here](https://github.com/getsentry/sentry/blob/master/src/sentry/issues/endpoints/organization_group_search_views.py)
1 parent daf8464 commit 718f63f

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

static/app/types/views.tsx

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {addErrorMessage} from 'sentry/actionCreators/indicator';
2+
import {t} from 'sentry/locale';
3+
import {
4+
setApiQueryData,
5+
useMutation,
6+
type UseMutationOptions,
7+
useQueryClient,
8+
} from 'sentry/utils/queryClient';
9+
import type RequestError from 'sentry/utils/requestError/requestError';
10+
import useApi from 'sentry/utils/useApi';
11+
import {makeFetchGroupSearchViewsKey} from 'sentry/views/issueList/queries/useFetchGroupSearchViews';
12+
import type {GroupSearchView} from 'sentry/views/issueList/types';
13+
14+
type UpdateGroupSearchViewsVariables = {
15+
groupSearchViews: GroupSearchView[];
16+
orgSlug: string;
17+
};
18+
19+
// The PUT groupsearchviews endpoint updates the views AND returns the updated views
20+
type UpdateGroupSearchViewResponse = GroupSearchView[];
21+
22+
export const useUpdateGroupSearchViews = (
23+
options: Omit<
24+
UseMutationOptions<
25+
UpdateGroupSearchViewResponse,
26+
RequestError,
27+
UpdateGroupSearchViewsVariables
28+
>,
29+
'mutationFn'
30+
> = {}
31+
) => {
32+
const api = useApi();
33+
const queryClient = useQueryClient();
34+
35+
return useMutation<
36+
UpdateGroupSearchViewResponse,
37+
RequestError,
38+
UpdateGroupSearchViewsVariables
39+
>({
40+
...options,
41+
mutationFn: ({orgSlug, groupSearchViews: data}: UpdateGroupSearchViewsVariables) =>
42+
api.requestPromise(`/organizations/${orgSlug}/group-search-views/`, {
43+
method: 'PUT',
44+
data,
45+
}),
46+
onSuccess: (groupSearchViews, parameters, context) => {
47+
setApiQueryData<GroupSearchView[]>(
48+
queryClient,
49+
makeFetchGroupSearchViewsKey({orgSlug: parameters.orgSlug}),
50+
groupSearchViews // Update the cache with the new groupSearchViews
51+
);
52+
options.onSuccess?.(groupSearchViews, parameters, context);
53+
},
54+
onError: (error, variables, context) => {
55+
addErrorMessage(t('Failed to update views'));
56+
options.onError?.(error, variables, context);
57+
},
58+
});
59+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type {UseApiQueryOptions} from 'sentry/utils/queryClient';
2+
import {useApiQuery} from 'sentry/utils/queryClient';
3+
import type {GroupSearchView} from 'sentry/views/issueList/types';
4+
5+
type FetchGroupSearchViewsParameters = {
6+
orgSlug: string;
7+
};
8+
9+
type FetchGroupSearchViewsResponse = GroupSearchView[];
10+
11+
export const makeFetchGroupSearchViewsKey = ({
12+
orgSlug,
13+
}: FetchGroupSearchViewsParameters) =>
14+
[`/organizations/${orgSlug}/group-search-views/`] as const;
15+
16+
export const useFetchGroupSearchViewsForOrg = (
17+
{orgSlug}: FetchGroupSearchViewsParameters,
18+
options: Partial<UseApiQueryOptions<FetchGroupSearchViewsResponse>> = {}
19+
) => {
20+
return useApiQuery<FetchGroupSearchViewsResponse>(
21+
makeFetchGroupSearchViewsKey({orgSlug}),
22+
{
23+
staleTime: Infinity,
24+
...options,
25+
}
26+
);
27+
};

static/app/views/issueList/types.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
PriorityLevel,
55
TagValue,
66
} from 'sentry/types';
7+
import type {IssueSortOptions} from 'sentry/views/issueList/utils';
78

89
export type TagValueLoader = (key: string, search: string) => Promise<TagValue[]>;
910

@@ -13,3 +14,10 @@ export type IssueUpdateData =
1314
| {priority: PriorityLevel}
1415
| MarkReviewed
1516
| GroupStatusResolution;
17+
18+
export type GroupSearchView = {
19+
name: string;
20+
query: string;
21+
querySort: IssueSortOptions;
22+
id?: string;
23+
};

0 commit comments

Comments
 (0)