'use client';

import { Component, type ReactNode } from 'react';

/**
 * Contains a rendering failure to one scene. A bar TV runs unattended, so instead of a blank screen it
 * shows the fallback until the next scene (the boundary is keyed per scene, which resets it).
 */
export class SceneBoundary extends Component<{ children: ReactNode; fallback: ReactNode }, { failed: boolean }> {
  override state = { failed: false };

  static getDerivedStateFromError(): { failed: boolean } {
    return { failed: true };
  }

  override componentDidCatch(error: unknown): void {
    console.error('[tv] scene failed to render', error);
  }

  override render(): ReactNode {
    return this.state.failed ? this.props.fallback : this.props.children;
  }
}
