Skip to content

Commit fca275c

Browse files
committed
Update flow for prefetches
1 parent e1be2e4 commit fca275c

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

app/pages/project/affinity/AffinityPage.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright Oxide Computer Company
77
*/
88

9+
import { useQuery } from '@tanstack/react-query'
910
import { createColumnHelper, getCoreRowModel, useReactTable } from '@tanstack/react-table'
1011
import { useMemo } from 'react'
1112
import { Link, type LoaderFunctionArgs } from 'react-router'
@@ -14,7 +15,6 @@ import {
1415
apiq,
1516
getListQFn,
1617
queryClient,
17-
useApiQuery,
1818
usePrefetchedQuery,
1919
type AffinityPolicy,
2020
type AntiAffinityGroup,
@@ -47,16 +47,16 @@ const memberList = ({ antiAffinityGroup, project }: PP.AntiAffinityGroup) =>
4747

4848
export async function clientLoader({ params }: LoaderFunctionArgs) {
4949
const { project } = getProjectSelector(params)
50-
await queryClient
51-
.fetchQuery(antiAffinityGroupList({ project }).optionsFn())
52-
.then((data) => {
53-
// Preload the anti-affinity group details
54-
data.items.forEach((antiAffinityGroup) => {
55-
queryClient.fetchQuery(
56-
memberList({ antiAffinityGroup: antiAffinityGroup.name, project })
57-
)
58-
})
59-
})
50+
const groups = await queryClient.fetchQuery(
51+
antiAffinityGroupList({ project }).optionsFn()
52+
)
53+
const memberFetches = groups.items.map(({ name }) =>
54+
queryClient.prefetchQuery(memberList({ antiAffinityGroup: name, project }))
55+
)
56+
// The browser will fetch up to 6 anti-affinity group member lists without queuing,
57+
// so we can prefetch them without slowing down the page. If there are more than 6 groups,
58+
// we won't bother to wait for the promises to fulfill, and will just load the actual page content.
59+
if (groups.items.length < 6) await Promise.all(memberFetches)
6060
return null
6161
}
6262

@@ -143,10 +143,7 @@ export const AffinityGroupMembersCell = ({
143143
antiAffinityGroup: string
144144
}) => {
145145
const { project } = useProjectSelector()
146-
const { data: members } = useApiQuery('antiAffinityGroupMemberList', {
147-
path: { antiAffinityGroup },
148-
query: { project, limit: 2 },
149-
})
146+
const { data: members } = useQuery(memberList({ antiAffinityGroup, project }))
150147

151148
if (!members) return <SkeletonCell />
152149
if (!members.items.length) return <EmptyCell />

0 commit comments

Comments
 (0)