| 1 | "use client"; |
| 2 | |
| 3 | import { useState, type ReactNode } from "react"; |
| 4 | import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 5 | import { httpBatchLink } from "@trpc/client"; |
| 6 | import superjson from "superjson"; |
| 7 | import { trpc } from "./react"; |
| 8 | |
| 9 | function baseUrl() { |
| 10 | if (typeof window !== "undefined") return ""; |
| 11 | if (process.env.NEXT_PUBLIC_SITE_URL) return process.env.NEXT_PUBLIC_SITE_URL; |
| 12 | return "http://localhost:3000"; |
| 13 | } |
| 14 | |
| 15 | export function TRPCProvider({ children }: { children: ReactNode }) { |
| 16 | const [queryClient] = useState(() => new QueryClient()); |
| 17 | const [client] = useState(() => |
| 18 | trpc.createClient({ |
| 19 | links: [ |
| 20 | httpBatchLink({ |
| 21 | url: `${baseUrl()}/api/trpc`, |
| 22 | transformer: superjson, |
| 23 | }), |
| 24 | ], |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | return ( |
| 29 | <trpc.Provider client={client} queryClient={queryClient}> |
| 30 | <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 31 | </trpc.Provider> |
| 32 | ); |
| 33 | } |