Skip to content
Open
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
51 changes: 36 additions & 15 deletions apps/editor/app/client-bootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
'use client'

// Loads `@pascal-app/nodes`' built-in plugin into the node registry on the
// client. Mounted from `layout.tsx` so every page in the standalone
// editor gets the registry populated before its first `<Viewer>` /
// `<Editor>` mounts — without this the registry is empty on the client
// (the server registers in its own module instance, which is unreachable
// from hydrated pages) and every `NodeRenderer` resolves to `null`. The
// `loaded` guard inside `../lib/bootstrap` keeps the side effect
// idempotent under HMR.
import '../lib/bootstrap'
import { createI18n, defaultI18n, PascalI18nProvider } from '@pascal-app/core'
import { registerBundledLocales } from '@pascal-app/editor/locales'
import {
type CompiledTranslationMessages,
createI18n,
defaultI18n,
PascalI18nProvider,
registerCompiledMessages,
} from '@pascal-app/core'
import { loadBundledLocale, registerEnglishLocale } from '@pascal-app/editor/locales/lazy'
import { type ReactNode, useEffect, useMemo } from 'react'
import { registerDevLocales } from '@/lib/dev-locales'
import { loadDevLocale } from '@/lib/dev-locales'

registerBundledLocales()
registerDevLocales()
registerEnglishLocale()

const registeredInitialLocales = new Set<string>()

