Skip to content

Commit 56cde4f

Browse files
authored
chore: Migrate useSelfHostedCurrentUser to TS Query V5 (#3582)
1 parent ccf9ea6 commit 56cde4f

File tree

15 files changed

+176
-127
lines changed

15 files changed

+176
-127
lines changed

src/layouts/Header/components/AdminLink/AdminLink.test.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
2-
import { render, screen } from '@testing-library/react'
1+
import {
2+
QueryClientProvider as QueryClientProviderV5,
3+
QueryClient as QueryClientV5,
4+
} from '@tanstack/react-queryV5'
5+
import { render, screen, waitFor } from '@testing-library/react'
36
import { http, HttpResponse } from 'msw'
47
import { setupServer } from 'msw/node'
8+
import { Suspense } from 'react'
59
import { MemoryRouter, Route } from 'react-router-dom'
610

711
import AdminLink from './AdminLink'
812

13+
const queryClientV5 = new QueryClientV5({
14+
defaultOptions: { queries: { retry: false } },
15+
})
16+
917
const wrapper: ({
1018
initialEntries,
1119
}: {
1220
initialEntries?: string
1321
}) => React.FC<React.PropsWithChildren> =
1422
({ initialEntries = '/gh' }) =>
1523
({ children }) => (
16-
<QueryClientProvider client={queryClient}>
24+
<QueryClientProviderV5 client={queryClientV5}>
1725
<MemoryRouter initialEntries={[initialEntries]}>
1826
<Route path="/:provider" exact>
19-
{children}
27+
<Suspense fallback={<div>Loading</div>}>{children}</Suspense>
2028
</Route>
2129
</MemoryRouter>
22-
</QueryClientProvider>
30+
</QueryClientProviderV5>
2331
)
2432

25-
const queryClient = new QueryClient({
26-
defaultOptions: { queries: { retry: false } },
27-
})
28-
2933
const server = setupServer()
3034
beforeAll(() => {
3135
server.listen()
3236
})
3337

34-
beforeEach(() => {
38+
afterEach(() => {
39+
queryClientV5.clear()
3540
server.resetHandlers()
36-
queryClient.clear()
3741
})
3842

3943
afterAll(() => {
@@ -69,7 +73,7 @@ describe('AdminLink', () => {
6973
})
7074

7175
describe('user is not an admin', () => {
72-
it('renders nothing', () => {
76+
it('renders nothing', async () => {
7377
setup({
7478
activated: false,
7579
@@ -80,7 +84,7 @@ describe('AdminLink', () => {
8084
})
8185

8286
const { container } = render(<AdminLink />, { wrapper: wrapper({}) })
83-
expect(container).toBeEmptyDOMElement()
87+
await waitFor(() => expect(container).toBeEmptyDOMElement())
8488
})
8589
})
8690
})

src/layouts/Header/components/AdminLink/AdminLink.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { useSelfHostedCurrentUser } from 'services/selfHosted'
1+
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
2+
import { useParams } from 'react-router'
3+
4+
import { SelfHostedCurrentUserQueryOpts } from 'services/selfHosted/SelfHostedCurrentUserQueryOpts'
25
import A from 'ui/A'
36
import Icon from 'ui/Icon'
47

8+
interface URLParams {
9+
provider: string
10+
}
11+
512
function AdminLink() {
6-
const { data: user } = useSelfHostedCurrentUser()
13+
const { provider } = useParams<URLParams>()
14+
const { data: user } = useSuspenseQueryV5(
15+
SelfHostedCurrentUserQueryOpts({ provider })
16+
)
717

818
if (!user?.isAdmin) {
919
return null

src/pages/AccountSettings/tabs/Profile/ActivationBanner/ActivationBanner.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { useQueryClient } from '@tanstack/react-query'
21
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
32
import { useParams } from 'react-router'
43

5-
import { useSelfHostedCurrentUser } from 'services/selfHosted'
4+
import { SelfHostedCurrentUserQueryOpts } from 'services/selfHosted/SelfHostedCurrentUserQueryOpts'
65
import { SelfHostedSeatsConfigQueryOpts } from 'services/selfHosted/SelfHostedSeatsConfigQueryOpts'
76
import A from 'ui/A'
87
import Banner from 'ui/Banner'
@@ -40,19 +39,20 @@ function canChangeActivation({ seatConfig, currentUser }) {
4039

4140
function ActivationBanner() {
4241
const { provider } = useParams()
43-
const queryClient = useQueryClient()
44-
const { data: currentUser } = useSelfHostedCurrentUser()
42+
4543
const { data: seatConfig } = useSuspenseQueryV5(
4644
SelfHostedSeatsConfigQueryOpts({ provider })
4745
)
46+
const { data: currentUser } = useSuspenseQueryV5(
47+
SelfHostedCurrentUserQueryOpts({ provider })
48+
)
4849

4950
const { canChange, displaySeatMsg } = canChangeActivation({
5051
seatConfig,
5152
currentUser,
5253
})
5354

5455
const { mutate } = useSelfActivationMutation({
55-
queryClient,
5656
canChange,
5757
})
5858

src/pages/AccountSettings/tabs/Profile/ActivationBanner/useSelfActivationMutation.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { useMutation } from '@tanstack/react-query'
2-
import { useQueryClient as useQueryClientV5 } from '@tanstack/react-queryV5'
1+
import {
2+
useMutation as useMutationV5,
3+
useQueryClient as useQueryClientV5,
4+
} from '@tanstack/react-queryV5'
35
import { useParams } from 'react-router-dom'
46

7+
import { SelfHostedCurrentUserQueryOpts } from 'services/selfHosted/SelfHostedCurrentUserQueryOpts'
58
import { SelfHostedSeatsConfigQueryOpts } from 'services/selfHosted/SelfHostedSeatsConfigQueryOpts'
69
import Api from 'shared/api'
710

8-
export const useSelfActivationMutation = ({ queryClient, canChange }) => {
11+
export const useSelfActivationMutation = ({ canChange }) => {
912
const { provider } = useParams()
1013
const queryClientV5 = useQueryClientV5()
1114

12-
return useMutation({
15+
return useMutationV5({
1316
mutationFn: (activated) => {
1417
if (canChange) {
1518
return Api.patch({
@@ -20,49 +23,59 @@ export const useSelfActivationMutation = ({ queryClient, canChange }) => {
2023
}
2124
},
2225
onMutate: async (activated) => {
23-
await queryClient.cancelQueries(['SelfHostedCurrentUser'])
26+
// Cancel any in-flight queries
27+
await queryClientV5.cancelQueries({
28+
queryKey: SelfHostedCurrentUserQueryOpts({ provider }).queryKey,
29+
})
2430
await queryClientV5.cancelQueries({
2531
queryKey: SelfHostedSeatsConfigQueryOpts({ provider }).queryKey,
2632
})
2733

28-
const prevUser = queryClient.getQueryData(['SelfHostedCurrentUser'])
34+
// Get the current data before the mutation
2935
const prevSeat = queryClientV5.getQueryData(
3036
SelfHostedSeatsConfigQueryOpts({ provider }).queryKey
3137
)
38+
const prevUser = queryClientV5.getQueryData(
39+
SelfHostedCurrentUserQueryOpts({ provider }).queryKey
40+
)
3241

3342
if (canChange) {
34-
queryClient.setQueryData(['SelfHostedCurrentUser'], (user) => ({
35-
...user,
36-
activated,
37-
}))
43+
// Optimistically update the data in the query client
44+
queryClientV5.setQueryData(
45+
SelfHostedCurrentUserQueryOpts({ provider }).queryKey,
46+
(user) => ({ ...user, activated })
47+
)
3848

3949
queryClientV5.setQueryData(
4050
SelfHostedSeatsConfigQueryOpts({ provider }).queryKey,
4151
(seats) => {
42-
const seatsUsed = seats?.data?.config?.seatsUsed
43-
return {
44-
data: {
45-
config: {
46-
...seats?.data?.config,
47-
seatsUsed: activated ? seatsUsed + 1 : seatsUsed - 1,
48-
},
49-
},
50-
}
52+
const seatsUsed = activated
53+
? seats?.data?.config?.seatsUsed + 1
54+
: seats?.data?.config?.seatsUsed - 1
55+
56+
return { data: { config: { ...seats?.data?.config, seatsUsed } } }
5157
}
5258
)
5359
}
5460

5561
return { prevUser, prevSeat }
5662
},
5763
onError: (_err, _activated, context) => {
58-
queryClient.setQueryData(['SelfHostedCurrentUser'], context.prevUser)
64+
// Rollback the data if the mutation fails
65+
queryClientV5.setQueryData(
66+
SelfHostedCurrentUserQueryOpts({ provider }).queryKey,
67+
context.prevUser
68+
)
5969
queryClientV5.setQueryData(
6070
SelfHostedSeatsConfigQueryOpts({ provider }).queryKey,
6171
context.prevSeat
6272
)
6373
},
6474
onSettled: () => {
65-
queryClient.invalidateQueries(['SelfHostedCurrentUser'])
75+
// Invalidate the queries to ensure they are re-fetched
76+
queryClientV5.invalidateQueries({
77+
queryKey: SelfHostedCurrentUserQueryOpts({ provider }).queryKey,
78+
})
6679
queryClientV5.invalidateQueries(
6780
SelfHostedSeatsConfigQueryOpts({ provider }).queryKey
6881
)

0 commit comments

Comments
 (0)