From e86f6a9cf0eb271abfc7cf53ec10ef372d52f0bd Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sun, 17 Mar 2024 12:58:59 +0000
Subject: fix(mobile): Fix premature rendering of the app if the setting is not
 loaded

---
 apps/mobile/lib/providers.tsx | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

(limited to 'apps/mobile/lib/providers.tsx')

diff --git a/apps/mobile/lib/providers.tsx b/apps/mobile/lib/providers.tsx
index 036e8ae2..38eaa99e 100644
--- a/apps/mobile/lib/providers.tsx
+++ b/apps/mobile/lib/providers.tsx
@@ -1,9 +1,11 @@
 import { useEffect, useState } from "react";
+import FullPageSpinner from "@/components/ui/FullPageSpinner";
 import { ToastProvider } from "@/components/ui/Toast";
 import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
 import { httpBatchLink } from "@trpc/client";
 import superjson from "superjson";
 
+import type { Settings } from "./settings";
 import useAppSettings, { getAppSettings } from "./settings";
 import { api } from "./trpc";
 
@@ -26,8 +28,13 @@ function getTRPCClient(address: string) {
   });
 }
 
-export function Providers({ children }: { children: React.ReactNode }) {
-  const { settings } = useAppSettings();
+function TrpcProvider({
+  children,
+  settings,
+}: {
+  settings: Settings;
+  children: React.ReactNode;
+}) {
   const [queryClient] = useState(() => new QueryClient());
 
   const [trpcClient, setTrpcClient] = useState<
@@ -50,3 +57,18 @@ export function Providers({ children }: { children: React.ReactNode }) {
     </api.Provider>
   );
 }
+
+export function Providers({ children }: { children: React.ReactNode }) {
+  const { settings, isLoading } = useAppSettings();
+
+  if (isLoading) {
+    // Don't render anything if the settings still hasn't been loaded
+    return <FullPageSpinner />;
+  }
+
+  return (
+    <TrpcProvider settings={settings}>
+      <ToastProvider>{children}</ToastProvider>
+    </TrpcProvider>
+  );
+}
-- 
cgit v1.2.3-70-g09d2

