Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Migrate useRepoConfigurationStatus to TS Query V5 #3618

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClientProvider as QueryClientProviderV5,
QueryClient as QueryClientV5,
} from '@tanstack/react-queryV5'
import { render, screen, waitFor } from '@testing-library/react'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
Expand All @@ -7,7 +11,7 @@ import { MemoryRouter, Route } from 'react-router'
import { TierNames, TTierNames } from 'services/tier'

import ConfigurationManager from './ConfigurationManager'
import { RepositoryConfiguration } from './hooks/useRepoConfigurationStatus/useRepoConfigurationStatus'
import { RepositoryConfiguration } from './hooks/useRepoConfigurationStatus/RepoConfigurationStatusQueryOpts'

interface mockRepoConfigArgs {
tierName?: TTierNames
Expand Down Expand Up @@ -52,26 +56,28 @@ function mockRepoConfig({
}

const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
defaultOptions: { queries: { retry: false } },
})
const queryClientV5 = new QueryClientV5({
defaultOptions: { queries: { retry: false } },
})
const server = setupServer()
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/gh/codecov/cool-repo/config']}>
<Route path="/:provider/:owner/:repo/config">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
<QueryClientProviderV5 client={queryClientV5}>
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/gh/codecov/cool-repo/config']}>
<Route path="/:provider/:owner/:repo/config">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
</QueryClientProviderV5>
Comment on lines +66 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested QueryClientProvider components can be simplified to avoid redundancy and improve readability.

Suggested change
<QueryClientProviderV5 client={queryClientV5}>
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/gh/codecov/cool-repo/config']}>
<Route path="/:provider/:owner/:repo/config">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
</QueryClientProviderV5>
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProviderV5 client={queryClientV5}>
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={'/gh/codecov/cool-repo/config'}>
<Route path="/:provider/:owner:/repo?extra=children]>
{children}
</Route>
</MemoryRouter>
</QueryClientProvider>
</QueryClientProviderV5>
);

)

beforeAll(() => {
server.listen()
})
afterEach(() => {
queryClient.clear()
queryClientV5.clear()
server.resetHandlers()
})
afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
import { useParams } from 'react-router'

import { TierNames } from 'services/tier'

import FeatureGroup from './components/FeatureGroup'
import FeatureItem from './components/FeatureItem/FeatureItem'
import {
RepoConfigurationStatusQueryOpts,
RepositoryConfiguration,
useRepoConfigurationStatus,
} from './hooks/useRepoConfigurationStatus/useRepoConfigurationStatus'
} from './hooks/useRepoConfigurationStatus/RepoConfigurationStatusQueryOpts'

