-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Single responsibility for supabase-context
- Loading branch information
Showing
4 changed files
with
47 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,59 @@ | ||
'use client' | ||
|
||
import {createBrowserSupabaseClient} from '@supabase/auth-helpers-nextjs' | ||
import {QueryClientProvider, useQueryClient} from '@tanstack/react-query' | ||
// import {TRPCProvider} from '@usevenice/engine-frontend' | ||
import type {Viewer} from '@usevenice/cdk-core' | ||
import {QueryClientProvider} from '@tanstack/react-query' | ||
import {zViewerFromUnverifiedJwtToken} from '@usevenice/cdk-core' | ||
import {TRPCProvider} from '@usevenice/engine-frontend' | ||
import React from 'react' | ||
import { | ||
SupabaseViewerProvider, | ||
SupabaseProvider, | ||
useSupabaseContext, | ||
} from '../../contexts/SupabaseViewerProvider' | ||
} from '../../contexts/supabase-context' | ||
import {ViewerContext} from '../../contexts/viewer-context' | ||
import {createQueryClient} from '../../lib/query-client' | ||
import type {Database} from '../../supabase/supabase.gen' | ||
import {useViewerContext} from '../../contexts/viewer-context' | ||
import type {Database as DB} from '../../supabase/supabase.gen' | ||
|
||
export function AdminLayout({ | ||
children, | ||
initialViewer, | ||
}: { | ||
export function AdminLayout(props: { | ||
children: React.ReactNode | ||
initialViewer: Viewer & {accessToken?: string | null} | ||
/** Viewer will be inferred from this... */ | ||
initialAccessToken: string | null | undefined | ||
}) { | ||
console.log('[AdminLayout] rendering', initialViewer) | ||
const {current: queryClient} = React.useRef(createQueryClient()) | ||
const {current: supabase} = React.useRef( | ||
createBrowserSupabaseClient<Database>({}), | ||
) | ||
console.log('[AdminLayout] rendering', props.initialAccessToken) | ||
const {current: supabase} = React.useRef(createBrowserSupabaseClient<DB>({})) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
;(globalThis as any).supabase = supabase | ||
;(globalThis as any).queryClient = queryClient | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<SupabaseViewerProvider supabase={supabase} initialViewer={initialViewer}> | ||
<_TRPCProvider>{children}</_TRPCProvider> | ||
</SupabaseViewerProvider> | ||
</QueryClientProvider> | ||
<SupabaseProvider supabase={supabase}> | ||
<AdminLayoutInner {...props} /> | ||
</SupabaseProvider> | ||
) | ||
} | ||
|
||
function _TRPCProvider({children}: {children: React.ReactNode}) { | ||
const queryClient = useQueryClient() | ||
const {accessToken} = useViewerContext() | ||
/** Separate Inner component is needed in order to useSupabaseContext */ | ||
function AdminLayoutInner({ | ||
children, | ||
initialAccessToken, | ||
}: React.ComponentProps<typeof AdminLayout>) { | ||
const {session, status, error} = useSupabaseContext() | ||
const accessToken = | ||
status === 'initial' || status === 'loading' | ||
? initialAccessToken | ||
: session?.access_token | ||
const viewer = zViewerFromUnverifiedJwtToken.parse(accessToken) | ||
|
||
// NOTE: Should change queryClient when authenticated identity changes to reset all trpc cache | ||
const {current: queryClient} = React.useRef(createQueryClient()) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
;(globalThis as any).queryClient = queryClient | ||
|
||
return ( | ||
<TRPCProvider queryClient={queryClient} accessToken={accessToken}> | ||
{children} | ||
</TRPCProvider> | ||
<QueryClientProvider client={queryClient}> | ||
<TRPCProvider queryClient={queryClient} accessToken={accessToken}> | ||
<ViewerContext.Provider value={{error, status, viewer, accessToken}}> | ||
{children} | ||
</ViewerContext.Provider> | ||
</TRPCProvider> | ||
</QueryClientProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters