File size: 3,199 Bytes
c0a9bce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
export interface DebugWarning {
  id: string;
  message: string;
  timestamp: string;
  code: string;
}

export interface DebugError {
  id: string;
  message: string;
  timestamp: string;
  stack?: string;
}

export interface DebugStatus {
  warnings: DebugIssue[];
  errors: DebugIssue[];
}

export interface DebugIssue {
  id: string;
  message: string;
  type: 'warning' | 'error';
  timestamp: string;
  details?: Record<string, unknown>;
}

// Keep track of acknowledged issues
const acknowledgedIssues = new Set<string>();

export const getDebugStatus = async (): Promise<DebugStatus> => {
  const issues: DebugStatus = {
    warnings: [],
    errors: [],
  };

  try {
    // Check memory usage
    if (performance && 'memory' in performance) {
      const memory = (performance as any).memory;

      if (memory.usedJSHeapSize > memory.jsHeapSizeLimit * 0.8) {
        issues.warnings.push({
          id: 'high-memory-usage',
          message: 'High memory usage detected',
          type: 'warning',
          timestamp: new Date().toISOString(),
          details: {
            used: memory.usedJSHeapSize,
            total: memory.jsHeapSizeLimit,
          },
        });
      }
    }

    // Check storage quota
    if (navigator.storage && navigator.storage.estimate) {
      const estimate = await navigator.storage.estimate();
      const usageRatio = (estimate.usage || 0) / (estimate.quota || 1);

      if (usageRatio > 0.9) {
        issues.warnings.push({
          id: 'storage-quota-warning',
          message: 'Storage quota nearly reached',
          type: 'warning',
          timestamp: new Date().toISOString(),
          details: {
            used: estimate.usage,
            quota: estimate.quota,
          },
        });
      }
    }

    // Check for console errors (if any)
    const errorLogs = localStorage.getItem('error_logs');

    if (errorLogs) {
      const errors = JSON.parse(errorLogs);
      errors.forEach((error: any) => {
        issues.errors.push({
          id: `error-${error.timestamp}`,
          message: error.message,
          type: 'error',
          timestamp: error.timestamp,
          details: error.details,
        });
      });
    }

    // Filter out acknowledged issues
    issues.warnings = issues.warnings.filter((warning) => !acknowledgedIssues.has(warning.id));
    issues.errors = issues.errors.filter((error) => !acknowledgedIssues.has(error.id));

    return issues;
  } catch (error) {
    console.error('Error getting debug status:', error);
    return issues;
  }
};

export const acknowledgeWarning = async (id: string): Promise<void> => {
  acknowledgedIssues.add(id);
};

export const acknowledgeError = async (id: string): Promise<void> => {
  acknowledgedIssues.add(id);

  // Also remove from error logs if present
  try {
    const errorLogs = localStorage.getItem('error_logs');

    if (errorLogs) {
      const errors = JSON.parse(errorLogs);
      const updatedErrors = errors.filter((error: any) => `error-${error.timestamp}` !== id);
      localStorage.setItem('error_logs', JSON.stringify(updatedErrors));
    }
  } catch (error) {
    console.error('Error acknowledging error:', error);
  }
};