export function ClientBootstrap({
children,
enableDevDiagnostics,
initialLocaleCatalog,
locale,
}: {
children: ReactNode
enableDevDiagnostics: boolean
initialLocaleCatalog?: { locale: string; messages: CompiledTranslationMessages }
locale: string
}) {
if (initialLocaleCatalog && !registeredInitialLocales.has(initialLocaleCatalog.locale)) {
registeredInitialLocales.add(initialLocaleCatalog.locale)
registerCompiledMessages(initialLocaleCatalog.locale, '', initialLocaleCatalog.messages)
}

const i18n = useMemo(() => createI18n({ locale }), [locale])

useEffect(() => {
Expand All @@ -42,7 +47,23 @@ export function ClientBootstrap({
}, [i18n])

useEffect(() => {
return defaultI18n.subscribeLocale((nextLocale) => i18n.setLocale(nextLocale))
let cancelled = false
let latestRequestId = 0
const unsubscribe = defaultI18n.subscribeLocale((nextLocale) => {
const requestId = ++latestRequestId
Promise.all([loadDevLocale(nextLocale), loadBundledLocale(nextLocale)])
.then(() => {
if (cancelled || requestId !== latestRequestId) return
i18n.setLocale(nextLocale)
})
.catch(() => {
// Keep the active locale when catalog loading fails.
})
})
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return () => {
cancelled = true
unsubscribe()
}
}, [i18n])

return <PascalI18nProvider i18n={i18n}>{children}</PascalI18nProvider>
Expand Down
1 change: 1 addition & 0 deletions apps/editor/app/import/import-client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import '@/lib/bootstrap'
import { type ValidateBuildJsonResult, validateBuildJson } from '@pascal-app/core'
import { useRouter } from 'next/navigation'
import { useCallback, useEffect, useRef, useState } from 'react'
Expand Down
17 changes: 12 additions & 5 deletions apps/editor/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { registerBundledLocales } from '@pascal-app/editor/locales'
import { loadBundledLocale, registerEnglishLocale } from '@pascal-app/editor/locales/lazy'
import { Agentation } from 'agentation'
import { GeistPixelSquare } from 'geist/font/pixel'
import { Barlow } from 'next/font/google'
import localFont from 'next/font/local'
import { headers } from 'next/headers'
import { registerDevLocales } from '@/lib/dev-locales'
import { loadDevLocale } from '@/lib/dev-locales'
import { PASCAL_LOCALE_HEADER, resolveUiLocale } from '@/lib/ui-locale'
import { ClientBootstrap } from './client-bootstrap'
import './globals.css'

registerBundledLocales()
registerDevLocales()
registerEnglishLocale()

const geistSans = localFont({
src: './fonts/GeistVF.woff',
Expand All @@ -36,14 +35,22 @@ 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 loadedCatalog =
locale === 'en' ? null : ((await loadDevLocale(locale)) ?? (await loadBundledLocale(locale)))
const initialLocaleCatalog =
loadedCatalog && loadedCatalog.locale !== 'en' ? loadedCatalog : undefined

return (
<html
className={`${geistSans.variable} ${geistMono.variable} ${GeistPixelSquare.variable} ${barlow.variable}`}
lang={locale}
>
<body className="font-sans">
<ClientBootstrap enableDevDiagnostics={enableDevDiagnostics} locale={locale}>
<ClientBootstrap
enableDevDiagnostics={enableDevDiagnostics}
initialLocaleCatalog={initialLocaleCatalog}
locale={locale}
>
{children}
</ClientBootstrap>
{enableDevDiagnostics && <Agentation />}
Expand Down
1 change: 1 addition & 0 deletions apps/editor/components/embed-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import '@/lib/bootstrap'
import { initSpatialGridSync, useScene } from '@pascal-app/core'
import {
applySceneGraphToEditor,
Expand Down
24 changes: 15 additions & 9 deletions apps/editor/components/scene-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use client'

// Node registry bootstrap is loaded once at the root via
// `<ClientBootstrap>` in `app/layout.tsx` — no per-page side-effect
// import here.
// Populates the built-in node registry synchronously before `<Editor>` mounts,
// scoped to 3D routes instead of the root layout so non-3D pages stay light.
import '@/lib/bootstrap'
import { type MessageDescriptor, usePascalI18n } from '@pascal-app/core'
import {
applySceneGraphToEditor,
Expand All @@ -11,11 +11,10 @@ import {
type SceneGraph,
type SidebarTab,
} from '@pascal-app/editor'
import { PascalWebXRButton } from '@webxr/plugin/pascal-editor'
import { Hammer, Layers, Package, Palette, Settings } from 'lucide-react'
import Image from 'next/image'
import { useRouter, useSearchParams } from 'next/navigation'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { lazy, Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { PoolSectionBanner } from '@/components/pool-section-banner'
import { countGraphNodes, isEmptyGraphOverwrite } from '@/lib/empty-graph-guard'
import { useLocalArtifactStore } from '@/lib/local-artifact-store'
Expand All @@ -32,6 +31,11 @@ import { PaintPanel } from './paint-panel'
import { EditorViewerToolbarLeft, EditorViewerToolbarRight } from './viewer-toolbar'
import { useWebXRFeature, useWebXRInstalled, WebXRFeatureRuntime } from './webxr-feature-gate'

const LazyPascalWebXRButton = lazy(async () => {
const mod = await import('@webxr/plugin/pascal-editor')
return { default: mod.PascalWebXRButton }
})

export interface SceneMeta {
id: string
name: string
Expand Down Expand Up @@ -421,10 +425,12 @@ function SceneEditor({ initialScene, meta: initialMeta }: SceneLoaderProps) {
<EditorViewerToolbarRight
vrButton={
vr ? (
<PascalWebXRButton
className="flex h-8 w-8 items-center justify-center text-muted-foreground hover:bg-accent disabled:opacity-50"
feature={vr}
/>
<Suspense fallback={null}>
<LazyPascalWebXRButton
className="flex h-8 w-8 items-center justify-center text-muted-foreground hover:bg-accent disabled:opacity-50"
feature={vr}
/>
</Suspense>
) : null
}
vrLabel="Enter VR"
Expand Down
37 changes: 37 additions & 0 deletions apps/editor/components/webxr-enabled-runtime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use client'

import { usePascalWebXR } from '@webxr/plugin/pascal-editor'
import { useEffect, useMemo } from 'react'
import { webXRWandBindings } from './webxr-wand-bindings'

export type PascalWebXRFeature = ReturnType<typeof usePascalWebXR>

export default function EnabledWebXRFeature({
onFeature,
}: {
onFeature: (feature: PascalWebXRFeature | null) => void
}) {
const feature = usePascalWebXR(webXRWandBindings)
// Keep the context stable when the plugin returns a new aggregate object.
// biome-ignore lint/correctness/useExhaustiveDependencies: stabilize on public feature fields
const stableFeature = useMemo(
() => feature,
[
feature.enter,
feature.entering,
feature.error,
feature.exit,
feature.immersive,
feature.ready,
feature.runtime,
feature.session,
],
)

useEffect(() => {
onFeature(stableFeature)
return () => onFeature(null)
}, [onFeature, stableFeature])

return null
}
51 changes: 15 additions & 36 deletions apps/editor/components/webxr-feature-gate.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
'use client'

import { useScene } from '@pascal-app/editor'
import { WEBXR_PLUGIN_ID } from '@webxr/plugin'
import { usePascalWebXR } from '@webxr/plugin/pascal-editor'
import {
createContext,
lazy,
type ReactNode,
Suspense,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react'
import { webXRWandBindings } from './webxr-wand-bindings'
import type { PascalWebXRFeature } from './webxr-enabled-runtime'

export type PascalWebXRFeature = ReturnType<typeof usePascalWebXR>
export type { PascalWebXRFeature }

const WebXRFeatureContext = createContext<PascalWebXRFeature | null>(null)
// Kept inline instead of importing from `@webxr/plugin` so the WebXR bundle
// (`@react-three/xr`, `iwer`, `@iwer/devui`) is never loaded unless the plugin
// is actually installed in the active scene.
const WEBXR_PLUGIN_ID = 'webxr:core'

function EnabledWebXRFeature({
onFeature,
}: {
onFeature: (feature: PascalWebXRFeature | null) => void
}) {
const feature = usePascalWebXR(webXRWandBindings)
// Keep the context stable when the plugin returns a new aggregate object.
// biome-ignore lint/correctness/useExhaustiveDependencies: stabilize on public feature fields
const stableFeature = useMemo(
() => feature,
[
feature.enter,
feature.entering,
feature.error,
feature.exit,
feature.immersive,
feature.ready,
feature.runtime,
feature.session,
],
)
const LazyEnabledWebXRFeature = lazy(() => import('./webxr-enabled-runtime'))

useEffect(() => {
onFeature(stableFeature)
return () => onFeature(null)
}, [onFeature, stableFeature])

return null
}
const WebXRFeatureContext = createContext<PascalWebXRFeature | null>(null)

export function WebXRFeatureRuntime({
enabled,
Expand All @@ -61,7 +36,11 @@ export function WebXRFeatureRuntime({
return (
<WebXRFeatureContext.Provider value={enabled ? feature : null}>
{children}
{enabled ? <EnabledWebXRFeature onFeature={setFeatureStable} /> : null}
{enabled ? (
<Suspense fallback={null}>
<LazyEnabledWebXRFeature onFeature={setFeatureStable} />
</Suspense>
) : null}
</WebXRFeatureContext.Provider>
)
}
Expand Down
Loading
Loading