interface URLParams {
provider: string
Expand All @@ -17,11 +18,13 @@ interface URLParams {

function ConfigurationManager() {
const { provider, owner, repo } = useParams<URLParams>()
const { data: repoConfiguration } = useRepoConfigurationStatus({
provider,
owner,
repo,
})
const { data: repoConfiguration } = useSuspenseQueryV5(
RepoConfigurationStatusQueryOpts({
provider,
owner,
repo,
})
)
Comment on lines +21 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useSuspenseQueryV5 call can be simplified by directly passing the arguments instead of wrapping them in RepoConfigurationStatusQueryOpts.

Suggested change
const { data: repoConfiguration } = useSuspenseQueryV5(
RepoConfigurationStatusQueryOpts({
provider,
owner,
repo,
})
)
const { data: repoConfiguration } = useSuspenseQueryV5(['name', { provider, owner, repo }]);


return (
<div className="flex flex-col gap-6 lg:w-3/4">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClientProvider as QueryClientProviderV5,
QueryClient as QueryClientV5,
useQuery as useQueryV5,
} from '@tanstack/react-queryV5'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

import { useRepoConfigurationStatus } from './useRepoConfigurationStatus'
import { RepoConfigurationStatusQueryOpts } from './RepoConfigurationStatusQueryOpts'

const mockRepoNotFound = {
owner: {
Expand Down Expand Up @@ -49,20 +53,22 @@ const mockGoodResponse = {
},
}

const queryClient = new QueryClient({
const queryClientV5 = new QueryClientV5({
defaultOptions: { queries: { retry: false } },
})
const server = setupServer()

const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
<QueryClientProviderV5 client={queryClientV5}>
{children}
</QueryClientProviderV5>
)

beforeAll(() => {
server.listen()
})
afterEach(() => {
queryClient.clear()
queryClientV5.clear()
server.resetHandlers()
})
afterAll(() => {
Expand Down Expand Up @@ -104,11 +110,13 @@ describe('useRepoConfigurationStatus', () => {
console.error = () => {}
const { result } = renderHook(
() =>
useRepoConfigurationStatus({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
}),
useQueryV5(
RepoConfigurationStatusQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
})
),
{ wrapper }
)

Expand All @@ -128,11 +136,13 @@ describe('useRepoConfigurationStatus', () => {
console.error = () => {}
const { result } = renderHook(
() =>
useRepoConfigurationStatus({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
}),
useQueryV5(
RepoConfigurationStatusQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
})
),
{ wrapper }
)

Expand All @@ -152,11 +162,13 @@ describe('useRepoConfigurationStatus', () => {
console.error = () => {}
const { result } = renderHook(
() =>
useRepoConfigurationStatus({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
}),
useQueryV5(
RepoConfigurationStatusQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
})
),
{ wrapper }
)

Expand All @@ -175,11 +187,13 @@ describe('useRepoConfigurationStatus', () => {
setup({ nullOwner: true })
const { result } = renderHook(
() =>
useRepoConfigurationStatus({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
}),
useQueryV5(
RepoConfigurationStatusQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
})
),
{ wrapper }
)

Expand All @@ -197,11 +211,13 @@ describe('useRepoConfigurationStatus', () => {
setup({})
const { result } = renderHook(
() =>
useRepoConfigurationStatus({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
}),
useQueryV5(
RepoConfigurationStatusQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
})
),
{ wrapper }
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@tanstack/react-query'
import { queryOptions as queryOptionsV5 } from '@tanstack/react-queryV5'
import { z } from 'zod'

import {
Expand All @@ -7,7 +7,7 @@ import {
} from 'services/repo'
import { TierNames } from 'services/tier'
import Api from 'shared/api/api'
import { NetworkErrorObject } from 'shared/api/helpers'
import { rejectNetworkError } from 'shared/api/helpers'
import A from 'ui/A'

const RepositorySchema = z.object({
Expand Down Expand Up @@ -51,7 +51,8 @@ const RequestSchema = z.object({
.nullable(),
})

const query = `query GetRepoConfigurationStatus($owner: String!, $repo: String!){
const query = `
query GetRepoConfigurationStatus($owner: String!, $repo: String!) {
owner(username: $owner) {
plan {
tierName
Expand Down Expand Up @@ -79,18 +80,18 @@ const query = `query GetRepoConfigurationStatus($owner: String!, $repo: String!)
}
}`

interface UseRepoConfigurationStatusArgs {
interface RepoConfigurationStatusQueryArgs {
provider: string
owner: string
repo: string
}

export function useRepoConfigurationStatus({
export function RepoConfigurationStatusQueryOpts({
provider,
owner,
repo,
}: UseRepoConfigurationStatusArgs) {
return useQuery({
}: RepoConfigurationStatusQueryArgs) {
return queryOptionsV5({
queryKey: ['GetRepoConfigurationStatus', provider, owner, repo],
queryFn: ({ signal }) => {
return Api.graphql({
Expand All @@ -105,25 +106,26 @@ export function useRepoConfigurationStatus({
const parsedRes = RequestSchema.safeParse(res?.data)

if (!parsedRes.success) {
return Promise.reject({
return rejectNetworkError({
status: 404,
data: {},
dev: 'useRepoConfigurationStatus - 404 Failed to parse data',
} satisfies NetworkErrorObject)
error: parsedRes.error,
})
}

const data = parsedRes.data

if (data?.owner?.repository?.__typename === 'NotFoundError') {
return Promise.reject({
return rejectNetworkError({
status: 404,
data: {},
dev: 'useRepoConfigurationStatus - 404 Not found error',
} satisfies NetworkErrorObject)
})
}

if (data?.owner?.repository?.__typename === 'OwnerNotActivatedError') {
return Promise.reject({
return rejectNetworkError({
status: 403,
data: {
detail: (
Expand All @@ -136,7 +138,7 @@ export function useRepoConfigurationStatus({
),
},
dev: 'useRepoConfigurationStatus - 403 Owner not activated error',
} satisfies NetworkErrorObject)
})
}

return {
Expand Down
Loading