diff --git a/resources/scripts/extensions/ComponentView.tsx b/resources/scripts/extensions/ComponentView.tsx index af4450d28..1df95e66c 100644 --- a/resources/scripts/extensions/ComponentView.tsx +++ b/resources/scripts/extensions/ComponentView.tsx @@ -1,11 +1,9 @@ -import { Suspense, useLayoutEffect, useMemo, useState, type ReactNode } from 'react'; -import ExtensionBoundary from './ExtensionBoundary'; -import { ExtensionContext } from './context'; +import { useCallback, useLayoutEffect, useState, type ReactNode } from 'react'; +import ExtensionMount from './ExtensionMount'; import { reportExtensionError } from './registry'; import { useComponentDecision } from './componentSession'; import type { ComponentName, ReplacementProps } from './componentTypes'; -const noReset = () => {}; function SuspendedView({ onSuspend }: { onSuspend: () => void }) { useLayoutEffect(onSuspend, [onSuspend]); return null; @@ -24,35 +22,29 @@ export default function ComponentView({ }) { const decision = useComponentDecision(name); const [suspended, setSuspended] = useState(false); - const mount = useMemo( - () => ({ - extensionId: decision.status === 'ready' ? decision.extensionId : '', - context: `component "${name}" (${resetKey})`, - }), - [decision, name, resetKey] - ); - const onSuspend = useMemo( - () => () => { - reportExtensionError( - mount.extensionId, - mount.context, - new Error('Replacement suspended outside a local Suspense boundary; using the native view.') - ); - setSuspended(true); - }, - [mount] - ); + const extensionId = decision.status === 'ready' ? decision.extensionId : ''; + const context = `component "${name}" (${resetKey})`; + const onSuspend = useCallback(() => { + reportExtensionError( + extensionId, + context, + new Error('Replacement suspended outside a local Suspense boundary; using the native view.') + ); + setSuspended(true); + }, [extensionId, context]); const Default = props.Default; if (decision.status === 'loading') return loading; if (decision.status === 'default' || suspended) return ; const Component = decision.component; return ( - - }> - }> - - - - + } + failure={} + > + + ); } diff --git a/resources/scripts/extensions/ExtensionBoundary.tsx b/resources/scripts/extensions/ExtensionBoundary.tsx deleted file mode 100644 index 2aba0f48b..000000000 --- a/resources/scripts/extensions/ExtensionBoundary.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import React from 'react'; -import { clearExtensionError, reportExtensionError } from '@/extensions/registry'; - -interface Props { - extensionId: string; - context: string; - resetKey: string; - onReset: () => void; - fallback: (error: Error, retry: () => void) => React.ReactNode; - children?: React.ReactNode; -} - -interface BoundaryState { - error: Error | null; -} - -export default class ExtensionBoundary extends React.Component { - state: BoundaryState = { error: null }; - - static getDerivedStateFromError(cause: unknown) { - return { error: cause instanceof Error ? cause : new Error(String(cause)) }; - } - - componentDidCatch(error: Error) { - reportExtensionError(this.props.extensionId, this.props.context, error); - } - - componentDidUpdate(previous: Props) { - if (previous.resetKey !== this.props.resetKey && this.state.error) this.retry(); - } - - retry = () => { - this.props.onReset(); - clearExtensionError(this.props.extensionId, this.props.context); - this.setState({ error: null }); - }; - - render() { - return this.state.error ? this.props.fallback(this.state.error, this.retry) : this.props.children; - } -} diff --git a/resources/scripts/extensions/ExtensionMount.tsx b/resources/scripts/extensions/ExtensionMount.tsx index edd827351..2416de6a3 100644 --- a/resources/scripts/extensions/ExtensionMount.tsx +++ b/resources/scripts/extensions/ExtensionMount.tsx @@ -1,11 +1,50 @@ -import { Suspense, useEffect, useMemo, type ReactNode } from 'react'; +import { Component, Suspense, useEffect, useMemo, type ReactNode } from 'react'; import { QueryErrorResetBoundary } from '@tanstack/react-query'; import { toast } from 'sonner'; import Button from '@/components/elements/Button'; -import ExtensionBoundary from '@/extensions/ExtensionBoundary'; -import { ExtensionImportError } from '@/extensions/registry'; +import { clearExtensionError, ExtensionImportError, reportExtensionError } from '@/extensions/registry'; import { ExtensionContext } from '@/extensions/context'; +interface BoundaryProps { + extensionId: string; + context: string; + resetKey: string; + onReset: () => void; + fallback: (error: Error, retry: () => void) => ReactNode; + children?: ReactNode; +} + +/** Records a crash against the extension and renders the fallback until it is retried or the key changes. */ +interface BoundaryState { + error: Error | null; +} + +class ExtensionBoundary extends Component { + state: BoundaryState = { error: null }; + + static getDerivedStateFromError(cause: unknown) { + return { error: cause instanceof Error ? cause : new Error(String(cause)) }; + } + + componentDidCatch(error: Error) { + reportExtensionError(this.props.extensionId, this.props.context, error); + } + + componentDidUpdate(previous: BoundaryProps) { + if (previous.resetKey !== this.props.resetKey && this.state.error) this.retry(); + } + + retry = () => { + this.props.onReset(); + clearExtensionError(this.props.extensionId, this.props.context); + this.setState({ error: null }); + }; + + render() { + return this.state.error ? this.props.fallback(this.state.error, this.retry) : this.props.children; + } +} + interface FailedMount { retry?: () => void; }