"use client"; import React, { Component, ErrorInfo, ReactNode } from "react"; import { Box, Button, Heading, Text, VStack } from "@chakra-ui/react"; interface Props { children: ReactNode; } interface State { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, errorInfo: null, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log error to error tracking service console.error("Error caught by boundary:", error, errorInfo); this.setState({ error, errorInfo, }); // In production, send to error tracking service if (process.env.NODE_ENV === "production") { // Example: sendToErrorTracking(error, errorInfo); } } handleReset = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError) { return ( Something went wrong {this.state.error?.message || "An unexpected error occurred"} {process.env.NODE_ENV === "development" && this.state.errorInfo && ( {this.state.error?.stack} {"\n\n"} {this.state.errorInfo.componentStack} )} ); } return this.props.children; } } export default ErrorBoundary;