Просмотр исходного кода

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)
Michael Sun 7 месяцев назад
Родитель
Сommit
718f63febf

+ 0 - 0
static/app/types/views.tsx


+ 59 - 0
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<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 - 0
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<UseApiQueryOptions<FetchGroupSearchViewsResponse>> = {}
+) => {
+  return useApiQuery<FetchGroupSearchViewsResponse>(
+    makeFetchGroupSearchViewsKey({orgSlug}),
+    {
+      staleTime: Infinity,
+      ...options,
+    }
+  );
+};

+ 8 - 0
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<TagValue[]>;
 
@@ -13,3 +14,10 @@ export type IssueUpdateData =
   | {priority: PriorityLevel}
   | MarkReviewed
   | GroupStatusResolution;
+
+export type GroupSearchView = {
+  name: string;
+  query: string;
+  querySort: IssueSortOptions;
+  id?: string;
+};