aboutsummaryrefslogtreecommitdiffstats
path: root/packages/mobile/lib/providers.tsx
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-03-11 11:05:36 +0000
committerMohamedBassem <me@mbassem.com>2024-03-11 11:05:36 +0000
commit999ed977a588b2c3b2055f18db4218d77882a1a1 (patch)
treebe926d82bcd495878956483ee7b21a172666566a /packages/mobile/lib/providers.tsx
parente774f1ad01d4755651a82ed100c26587774c8ee0 (diff)
downloadkarakeep-999ed977a588b2c3b2055f18db4218d77882a1a1.tar.zst
mobile: Add support for app settings
Diffstat (limited to 'packages/mobile/lib/providers.tsx')
-rw-r--r--packages/mobile/lib/providers.tsx47
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/mobile/lib/providers.tsx b/packages/mobile/lib/providers.tsx
new file mode 100644
index 00000000..d5638da8
--- /dev/null
+++ b/packages/mobile/lib/providers.tsx
@@ -0,0 +1,47 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { httpBatchLink } from "@trpc/client";
+import { useEffect, useState } from "react";
+import superjson from "superjson";
+
+import useAppSettings, { getAppSettings } from "./settings";
+import { api } from "./trpc";
+
+function getTRPCClient(address: string) {
+ return api.createClient({
+ links: [
+ httpBatchLink({
+ url: `${address}/api/trpc`,
+ async headers() {
+ const settings = await getAppSettings();
+ return {
+ Authorization: settings ? `Bearer ${settings.apiKey}` : undefined,
+ };
+ },
+ transformer: superjson,
+ }),
+ ],
+ });
+}
+
+export function Providers({ children }: { children: React.ReactNode }) {
+ const { settings } = useAppSettings();
+ const [queryClient] = useState(() => new QueryClient());
+
+ const [trpcClient, setTrpcClient] = useState<
+ ReturnType<typeof getTRPCClient>
+ >(getTRPCClient(settings.address));
+
+ useEffect(() => {
+ setTrpcClient(getTRPCClient(settings.address));
+ }, [settings.address]);
+
+ return (
+ <api.Provider
+ key={settings.address}
+ client={trpcClient}
+ queryClient={queryClient}
+ >
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ </api.Provider>
+ );
+}