From: Viktor Vorona Date: Wed, 24 Jul 2024 10:11:41 +0000 (+0200) Subject: [NO JIRA] allow createQueryHook without params X-Git-Tag: 10.7.0.96327~311 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=89c2d1a6426234d4baa8b9a0e01e45342a3e35d8;p=sonarqube.git [NO JIRA] allow createQueryHook without params --- diff --git a/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx b/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx index 675966d6dc1..e5c692ba5cc 100644 --- a/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx @@ -56,7 +56,7 @@ export interface TutorialSelectionRendererProps { currentUser: LoggedInUser; currentUserCanScanProject: boolean; loading: boolean; - projectBinding?: ProjectAlmBindingResponse; + projectBinding?: ProjectAlmBindingResponse | null; selectedTutorial?: TutorialModes; willRefreshAutomatically?: boolean; } diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx index 4e1b4ac0f02..9655bc5fc29 100644 --- a/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx @@ -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]/ */ diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx index 44b74bd6623..504b7693884 100644 --- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx @@ -35,7 +35,7 @@ export interface WebhookStepProps { alm: AlmKeys; almBinding?: AlmSettingsInstance; branchesEnabled: boolean; - projectBinding?: ProjectAlmBindingResponse; + projectBinding?: ProjectAlmBindingResponse | null; } function renderAlmSpecificInstructions(props: WebhookStepProps) { diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx index cddf01e85d2..884e7fb04ba 100644 --- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx @@ -36,7 +36,7 @@ export interface WebhookStepBitbucketProps { alm: AlmKeys; almBinding?: AlmSettingsInstance; branchesEnabled: boolean; - projectBinding?: ProjectAlmBindingResponse; + projectBinding?: ProjectAlmBindingResponse | null; } function buildUrlSnippet( diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx index de5f5bace1a..9574d4c0bd6 100644 --- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx @@ -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) { diff --git a/server/sonar-web/src/main/js/queries/common.ts b/server/sonar-web/src/main/js/queries/common.ts index 9bf450ae683..fe56e4c8c38 100644 --- a/server/sonar-web/src/main/js/queries/common.ts +++ b/server/sonar-web/src/main/js/queries/common.ts @@ -29,23 +29,38 @@ import { useQuery, } from '@tanstack/react-query'; +const isFnWithoutParams = (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, -): ( - data: T, - options?: Omit< - UseQueryOptions, - 'queryKey' | 'queryFn' - >, -) => UseQueryResult; + fn: + | ((data: T) => UseQueryOptions) + | (() => UseQueryOptions), +): unknown extends T + ? ( + options?: Omit< + UseQueryOptions, + 'queryKey' | 'queryFn' + >, + ) => UseQueryResult + : ( + data: T, + options?: Omit< + UseQueryOptions, + 'queryKey' | 'queryFn' + >, + ) => UseQueryResult; -export function createQueryHook(fn: (data: any) => UseQueryOptions) { +export function createQueryHook(fn: ((data: any) => UseQueryOptions) | (() => UseQueryOptions)) { + if (isFnWithoutParams(fn)) { + return (options?: Omit) => + useQuery({ ...fn(), ...options }); + } return (data: any, options?: Omit) => useQuery({ ...fn(data), ...options }); } @@ -59,18 +74,58 @@ export function createInfiniteQueryHook< TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, >( - fn: ( - data: T, - ) => UseInfiniteQueryOptions, -): ( - data: T, - options?: Omit< - UseInfiniteQueryOptions, - 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam' - >, -) => UseInfiniteQueryResult; + fn: + | (( + data: T, + ) => UseInfiniteQueryOptions) + | (() => UseInfiniteQueryOptions< + TQueryFnData, + TError, + TData, + TQueryData, + TQueryKey, + TPageParam + >), +): unknown extends T + ? ( + options?: Omit< + UseInfiniteQueryOptions< + TQueryFnData, + TError, + SelectType, + TQueryData, + TQueryKey, + TPageParam + >, + 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam' + >, + ) => UseInfiniteQueryResult + : ( + data: T, + options?: Omit< + UseInfiniteQueryOptions< + TQueryFnData, + TError, + SelectType, + TQueryData, + TQueryKey, + TPageParam + >, + 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam' + >, + ) => UseInfiniteQueryResult; -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< diff --git a/server/sonar-web/src/main/js/queries/devops-integration.ts b/server/sonar-web/src/main/js/queries/devops-integration.ts index ff7a6478ea2..fc5824edfcb 100644 --- a/server/sonar-web/src/main/js/queries/devops-integration.ts +++ b/server/sonar-web/src/main/js/queries/devops-integration.ts @@ -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( - 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(project, { + return useProjectBindingQuery(project, { select: (data) => data?.alm === AlmKeys.GitHub, }); } diff --git a/server/sonar-web/src/main/js/queries/system.ts b/server/sonar-web/src/main/js/queries/system.ts index 1f1f41c406e..d5de4ccdf54 100644 --- a/server/sonar-web/src/main/js/queries/system.ts +++ b/server/sonar-web/src/main/js/queries/system.ts @@ -17,19 +17,14 @@ * 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>>( - options?: Omit< - UseQueryOptions>, Error, T>, - 'queryKey' | 'queryFn' - >, -) { - return useQuery({ +export const useSystemUpgrades = createQueryHook(() => { + return queryOptions({ queryKey: ['system', 'upgrades'], queryFn: () => getSystemUpgrades(), staleTime: Infinity, - ...options, }); -} +});