Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.2 KB
Raw Blame
import { Component } from 'react';
import { AlertTriangle } from 'lucide-react';
import Banner from './ui/Banner';
import { isStaleChunkError, reloadOnceForStaleChunk } from '../utils/staleChunkReload';

export default class ErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, error: null };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true, error };
  }

  componentDidUpdate(previousProps) {
    if (this.state.hasError && previousProps.resetKey !== this.props.resetKey) {
      this.resetError();
    }
  }

  resetError = () => {
    this.setState({ hasError: false, error: null });
  };

  componentDidCatch(error, errorInfo) {
    const report = () => {
      console.error(`💥 React Error: ${error.message}`, errorInfo);
      // `onError` lets a caller that owns chrome AROUND the failed subtree
      // react to the failure — an r3f `<Canvas>` can only degrade to
      // `fallback={null}` inside the scene, so the DOM-side message has to be
      // rendered by the parent from its own state.
      this.props.onError?.(error);
    };

    if (isStaleChunkError(error, { duringRender: true })) {
      // React may catch missing runtime exports after import() already resolved.
      // Recover only when the live shell confirms this page is stale; otherwise
      // report the real render error to the owning boundary.
      void reloadOnceForStaleChunk().then((reloaded) => {
        if (!reloaded) report();
      });
      return;
    }

    report();
  }

  render() {
    if (this.state.hasError) {
      // Callers can supply a custom fallback (including `null`) — used to degrade
      // gracefully inside non-DOM trees like the R3F Canvas, where the default
      // full-screen DOM error UI can't render.
      if (this.props.fallback !== undefined) {
        return typeof this.props.fallback === 'function'
          ? this.props.fallback(this.state.error, this.resetError)
          : this.props.fallback;
      }
      return (
        <div className="min-h-dvh-cap bg-port-bg flex items-center justify-center p-4">
          <div className="bg-port-card border border-port-border rounded-xl p-8 max-w-md w-full">
            <div className="flex items-center justify-center mb-4">
              <AlertTriangle size={32} className="text-port-error" />
            </div>
            <h1 className="text-xl font-bold text-port-text text-center mb-2">Something went wrong</h1>
            <p className="text-port-text-muted text-sm text-center mb-4">
              An unexpected error occurred. Please try refreshing the page.
            </p>
            {this.state.error && (
              <Banner tone="error" size="md" className="mb-4">
                <p className="text-xs font-mono break-all">
                  {this.state.error.message}
                </p>
              </Banner>
            )}
            <button
              onClick={() => window.location.reload()}
              className="w-full px-4 py-2 bg-port-accent hover:bg-port-accent/80 text-port-on-accent rounded-lg transition-colors"
            >
              Refresh Page
            </button>
          </div>
        </div>
      );
    }

    return this.props.children;
  }
}