component view cleanup

This commit is contained in:
robert dennis
2026-10-08 16:53:56 -04:00
parent c3966074d3
commit df1a5deea1
3 changed files with 63 additions and 73 deletions
+21 -29
View File
@@ -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<TName extends ComponentName>({
}) {
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 <Default />;
const Component = decision.component;
return (
<ExtensionContext.Provider value={mount}>
<ExtensionBoundary {...mount} resetKey={resetKey} onReset={noReset} fallback={() => <Default />}>
<Suspense fallback={<SuspendedView onSuspend={onSuspend} />}>
<Component {...props} />
</Suspense>
</ExtensionBoundary>
</ExtensionContext.Provider>
<ExtensionMount
extensionId={extensionId}
context={context}
resetKey={resetKey}
loading={<SuspendedView onSuspend={onSuspend} />}
failure={<Default />}
>
<Component {...props} />
</ExtensionMount>
);
}
@@ -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<Props, BoundaryState> {
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;
}
}
@@ -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<BoundaryProps, BoundaryState> {
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;
}