-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIssueCard.tsx
More file actions
239 lines (222 loc) · 9.29 KB
/
Copy pathIssueCard.tsx
File metadata and controls
239 lines (222 loc) · 9.29 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { useState } from 'react';
import type { Issue } from '@/types';
interface IssueCardProps {
issue: Issue;
}
const SEVERITY_CONFIG = {
error: { iconClass: 'indicator-dot indicator-dot--error', color: '#ff4444', label: 'Error', bgColor: 'rgba(255, 68, 68, 0.1)' },
warning: { iconClass: 'indicator-dot indicator-dot--warning', color: '#ffaa00', label: 'Warning', bgColor: 'rgba(255, 170, 0, 0.1)' },
info: { iconClass: 'indicator-dot indicator-dot--info', color: '#4488ff', label: 'Info', bgColor: 'rgba(68, 136, 255, 0.1)' },
};
const ISSUE_INFO: Record<string, { title: string; why: string; learnUrl?: string }> = {
MISSING_KEY: {
title: 'Missing Key in List',
why: 'Keys help React identify which items have changed, are added, or removed.',
learnUrl: 'https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key',
},
INDEX_AS_KEY: {
title: 'Index Used as Key',
why: 'Using index as key can cause issues when list items are reordered or filtered.',
learnUrl: 'https://react.dev/learn/rendering-lists#why-does-react-need-keys',
},
MISSING_CLEANUP: {
title: 'Missing Effect Cleanup',
why: 'Effects with subscriptions, timers, or event listeners need cleanup to prevent memory leaks.',
learnUrl: 'https://react.dev/learn/synchronizing-with-effects#step-3-add-cleanup-if-needed',
},
MISSING_DEP: {
title: 'Missing Dependencies',
why: 'Missing dependencies can cause stale closures and bugs.',
learnUrl: 'https://react.dev/reference/react/useEffect#specifying-reactive-dependencies',
},
INFINITE_LOOP_RISK: {
title: 'Infinite Loop Risk',
why: 'This effect may cause infinite re-renders if state is updated without proper guards.',
learnUrl: 'https://react.dev/reference/react/useEffect#my-effect-runs-twice-in-strict-mode',
},
EXCESSIVE_RERENDERS: {
title: 'Excessive Re-renders',
why: 'Too many re-renders can cause performance issues and poor user experience.',
learnUrl: 'https://react.dev/reference/react/memo',
},
STALE_CLOSURE: {
title: 'Stale Closure Detected',
why: 'This callback was created in an earlier render and may be using outdated state or props values. This is one of the most common and hard-to-debug React bugs.',
learnUrl: 'https://react.dev/learn/referencing-values-with-refs#differences-between-refs-and-state',
},
STALE_CLOSURE_RISK: {
title: 'Potential Stale Closure',
why: 'This async operation or event handler might capture stale values if the component re-renders before execution.',
learnUrl: 'https://react.dev/reference/react/useCallback',
},
};
export const IssueCard = React.memo(function IssueCard({ issue }: IssueCardProps) {
const [expanded, setExpanded] = useState(false);
const severity = SEVERITY_CONFIG[issue.severity];
const info = ISSUE_INFO[issue.type] || { title: issue.type, why: '' };
const location = issue.location;
const renderCountMatch = issue.message.match(/Rendered (\d+) times/);
const renderCount = renderCountMatch ? parseInt(renderCountMatch[1], 10) : null;
return (
<div
className={`issue-card severity-${issue.severity}`}
style={{ borderLeftColor: severity.color }}
>
<div
className="issue-header"
role="button"
tabIndex={0}
aria-expanded={expanded}
onClick={() => setExpanded(!expanded)}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
setExpanded(!expanded);
}
}}
>
<span className={`issue-icon ${severity.iconClass}`} />
<div className="issue-info">
<div className="issue-title-row">
<h4 className="issue-title">{info.title}</h4>
<span
className="severity-badge"
style={{
backgroundColor: severity.bgColor,
color: severity.color,
border: `1px solid ${severity.color}`,
}}
>
{severity.label}
</span>
</div>
<div className="issue-location">
{location?.elementType && (
<span className="element-type">{location.elementType}</span>
)}
<span className="issue-component">
in <strong>{issue.component}</strong>
</span>
{renderCount !== null && (
<span className="render-count-badge" title="Renders in last second">
{renderCount}×
</span>
)}
{location?.componentPath && location.componentPath.length > 1 && (
<span className="component-path">
({location.componentPath.join(' → ')})
</span>
)}
</div>
</div>
<span className="expand-button">{expanded ? '▼' : '▶'}</span>
</div>
{expanded && (
<div className="issue-details">
<p className="issue-message">{issue.message}</p>
{location?.childElements && location.childElements.length > 0 && (
<div className="issue-elements">
<strong>Affected Elements:</strong>
<div className="elements-list">
{location.childElements.slice(0, 10).map((el, i) => (
<div key={i} className={`element-item ${el.key === null ? 'missing-key' : ''}`}>
<span className="el-index">[{el.index}]</span>
<span className="el-type">{el.type}</span>
<span className={`el-key ${el.key === null ? 'null' : ''}`}>
key={el.key === null ? 'null' : `"${el.key}"`}
</span>
</div>
))}
{location.childElements.length > 10 && (
<div className="element-item more">
...and {location.childElements.length - 10} more
</div>
)}
</div>
</div>
)}
{location?.closureInfo && (
<div className="closure-info">
<strong><span className="action-badge action-badge--search" /> Closure Timeline:</strong>
<div className="closure-timeline">
<div className="timeline-item created">
<span className="timeline-badge">Created</span>
<span className="timeline-detail">
Render #{location.closureInfo.createdAtRender}
</span>
</div>
<div className="timeline-arrow">→</div>
<div className="timeline-item executed">
<span className="timeline-badge warning">Executed</span>
<span className="timeline-detail">
Render #{location.closureInfo.executedAtRender}
</span>
</div>
</div>
<div className="closure-details">
<div className="closure-row">
<span className="closure-label">Function:</span>
<code className="closure-value">{location.closureInfo.functionName}()</code>
</div>
<div className="closure-row">
<span className="closure-label">Type:</span>
<span className={`closure-type type-${location.closureInfo.asyncType}`}>
{location.closureInfo.asyncType}
</span>
</div>
<div className="closure-row">
<span className="closure-label">Renders behind:</span>
<span className="closure-stale-count">
{location.closureInfo.executedAtRender - location.closureInfo.createdAtRender} render(s)
</span>
</div>
</div>
{location.closureInfo.capturedValues.length > 0 && (
<div className="captured-values">
<strong>Potentially stale variables:</strong>
<div className="values-list">
{location.closureInfo.capturedValues.map((v, i) => (
<div key={i} className="value-item">
<code>{v.name}</code>
</div>
))}
</div>
</div>
)}
</div>
)}
{info.why && (
<div className="issue-why">
<strong>Why this matters:</strong>
<p>{info.why}</p>
</div>
)}
<div className="issue-suggestion">
<strong><span className="action-badge action-badge--suggestion" /> Suggestion:</strong>
<p>{issue.suggestion}</p>
</div>
{issue.code && (
<div className="issue-code">
<strong>Example:</strong>
<pre>
<code>{issue.code}</code>
</pre>
</div>
)}
<div className="issue-actions">
{info.learnUrl && (
<a
href={info.learnUrl}
target="_blank"
rel="noopener noreferrer"
className="learn-link"
>
<span className="action-badge action-badge--learn" /> Learn more
</a>
)}
</div>
</div>
)}
</div>
);
});