Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 164 additions & 7 deletions apps/editor/app/client-bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,166 @@
// idempotent under HMR.
import '../lib/bootstrap'
import { createI18n, defaultI18n, PascalI18nProvider } from '@pascal-app/core'
import { registerBundledLocales } from '@pascal-app/editor/locales'
import { type ReactNode, useEffect, useMemo } from 'react'
import {
isBundledLocaleLoaded,
type LocaleCatalog,
loadBundledLocale,
registerBundledCatalog,
} from '@pascal-app/editor/locales/lazy'
import { useSearchParams } from 'next/navigation'
import {
type ReactNode,
Suspense,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
} from 'react'
import { registerDevLocales } from '@/lib/dev-locales'
import {
createLocaleApplier,
type LocaleApplyResult,
resolveClientLangNavigation,
} from '@/lib/ui-locale'

registerBundledLocales()
registerDevLocales()

const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect

function LocaleQuerySync({
defaultLocale,
onCancelPending,
onLocaleChange,
onRetryPending,
}: {
defaultLocale: string
onCancelPending: () => void
onLocaleChange: (nextLocale: string) => Promise<LocaleApplyResult>
onRetryPending: () => Promise<{ result: LocaleApplyResult | 'none'; source?: string }>
}) {
const searchParams = useSearchParams()
const langParam = searchParams.get('lang')
const committedLangRef = useRef<string | null | undefined>(undefined)
const latestLangRef = useRef<string | null>(langParam)
const hasUncommittedQueryRef = useRef(false)
const lastApplierRef = useRef(onLocaleChange)

useIsomorphicLayoutEffect(() => {
latestLangRef.current = langParam
}, [langParam])

useEffect(() => {
if (lastApplierRef.current !== onLocaleChange) {
lastApplierRef.current = onLocaleChange
committedLangRef.current = undefined
hasUncommittedQueryRef.current = false
}
const previousLang = committedLangRef.current
const nextLocale = resolveClientLangNavigation(previousLang, langParam, defaultLocale)
if (nextLocale === null) {
if (hasUncommittedQueryRef.current) {
hasUncommittedQueryRef.current = false
onCancelPending()
}
committedLangRef.current = langParam
return
}
hasUncommittedQueryRef.current = true
let cancelled = false
void onLocaleChange(nextLocale).then((result) => {
if (cancelled || latestLangRef.current !== langParam) return
if (result === 'applied') {
hasUncommittedQueryRef.current = false
committedLangRef.current = langParam
}
})
return () => {
cancelled = true
}
}, [langParam, defaultLocale, onLocaleChange, onCancelPending])

useEffect(() => {
const retry = () => {
void onRetryPending().then(({ result, source }) => {
if (result === 'applied' && source === 'query') {
hasUncommittedQueryRef.current = false
committedLangRef.current = latestLangRef.current
}
})
}
window.addEventListener('online', retry)
window.addEventListener('focus', retry)
return () => {
window.removeEventListener('online', retry)
window.removeEventListener('focus', retry)
}
}, [onRetryPending])

return null
}

export function ClientBootstrap({
children,
enableDevDiagnostics,
initialCatalog = null,
locale,
}: {
children: ReactNode
enableDevDiagnostics: boolean
initialCatalog?: LocaleCatalog | null
locale: string
}) {
const i18n = useMemo(() => createI18n({ locale }), [locale])
const initialCatalogRef = useRef(initialCatalog)
initialCatalogRef.current = initialCatalog

const i18n = useMemo(() => {
if (initialCatalogRef.current) registerBundledCatalog(initialCatalogRef.current)
return createI18n({ locale })
}, [locale])

const activeI18nRef = useRef<typeof i18n | null>(null)

useIsomorphicLayoutEffect(() => {
activeI18nRef.current = i18n
return () => {
if (activeI18nRef.current === i18n) {
activeI18nRef.current = null
}
}
}, [i18n])

const localeApplier = useMemo(
() =>
createLocaleApplier({
isLoaded: isBundledLocaleLoaded,
isActive: () => activeI18nRef.current === i18n,
loadLocale: loadBundledLocale,
setLocale: (nextLocale) => i18n.setLocale(nextLocale),
}),
[i18n],
)

const applyQueryLocale = useCallback(
(nextLocale: string) => localeApplier.apply(nextLocale, 'query'),
[localeApplier],
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

const applyImperativeLocale = useCallback(
(nextLocale: string) => localeApplier.apply(nextLocale, 'imperative'),
[localeApplier],
)

const cancelPendingQueryLocale = useCallback(
() => localeApplier.cancelPending('query'),
[localeApplier],
)

const retryPendingLocale = useCallback(async () => {
const source = localeApplier.getFailedSource()
const result = await localeApplier.retryPending()
return { result, source }
}, [localeApplier])

useEffect(() => {
if (!enableDevDiagnostics) return
Expand All @@ -42,8 +185,22 @@ export function ClientBootstrap({
}, [i18n])

useEffect(() => {
return defaultI18n.subscribeLocale((nextLocale) => i18n.setLocale(nextLocale))
}, [i18n])
return defaultI18n.subscribeLocale((nextLocale) => {
void applyImperativeLocale(nextLocale)
})
}, [applyImperativeLocale])

return <PascalI18nProvider i18n={i18n}>{children}</PascalI18nProvider>
return (
<PascalI18nProvider i18n={i18n}>
<Suspense fallback={null}>
<LocaleQuerySync
defaultLocale={locale}
onCancelPending={cancelPendingQueryLocale}
onLocaleChange={applyQueryLocale}
onRetryPending={retryPendingLocale}
/>
</Suspense>
{children}
</PascalI18nProvider>
)
}
10 changes: 7 additions & 3 deletions apps/editor/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerBundledLocales } from '@pascal-app/editor/locales'
import { loadBundledLocale } from '@pascal-app/editor/locales/lazy'
import { Agentation } from 'agentation'
import { GeistPixelSquare } from 'geist/font/pixel'
import { Barlow, Inter } from 'next/font/google'
Expand All @@ -9,7 +9,6 @@ import { PASCAL_LOCALE_HEADER, resolveUiLocale } from '@/lib/ui-locale'
import { ClientBootstrap } from './client-bootstrap'
import './globals.css'

registerBundledLocales()
registerDevLocales()

const geistSans = localFont({
Expand Down Expand Up @@ -42,14 +41,19 @@ export default async function RootLayout({
const enableDevDiagnostics =
process.env.NODE_ENV === 'development' && process.env.PASCAL_DEV_DIAGNOSTICS === '1'
const locale = resolveUiLocale((await headers()).get(PASCAL_LOCALE_HEADER))
const initialCatalog = await loadBundledLocale(locale)

return (
<html
className={`${geistSans.variable} ${geistMono.variable} ${GeistPixelSquare.variable} ${barlow.variable} ${inter.variable}`}
lang={locale}
>
<body className="font-sans">
<ClientBootstrap enableDevDiagnostics={enableDevDiagnostics} locale={locale}>
<ClientBootstrap
enableDevDiagnostics={enableDevDiagnostics}
initialCatalog={initialCatalog}
locale={locale}
>
{children}
</ClientBootstrap>
{enableDevDiagnostics && <Agentation />}
Expand Down
Loading
Loading