Fix: Improve preview loading and error handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-11 14:45:22 +00:00
parent 1e30dedca2
commit 092337ee9e
4 changed files with 123 additions and 30 deletions

View File

@@ -0,0 +1,55 @@
import React, { Component, ErrorInfo, ReactNode } from 'react';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { AlertCircle } from 'lucide-react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error?: Error;
}
export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
private handleReset = () => {
this.setState({ hasError: false, error: undefined });
window.location.reload();
};
public render() {
if (this.state.hasError) {
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-background">
<Alert variant="destructive" className="max-w-lg">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Something went wrong</AlertTitle>
<AlertDescription className="mt-2">
<p className="mb-4">
{this.state.error?.message || 'An unexpected error occurred'}
</p>
<Button onClick={this.handleReset} variant="outline">
Reload Page
</Button>
</AlertDescription>
</Alert>
</div>
);
}
return this.props.children;
}
}