|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { ThemeProvider } from '@primer/react' |
| 4 | +import { useEffect, useMemo, useState } from 'react' |
| 5 | + |
| 6 | +import { LocaleProvider } from '@/app/lib/locale-context' |
| 7 | +import { useDetectLocale } from '@/app/lib/use-detect-locale' |
| 8 | +import { useTheme } from '@/color-schemes/components/useTheme' |
| 9 | +import { initializeEvents } from '@/events/components/events' |
| 10 | +import { CTAPopoverProvider } from '@/frame/components/context/CTAContext' |
| 11 | +import { SharedUIContextProvider } from '@/frame/components/context/SharedUIContext' |
| 12 | +import { LanguagesContext, LanguagesContextT } from '@/languages/components/LanguagesContext' |
| 13 | +import { clientLanguages, type ClientLanguageCode } from '@/languages/lib/client-languages' |
| 14 | +import { MainContextProvider } from '@/app/components/MainContextProvider' |
| 15 | +import { createMinimalMainContext } from '@/app/lib/main-context-adapter' |
| 16 | +import type { AppRouterContext } from '@/app/lib/app-router-context' |
| 17 | + |
| 18 | +interface ClientLayoutProps { |
| 19 | + readonly children: React.ReactNode |
| 20 | + readonly appContext?: AppRouterContext |
| 21 | + readonly pageData?: { |
| 22 | + title?: string |
| 23 | + fullTitle?: string |
| 24 | + introPlainText?: string |
| 25 | + topics?: string[] |
| 26 | + documentType?: string |
| 27 | + type?: string |
| 28 | + hidden?: boolean |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export function ClientLayout({ children, appContext, pageData }: ClientLayoutProps): JSX.Element { |
| 33 | + const { theme } = useTheme() |
| 34 | + const locale: ClientLanguageCode = useDetectLocale() |
| 35 | + const [isInitialized, setIsInitialized] = useState(false) |
| 36 | + const [initializationError, setInitializationError] = useState<Error | null>(null) |
| 37 | + |
| 38 | + const languagesContext: LanguagesContextT = useMemo( |
| 39 | + () => ({ |
| 40 | + languages: clientLanguages, |
| 41 | + }), |
| 42 | + [], |
| 43 | + ) |
| 44 | + |
| 45 | + // Create MainContext-compatible data for App Router |
| 46 | + const mainContext = useMemo( |
| 47 | + () => createMinimalMainContext(pageData, appContext), |
| 48 | + [pageData, appContext], |
| 49 | + ) |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + const initializeTheme = async (): Promise<void> => { |
| 53 | + try { |
| 54 | + const html = document.documentElement |
| 55 | + |
| 56 | + if (theme.css?.colorMode) { |
| 57 | + html.setAttribute('data-color-mode', theme.css.colorMode) |
| 58 | + } |
| 59 | + |
| 60 | + if (theme.css?.darkTheme) { |
| 61 | + html.setAttribute('data-dark-theme', theme.css.darkTheme) |
| 62 | + } |
| 63 | + |
| 64 | + if (theme.css?.lightTheme) { |
| 65 | + html.setAttribute('data-light-theme', theme.css.lightTheme) |
| 66 | + } |
| 67 | + |
| 68 | + if (!isInitialized) { |
| 69 | + await initializeEvents() |
| 70 | + setIsInitialized(true) |
| 71 | + } |
| 72 | + } catch (error) { |
| 73 | + console.error('Failed to initialize theme:', error) |
| 74 | + setInitializationError(error as Error) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + initializeTheme() |
| 79 | + }, [theme, isInitialized]) |
| 80 | + |
| 81 | + if (initializationError) { |
| 82 | + return ( |
| 83 | + <div |
| 84 | + role="alert" |
| 85 | + className="min-h-screen flex items-center justify-center bg-canvas-default p-4" |
| 86 | + > |
| 87 | + <div className="max-w-md text-center"> |
| 88 | + <h2 className="text-xl font-semibold mb-4 text-danger-fg">Something went wrong</h2> |
| 89 | + <p className="text-fg-muted mb-4">Please try refreshing the page.</p> |
| 90 | + <button |
| 91 | + onClick={() => { |
| 92 | + setInitializationError(null) |
| 93 | + setIsInitialized(false) |
| 94 | + }} |
| 95 | + className="btn btn-primary" |
| 96 | + type="button" |
| 97 | + aria-label="Try again" |
| 98 | + > |
| 99 | + Try again |
| 100 | + </button> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + <LocaleProvider locale={locale}> |
| 108 | + <LanguagesContext.Provider value={languagesContext}> |
| 109 | + <MainContextProvider value={mainContext}> |
| 110 | + <ThemeProvider |
| 111 | + colorMode={theme.component.colorMode} |
| 112 | + dayScheme={theme.component.dayScheme} |
| 113 | + nightScheme={theme.component.nightScheme} |
| 114 | + preventSSRMismatch |
| 115 | + > |
| 116 | + <SharedUIContextProvider> |
| 117 | + <CTAPopoverProvider> |
| 118 | + <div className="min-h-screen flex flex-col">{children}</div> |
| 119 | + </CTAPopoverProvider> |
| 120 | + </SharedUIContextProvider> |
| 121 | + </ThemeProvider> |
| 122 | + </MainContextProvider> |
| 123 | + </LanguagesContext.Provider> |
| 124 | + </LocaleProvider> |
| 125 | + ) |
| 126 | +} |
0 commit comments