Skip to content

Commit

Permalink
feat(custom-views): Create FE hooks for group search view endpoint (#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
MichaelSun48 authored and roaga committed Jul 31, 2024
1 parent 9741c8c commit 2af053d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
Empty file added static/app/types/views.tsx
Empty file.
59 changes: 59 additions & 0 deletions static/app/views/issueList/mutations/useUpdateGroupSearchViews.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {addErrorMessage} from 'sentry/actionCreators/indicator';
import {t} from 'sentry/locale';
import {
setApiQueryData,
useMutation,
type UseMutationOptions,
useQueryClient,
} from 'sentry/utils/queryClient';
import type RequestError from 'sentry/utils/requestError/requestError';
import useApi from 'sentry/utils/useApi';
import {makeFetchGroupSearchViewsKey} from 'sentry/views/issueList/queries/useFetchGroupSearchViews';
import type {GroupSearchView} from 'sentry/views/issueList/types';

type UpdateGroupSearchViewsVariables = {
groupSearchViews: GroupSearchView[];
orgSlug: string;
};

// The PUT groupsearchviews endpoint updates the views AND returns the updated views
type UpdateGroupSearchViewResponse = GroupSearchView[];

export const useUpdateGroupSearchViews = (
options: Omit<
UseMutationOptions<
UpdateGroupSearchViewResponse,
RequestError,
UpdateGroupSearchViewsVariables
>,
'mutationFn'
> = {}
) => {
const api = useApi();
const queryClient = useQueryClient();

return useMutation<
UpdateGroupSearchViewResponse,
RequestError,
UpdateGroupSearchViewsVariables
>({
...options,
mutationFn: ({orgSlug, groupSearchViews: data}: UpdateGroupSearchViewsVariables) =>
api.requestPromise(`/organizations/${orgSlug}/group-search-views/`, {
method: 'PUT',
data,
}),
onSuccess: (groupSearchViews, parameters, context) => {
setApiQueryData<GroupSearchView[]>(
queryClient,
makeFetchGroupSearchViewsKey({orgSlug: parameters.orgSlug}),
groupSearchViews // Update the cache with the new groupSearchViews
);
options.onSuccess?.(groupSearchViews, parameters, context);
},
onError: (error, variables, context) => {
addErrorMessage(t('Failed to update views'));
options.onError?.(error, variables, context);
},
});
};
27 changes: 27 additions & 0 deletions static/app/views/issueList/queries/useFetchGroupSearchViews.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {UseApiQueryOptions} from 'sentry/utils/queryClient';
import {useApiQuery} from 'sentry/utils/queryClient';
import type {GroupSearchView} from 'sentry/views/issueList/types';

type FetchGroupSearchViewsParameters = {
orgSlug: string;
};

type FetchGroupSearchViewsResponse = GroupSearchView[];

export const makeFetchGroupSearchViewsKey = ({
orgSlug,
}: FetchGroupSearchViewsParameters) =>
[`/organizations/${orgSlug}/group-search-views/`] as const;

export const useFetchGroupSearchViewsForOrg = (
{orgSlug}: FetchGroupSearchViewsParameters,
options: Partial<UseApiQueryOptions<FetchGroupSearchViewsResponse>> = {}
) => {
return useApiQuery<FetchGroupSearchViewsResponse>(
makeFetchGroupSearchViewsKey({orgSlug}),
{
staleTime: Infinity,
...options,
}
);
};
8 changes: 8 additions & 0 deletions static/app/views/issueList/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
PriorityLevel,
TagValue,
} from 'sentry/types';
import type {IssueSortOptions} from 'sentry/views/issueList/utils';

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

Expand All @@ -13,3 +14,10 @@ export type IssueUpdateData =
| {priority: PriorityLevel}
| MarkReviewed
| GroupStatusResolution;

export type GroupSearchView = {
name: string;
query: string;
querySort: IssueSortOptions;
id?: string;
};

0 comments on commit 2af053d

Please sign in to comment.