import { useQueryClient } from '@tanstack/react-query' import { useRouter } from '@tanstack/react-router' import { PropsWithChildren, useCallback } from 'react' import PrefetchableLink, { PrefetchableLinkProps } from './PrefetchableLink' import { prefetchProjectDetail } from '@/data/projects/project-detail-query' export function usePrefetchProjectIndexPage() { const router = useRouter() const queryClient = useQueryClient() return useCallback( ({ projectRef }: { projectRef?: string }) => { // Prefetch code if (projectRef) { void router.preloadRoute({ to: '/project/$ref', params: { ref: projectRef } }) } // Prefetch data prefetchProjectDetail(queryClient, { ref: projectRef, }).catch(() => { // eat prefetching errors as they are not critical }) }, [queryClient, router] ) } interface ProjectIndexPageLinkProps extends Omit { projectRef?: string href?: PrefetchableLinkProps['href'] } export function ProjectIndexPageLink({ href, projectRef, children, ...props }: PropsWithChildren) { const prefetch = usePrefetchProjectIndexPage() return ( prefetch({ projectRef })} {...props} > {children} ) }