File size: 3,392 Bytes
ccd47e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { Component, type ReactNode } from 'react';
import { RotateCcw, X, AlertTriangle } from 'lucide-react';

interface WindowErrorBoundaryProps {
  windowId: string;
  windowTitle?: string;
  onClose?: () => void;
  children: ReactNode;
}

interface WindowErrorBoundaryState {
  hasError: boolean;
  error: Error | null;
}

export default class WindowErrorBoundary extends Component<
  WindowErrorBoundaryProps,
  WindowErrorBoundaryState
> {
  constructor(props: WindowErrorBoundaryProps) {
    super(props);
    this.state = { hasError: false, error: null };
  }

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

  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    console.error(
      `[WindowErrorBoundary] Error caught in window "${this.props.windowId}":`,
      error,
      errorInfo
    );
  }

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

  render() {
    if (this.state.hasError) {
      return (
        <div className="w-full h-full p-6 bg-[#0f172a] text-white flex flex-col items-center justify-center font-sans select-none overflow-auto">
          <div className="max-w-md w-full bg-[#1e293b] border-[3px] border-rose-500 rounded-lg p-5 shadow-[6px_6px_0px_0px_rgba(0,0,0,0.5)] flex flex-col gap-4">
            {/* Header */}
            <div className="flex items-center gap-2 text-rose-400 font-headline-md font-bold text-sm border-b border-slate-700 pb-2.5">
              <AlertTriangle className="w-5 h-5 shrink-0" />
              <span>SYSTEM INTERRUPT: {this.props.windowTitle || this.props.windowId}</span>
            </div>

            {/* Error Message */}
            <div className="bg-[#0b0f19] border border-slate-800 rounded p-3 font-mono text-[11px] text-rose-300 leading-relaxed max-h-32 overflow-y-auto custom-scrollbar">
              {this.state.error?.message || 'An unexpected runtime error occurred.'}
            </div>

            <p className="text-[11px] text-slate-400 leading-relaxed">
              The window process encountered a recoverable fault. You can restart this window module without losing your desktop session.
            </p>

            {/* Action Buttons */}
            <div className="flex items-center justify-end gap-2 pt-2 border-t border-slate-700">
              {this.props.onClose && (
                <button
                  type="button"
                  onClick={this.props.onClose}
                  className="px-3 py-1.5 bg-slate-700 hover:bg-slate-600 text-white rounded text-xs font-bold transition-all flex items-center gap-1.5 cursor-pointer"
                >
                  <X className="w-3.5 h-3.5" />
                  <span>Close Window</span>
                </button>
              )}
              <button
                type="button"
                onClick={this.handleReset}
                className="px-3 py-1.5 bg-rose-600 hover:bg-rose-500 text-white rounded text-xs font-bold transition-all shadow-[2px_2px_0px_0px_#000] active:translate-y-0.5 active:shadow-none flex items-center gap-1.5 cursor-pointer"
              >
                <RotateCcw className="w-3.5 h-3.5" />
                <span>Restart Module</span>
              </button>
            </div>
          </div>
        </div>
      );
    }

    return this.props.children;
  }
}