import { Component, useState, type ErrorInfo, type ReactNode } from "react"; import { isChunkLoadError } from "../lib/chunk-load-error"; import { LoadFailureNotice } from "./load-failure-notice"; import { useOpenGeniText } from "../messages/runtime"; type ConversationRenderBoundaryProps = { /** A change (another session or workspace) clears a previous failure. */ resetKey: string; children: ReactNode; }; type BoundaryState = { failed: boolean; notLoaded: boolean; resetKey: string }; /** * Contains a rendering error inside the chat. Without it, one bad payload or * an incompatible host response unmounts the whole embedding page (a blank * Site or dashboard). Try again remounts the chat with fresh data. Code that * failed to download asks for a reload instead: remounting reuses the failed * import. */ export function ConversationRenderBoundary({ resetKey, children, }: ConversationRenderBoundaryProps) { const [attempt, setAttempt] = useState(0); return ( setAttempt((value) => value + 1)}> {children} ); } class Boundary extends Component< { resetKey: string; onRetry: () => void; children: ReactNode }, BoundaryState > { state: BoundaryState = { failed: false, notLoaded: false, resetKey: this.props.resetKey }; static getDerivedStateFromError(error: unknown): Partial { return { failed: true, notLoaded: isChunkLoadError(error) }; } static getDerivedStateFromProps( props: { resetKey: string }, state: BoundaryState, ): Partial | null { return props.resetKey !== state.resetKey ? { failed: false, notLoaded: false, resetKey: props.resetKey } : null; } componentDidCatch(error: unknown, info: ErrorInfo): void { console.error("Opengeni chat failed to render", error, info.componentStack); } render(): ReactNode { if (!this.state.failed) return this.props.children; return this.state.notLoaded ? ( ) : ( ); } } function ConversationRenderError({ onRetry }: { onRetry: () => void }) { const t = useOpenGeniText(); return ( {t("conversationRenderBoundary.chatUnavailable")} {t("conversationRenderBoundary.tryAgain")} ); }
{t("conversationRenderBoundary.chatUnavailable")}