11"use client" ;
22
3- import { useAuth } from "@clerk/nextjs" ;
43import { QueryClient , QueryClientProvider } from "@tanstack/react-query" ;
54import dynamic from "next/dynamic" ;
65import { ThemeProvider } from "next-themes" ;
76import { NuqsAdapter } from "nuqs/adapters/next/app" ;
87import type { ReactNode } from "react" ;
9- import { useEffect , useLayoutEffect , useState } from "react" ;
8+ import { use , useEffect , useLayoutEffect , useState } from "react" ;
109import { Toaster } from "sonner" ;
1110import { useAppStore } from "@/lib/store/app-store" ;
1211import { useChatTabsStore } from "@/lib/store/chat-tabs-store" ;
@@ -17,22 +16,39 @@ const CommandPalette = dynamic(
1716 { ssr : false } ,
1817) ;
1918
20- export function Providers ( { children } : { children : ReactNode } ) {
19+ interface RootSession {
20+ orgId : null | string ;
21+ userId : null | string ;
22+ }
23+
24+ export function Providers ( {
25+ children,
26+ sessionPromise,
27+ } : {
28+ children : ReactNode ;
29+ sessionPromise : Promise < RootSession > ;
30+ } ) {
2131 return (
2232 < NuqsAdapter >
2333 < ThemeProvider attribute = "class" defaultTheme = "light" disableTransitionOnChange enableSystem >
24- < IdentityQueryProvider > { children } </ IdentityQueryProvider >
34+ < IdentityQueryProvider sessionPromise = { sessionPromise } > { children } </ IdentityQueryProvider >
2535 </ ThemeProvider >
2636 </ NuqsAdapter >
2737 ) ;
2838}
2939
30- function IdentityQueryProvider ( { children } : { children : ReactNode } ) {
31- const { isLoaded, orgId, userId } = useAuth ( ) ;
32- const identity = isLoaded ? `${ userId ?? "anonymous" } :${ orgId ?? "personal" } ` : "loading" ;
40+ function IdentityQueryProvider ( {
41+ children,
42+ sessionPromise,
43+ } : {
44+ children : ReactNode ;
45+ sessionPromise : Promise < RootSession > ;
46+ } ) {
47+ const { orgId, userId } = use ( sessionPromise ) ;
48+ const identity = `${ userId ?? "anonymous" } :${ orgId ?? "personal" } ` ;
3349
3450 return (
35- < IdentityQueryBoundary key = { identity } showCommandPalette = { Boolean ( isLoaded && userId ) } >
51+ < IdentityQueryBoundary key = { identity } showCommandPalette = { Boolean ( userId ) } >
3652 { children }
3753 </ IdentityQueryBoundary >
3854 ) ;
0 commit comments