hellth-hub/apps/admin/app/_components/providers.tsx

68 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

"use client";
import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { httpBatchLink } from "@trpc/client";
import { useState } from "react";
import superjson from "superjson";
import { trpc } from "@/lib/trpc";
import { ThemeProvider } from "./theme-provider";
function resolveApiUrl() {
const configured = process.env.NEXT_PUBLIC_API_URL;
if (configured) return configured;
if (process.env.NODE_ENV === "production") {
throw new Error("NEXT_PUBLIC_API_URL muss in Produktion gesetzt sein");
}
return "http://localhost:3002";
}
const API_URL = resolveApiUrl();
export function Providers({ children }: { children: React.ReactNode }) {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: { staleTime: 30_000, refetchOnWindowFocus: false },
},
})
);
const [trpcClient] = useState(() =>
trpc.createClient({
links: [
httpBatchLink({
url: `${API_URL}/api/trpc`,
transformer: superjson,
fetch: (url, opts) => {
const headers = new Headers(opts?.headers ?? {});
return fetch(url, {
...opts,
credentials: "include",
headers,
});
},
}),
],
})
);
return (
<MantineProvider
defaultColorScheme="light"
theme={{
primaryColor: "teal",
fontFamily: "Inter, system-ui, -apple-system, sans-serif",
}}
>
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>
<ThemeProvider>{children}</ThemeProvider>
</QueryClientProvider>
</trpc.Provider>
</MantineProvider>
);
}