origin/apps/web/src/providers/query-provider.tsx

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-07-22 09:57:25 +08:00
import { QueryClient } from '@tanstack/react-query';
2024-07-11 11:00:51 +08:00
import { unstable_httpBatchStreamLink, loggerLink } from '@trpc/client';
import React, { useState } from 'react';
import { api } from '../utils/trpc';
import superjson from 'superjson';
2024-07-22 09:57:25 +08:00
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
import { createIDBPersister } from '../utils/idb';
2024-07-11 11:00:51 +08:00
export default function QueryProvider({ children }: { children: React.ReactNode }) {
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() =>
api.createClient({
links: [
unstable_httpBatchStreamLink({
url: 'http://localhost:3000/trpc',
// You can pass any HTTP headers you wish here
async headers() {
return {
// authorization: getAuthCookie(),
};
},
transformer: superjson
}),
loggerLink({
enabled: (opts) =>
(process.env.NODE_ENV === 'development' &&
typeof window !== 'undefined') ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
],
}),
);
return (
<api.Provider client={trpcClient} queryClient={queryClient}>
2024-07-22 09:57:25 +08:00
<PersistQueryClientProvider client={queryClient} persistOptions={{ persister: createIDBPersister() }}>
2024-07-11 11:00:51 +08:00
{children}
2024-07-22 09:57:25 +08:00
</PersistQueryClientProvider>
2024-07-11 11:00:51 +08:00
</api.Provider>
);
}