]> source.dussan.org Git - sonarqube.git/commitdiff
[NO JIRA] allow createQueryHook without params
authorViktor Vorona <viktor.vorona@sonarsource.com>
Wed, 24 Jul 2024 10:11:41 +0000 (12:11 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 25 Jul 2024 20:02:50 +0000 (20:02 +0000)
server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx
server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx
server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx
server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx
server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx
server/sonar-web/src/main/js/queries/common.ts
server/sonar-web/src/main/js/queries/devops-integration.ts
server/sonar-web/src/main/js/queries/system.ts

index 675966d6dc14522cd7e8560cd674f91dd8c7262c..e5c692ba5ccc9b4253e93f70a4bb30af2e7457fc 100644 (file)
@@ -56,7 +56,7 @@ export interface TutorialSelectionRendererProps {
   currentUser: LoggedInUser;
   currentUserCanScanProject: boolean;
   loading: boolean;
-  projectBinding?: ProjectAlmBindingResponse;
+  projectBinding?: ProjectAlmBindingResponse | null;
   selectedTutorial?: TutorialModes;
   willRefreshAutomatically?: boolean;
 }
index 4e1b4ac0f02278eb9c17091bab1d8586697d428e..9655bc5fc29f0c24d4b34cb95e7fba28458eba7a 100644 (file)
@@ -41,7 +41,7 @@ export interface MultiBranchPipelineStepProps {
   alm: AlmKeys;
   almBinding?: AlmSettingsInstance;
 
-  projectBinding?: ProjectAlmBindingResponse;
+  projectBinding?: ProjectAlmBindingResponse | null;
 }
 
 /* Capture [workspaceID] from this pattern: https://bitbucket.org/[workspaceId]/  */
index 44b74bd66238fb51035df5b101c932b811b13354..504b76938849b1a83f10c4c0be00daf0c13e56d2 100644 (file)
@@ -35,7 +35,7 @@ export interface WebhookStepProps {
   alm: AlmKeys;
   almBinding?: AlmSettingsInstance;
   branchesEnabled: boolean;
-  projectBinding?: ProjectAlmBindingResponse;
+  projectBinding?: ProjectAlmBindingResponse | null;
 }
 
 function renderAlmSpecificInstructions(props: WebhookStepProps) {
index cddf01e85d2802038694eacda8e458dc26d0cf0c..884e7fb04bacf1e719fa47af95ed68eb769e0351 100644 (file)
@@ -36,7 +36,7 @@ export interface WebhookStepBitbucketProps {
   alm: AlmKeys;
   almBinding?: AlmSettingsInstance;
   branchesEnabled: boolean;
-  projectBinding?: ProjectAlmBindingResponse;
+  projectBinding?: ProjectAlmBindingResponse | null;
 }
 
 function buildUrlSnippet(
index de5f5bace1aba275d3317f233cbb0d2ee7ad42be..9574d4c0bd69f28104b972447c7182359161ff29 100644 (file)
@@ -29,7 +29,7 @@ import { buildGithubLink } from '../utils';
 export interface WebhookStepGithubProps {
   almBinding?: AlmSettingsInstance;
   branchesEnabled: boolean;
-  projectBinding?: ProjectAlmBindingResponse;
+  projectBinding?: ProjectAlmBindingResponse | null;
 }
 
 export default function WebhookStepGithub(props: WebhookStepGithubProps) {
index 9bf450ae683dfd7f4b1920110434b4442b050427..fe56e4c8c3822ff1889a913b21aaf7d565f07082 100644 (file)
@@ -29,23 +29,38 @@ import {
   useQuery,
 } from '@tanstack/react-query';
 
+const isFnWithoutParams = <T>(fn: ((data: any) => T) | (() => T)): fn is () => T => fn.length === 0;
+
 export function createQueryHook<
   T = unknown,
   TQueryData = unknown,
-  TError = Error,
+  TError extends Error = Error,
   TData = TQueryData,
   TQueryKey extends QueryKey = QueryKey,
 >(
-  fn: (data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>,
-): <SelectType = TQueryData>(
-  data: T,
-  options?: Omit<
-    UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
-    'queryKey' | 'queryFn'
-  >,
-) => UseQueryResult<SelectType, TError>;
+  fn:
+    | ((data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>)
+    | (() => UseQueryOptions<TQueryData, TError, TData, TQueryKey>),
+): unknown extends T
+  ? <SelectType = TQueryData>(
+      options?: Omit<
+        UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+        'queryKey' | 'queryFn'
+      >,
+    ) => UseQueryResult<SelectType, TError>
+  : <SelectType = TQueryData>(
+      data: T,
+      options?: Omit<
+        UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+        'queryKey' | 'queryFn'
+      >,
+    ) => UseQueryResult<SelectType, TError>;
 
-export function createQueryHook(fn: (data: any) => UseQueryOptions) {
+export function createQueryHook(fn: ((data: any) => UseQueryOptions) | (() => UseQueryOptions)) {
+  if (isFnWithoutParams(fn)) {
+    return (options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
+      useQuery({ ...fn(), ...options });
+  }
   return (data: any, options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
     useQuery({ ...fn(data), ...options });
 }
@@ -59,18 +74,58 @@ export function createInfiniteQueryHook<
   TQueryKey extends QueryKey = QueryKey,
   TPageParam = unknown,
 >(
-  fn: (
-    data: T,
-  ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>,
-): <SelectType = TData>(
-  data: T,
-  options?: Omit<
-    UseInfiniteQueryOptions<TQueryFnData, TError, SelectType, TQueryData, TQueryKey, TPageParam>,
-    'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
-  >,
-) => UseInfiniteQueryResult<SelectType, TError>;
+  fn:
+    | ((
+        data: T,
+      ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>)
+    | (() => UseInfiniteQueryOptions<
+        TQueryFnData,
+        TError,
+        TData,
+        TQueryData,
+        TQueryKey,
+        TPageParam
+      >),
+): unknown extends T
+  ? <SelectType = TData>(
+      options?: Omit<
+        UseInfiniteQueryOptions<
+          TQueryFnData,
+          TError,
+          SelectType,
+          TQueryData,
+          TQueryKey,
+          TPageParam
+        >,
+        'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+      >,
+    ) => UseInfiniteQueryResult<SelectType, TError>
+  : <SelectType = TData>(
+      data: T,
+      options?: Omit<
+        UseInfiniteQueryOptions<
+          TQueryFnData,
+          TError,
+          SelectType,
+          TQueryData,
+          TQueryKey,
+          TPageParam
+        >,
+        'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+      >,
+    ) => UseInfiniteQueryResult<SelectType, TError>;
 
-export function createInfiniteQueryHook(fn: (data: any) => UseInfiniteQueryOptions) {
+export function createInfiniteQueryHook(
+  fn: ((data?: any) => UseInfiniteQueryOptions) | (() => UseInfiniteQueryOptions),
+) {
+  if (isFnWithoutParams(fn)) {
+    return (
+      options?: Omit<
+        UseInfiniteQueryOptions,
+        'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+      >,
+    ) => useInfiniteQuery({ ...fn(), ...options });
+  }
   return (
     data: any,
     options?: Omit<
index ff7a6478ea2654751146f58eb4559f7980c29441..fc5824edfcbd4c879b926d704f9278fc66d40003 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-import { UseQueryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import { queryOptions, useMutation, useQueryClient } from '@tanstack/react-query';
 import { useLocation } from 'react-router-dom';
 import {
   deleteProjectAlmBinding,
@@ -30,6 +30,7 @@ import {
 } from '../api/alm-settings';
 import { HttpStatus } from '../helpers/request';
 import { AlmKeys, ProjectAlmBindingParams, ProjectAlmBindingResponse } from '../types/alm-settings';
+import { createQueryHook } from './common';
 
 function useProjectKeyFromLocation() {
   const location = useLocation();
@@ -42,39 +43,27 @@ function useProjectKeyFromLocation() {
   return id as string;
 }
 
-export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
-  project?: string,
-  options?: Omit<
-    UseQueryOptions<
-      ProjectAlmBindingResponse | null,
-      Error,
-      T,
-      ['devops_integration', string, 'binding']
-    >,
-    'queryKey' | 'queryFn'
-  >,
-) {
+export const useProjectBindingQuery = createQueryHook((project?: string) => {
   const keyFromUrl = useProjectKeyFromLocation();
 
   const projectKey = project ?? keyFromUrl;
 
-  return useQuery({
+  return queryOptions({
     queryKey: ['devops_integration', projectKey ?? '_blank_', 'binding'],
     queryFn: ({ queryKey: [_, key] }) =>
       getProjectAlmBinding(key).catch((e: Response) => {
         if (e.status === HttpStatus.NotFound) {
           return null;
         }
-        return e;
+        return e as unknown as ProjectAlmBindingResponse;
       }),
     staleTime: 60_000,
     enabled: projectKey !== null,
-    ...options,
   });
-}
+});
 
 export function useIsGitHubProjectQuery(project?: string) {
-  return useProjectBindingQuery<boolean>(project, {
+  return useProjectBindingQuery(project, {
     select: (data) => data?.alm === AlmKeys.GitHub,
   });
 }
index 1f1f41c406eb20a1b1a2d254f8694e76c38d3172..d5de4ccdf549debd1224675620fe8493897fc992 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-import { UseQueryOptions, useQuery } from '@tanstack/react-query';
+import { queryOptions } from '@tanstack/react-query';
 import { getSystemUpgrades } from '../api/system';
+import { createQueryHook } from './common';
 
-export function useSystemUpgrades<T = Awaited<ReturnType<typeof getSystemUpgrades>>>(
-  options?: Omit<
-    UseQueryOptions<Awaited<ReturnType<typeof getSystemUpgrades>>, Error, T>,
-    'queryKey' | 'queryFn'
-  >,
-) {
-  return useQuery({
+export const useSystemUpgrades = createQueryHook(() => {
+  return queryOptions({
     queryKey: ['system', 'upgrades'],
     queryFn: () => getSystemUpgrades(),
     staleTime: Infinity,
-    ...options,
   });
-}
+});