'use client' import { type QueryClient, QueryClientProvider } from '@tanstack/react-query' import { httpBatchStreamLink, loggerLink } from '@trpc/client' import { createTRPCReact } from '@trpc/react-query' import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server' import type { ReactNode } from 'react' import { useState } from 'react' import SuperJSON from 'superjson' import type { AppRouter } from '~/server/api/root' import { createQueryClient } from './query-client' let clientQueryClientSingleton: QueryClient | undefined const getQueryClient = () => { if (typeof window === 'undefined') { return createQueryClient() } clientQueryClientSingleton ??= createQueryClient() return clientQueryClientSingleton } export const api = createTRPCReact() export type RouterInputs = inferRouterInputs export type RouterOutputs = inferRouterOutputs function getBaseUrl() { if (typeof window !== 'undefined') return window.location.origin if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}` return `http://localhost:${process.env.PORT ?? 3000}` } export function TRPCReactProvider(props: { children: ReactNode }) { const queryClient = getQueryClient() const [trpcClient] = useState(() => api.createClient({ links: [ loggerLink({ enabled: (op) => op.direction === 'down' && op.result instanceof Error, }), httpBatchStreamLink({ transformer: SuperJSON, url: `${getBaseUrl()}/api/trpc`, }), ], }), ) return ( {props.children} ) }