From 2af053dc1de61c01317718110c62a25ba1a9d0d9 Mon Sep 17 00:00:00 2001 From: Michael Sun <55160142+MichaelSun48@users.noreply.github.com> Date: Mon, 29 Jul 2024 13:33:46 -0700 Subject: [PATCH] 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) --- static/app/types/views.tsx | 0 .../mutations/useUpdateGroupSearchViews.tsx | 59 +++++++++++++++++++ .../queries/useFetchGroupSearchViews.tsx | 27 +++++++++ static/app/views/issueList/types.tsx | 8 +++ 4 files changed, 94 insertions(+) create mode 100644 static/app/types/views.tsx create mode 100644 static/app/views/issueList/mutations/useUpdateGroupSearchViews.tsx create mode 100644 static/app/views/issueList/queries/useFetchGroupSearchViews.tsx diff --git a/static/app/types/views.tsx b/static/app/types/views.tsx new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/static/app/views/issueList/mutations/useUpdateGroupSearchViews.tsx b/static/app/views/issueList/mutations/useUpdateGroupSearchViews.tsx new file mode 100644 index 00000000000000..dee37bfaca884c --- /dev/null +++ b/static/app/views/issueList/mutations/useUpdateGroupSearchViews.tsx @@ -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( + 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); + }, + }); +}; diff --git a/static/app/views/issueList/queries/useFetchGroupSearchViews.tsx b/static/app/views/issueList/queries/useFetchGroupSearchViews.tsx new file mode 100644 index 00000000000000..a6707512d62c68 --- /dev/null +++ b/static/app/views/issueList/queries/useFetchGroupSearchViews.tsx @@ -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> = {} +) => { + return useApiQuery( + makeFetchGroupSearchViewsKey({orgSlug}), + { + staleTime: Infinity, + ...options, + } + ); +}; diff --git a/static/app/views/issueList/types.tsx b/static/app/views/issueList/types.tsx index 926ab5769338d1..6694a553c5ce87 100644 --- a/static/app/views/issueList/types.tsx +++ b/static/app/views/issueList/types.tsx @@ -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; @@ -13,3 +14,10 @@ export type IssueUpdateData = | {priority: PriorityLevel} | MarkReviewed | GroupStatusResolution; + +export type GroupSearchView = { + name: string; + query: string; + querySort: IssueSortOptions; + id?: string; +};