forked from codinit-dev/codinit-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportErrorModal.tsx
More file actions
373 lines (336 loc) · 13.4 KB
/
Copy pathImportErrorModal.tsx
File metadata and controls
373 lines (336 loc) · 13.4 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import * as RadixDialog from '@radix-ui/react-dialog';
import { memo, useState, useMemo } from 'react';
import { Dialog, DialogTitle, DialogDescription } from './Dialog';
import { Button } from './Button';
import { classNames } from '~/utils/classNames';
export interface FailedFileWithError {
path: string;
error: string;
errorType?: 'network' | 'rate_limit' | 'auth' | 'size' | 'format' | 'unknown';
}
interface ImportErrorModalProps {
isOpen: boolean;
type: 'warning' | 'error';
title: string;
message: string;
failedFiles?: string[];
failedFilesWithErrors?: FailedFileWithError[];
successCount?: number;
totalCount?: number;
onRetry?: () => void;
onRetryWithExclusions?: (excludedFiles: string[]) => void;
onContinue?: () => void;
onClose?: () => void;
onAskAI?: (errorContext: string) => void;
postMessage?: (message: string) => void;
}
const ERROR_TYPE_INFO: Record<string, { label: string; color: string; icon: string; bgColor: string }> = {
network: {
label: 'Network',
color: 'text-blue-600 dark:text-blue-400',
icon: 'i-ph:wifi-x',
bgColor: 'bg-blue-100 dark:bg-blue-500/20',
},
rate_limit: {
label: 'Rate Limit',
color: 'text-orange-600 dark:text-orange-400',
icon: 'i-ph:clock',
bgColor: 'bg-orange-100 dark:bg-orange-500/20',
},
auth: {
label: 'Auth',
color: 'text-blue-600 dark:text-blue-400',
icon: 'i-ph:lock',
bgColor: 'bg-blue-100 dark:bg-blue-500/20',
},
size: {
label: 'File Size',
color: 'text-yellow-600 dark:text-yellow-400',
icon: 'i-ph:file-x',
bgColor: 'bg-yellow-100 dark:bg-yellow-500/20',
},
format: {
label: 'Format',
color: 'text-pink-600 dark:text-pink-400',
icon: 'i-ph:warning',
bgColor: 'bg-pink-100 dark:bg-pink-500/20',
},
unknown: {
label: 'Unknown',
color: 'text-gray-600 dark:text-gray-400',
icon: 'i-ph:question',
bgColor: 'bg-gray-100 dark:bg-gray-500/20',
},
};
function copyToClipboard(text: string) {
navigator.clipboard.writeText(text).catch((err) => {
console.error('Failed to copy:', err);
});
}
export const ImportErrorModal = memo(
({
isOpen,
type,
title,
message,
failedFiles,
failedFilesWithErrors,
successCount,
totalCount,
onRetry,
onRetryWithExclusions,
onContinue,
onClose,
onAskAI,
postMessage,
}: ImportErrorModalProps) => {
const [selectedForExclusion, setSelectedForExclusion] = useState<Set<string>>(new Set());
const [isExpanded, setIsExpanded] = useState(false);
const failedFilesList = useMemo(() => {
if (failedFilesWithErrors) {
return failedFilesWithErrors;
}
if (failedFiles) {
return failedFiles.map((path) => ({
path,
error: 'Import failed',
errorType: 'unknown' as const,
}));
}
return [];
}, [failedFiles, failedFilesWithErrors]);
const groupedErrors = useMemo(() => {
const groups: Record<string, FailedFileWithError[]> = {};
failedFilesList.forEach((file) => {
const errorType = file.errorType || 'unknown';
if (!groups[errorType]) {
groups[errorType] = [];
}
groups[errorType].push(file);
});
return groups;
}, [failedFilesList]);
const handleToggleExclusion = (filePath: string) => {
const newSet = new Set(selectedForExclusion);
if (newSet.has(filePath)) {
newSet.delete(filePath);
} else {
newSet.add(filePath);
}
setSelectedForExclusion(newSet);
};
const handleRetryWithExclusions = () => {
if (onRetryWithExclusions) {
onRetryWithExclusions(Array.from(selectedForExclusion));
}
};
const handleCopyErrorReport = () => {
const report = `
=== Import Error Report ===
Title: ${title}
${totalCount !== undefined ? `Total Files: ${totalCount}` : ''}
${successCount !== undefined ? `Successful: ${successCount}` : ''}
Failed: ${failedFilesList.length}
${message}
Failed Files by Type:
${Object.entries(groupedErrors)
.map(
([errorType, files]) => `
${ERROR_TYPE_INFO[errorType]?.label || errorType} (${files.length}):
${files.map((f) => ` - ${f.path}: ${f.error}`).join('\n')}`,
)
.join('\n')}
`.trim();
copyToClipboard(report);
};
const handleAskAI = () => {
const aiPrompt = `*Help fix these import errors*
**Import Source:** ${title}
${totalCount !== undefined ? `**Total Files:** ${totalCount}` : ''}
${successCount !== undefined ? `**Successful:** ${successCount}` : ''}
**Failed:** ${failedFilesList.length}
**Error Summary:**
${message}
**Failed Files by Type:**
${Object.entries(groupedErrors)
.map(
([errorType, files]) =>
`\n**${ERROR_TYPE_INFO[errorType]?.label || errorType}** (${files.length} files):\n${files.map((f) => `- \`${f.path}\`: ${f.error}`).join('\n')}`,
)
.join('\n')}
Please analyze these errors and suggest how to resolve the import issues.
`;
if (postMessage) {
postMessage(aiPrompt);
} else if (onAskAI) {
onAskAI(aiPrompt);
}
};
const hasAIIntegration = Boolean(postMessage || onAskAI);
const hasExclusionFeature = Boolean(onRetryWithExclusions);
const showStats = successCount !== undefined && totalCount !== undefined;
return (
<RadixDialog.Root open={isOpen} onOpenChange={onClose}>
<Dialog showCloseButton={false}>
<div className="p-6 bg-white dark:bg-gray-950 relative z-10 max-w-2xl">
<div className="flex items-start gap-3">
<div
className={classNames(
'flex-shrink-0 w-10 h-10 rounded-full flex items-center justify-center',
type === 'warning' ? 'bg-yellow-100 dark:bg-yellow-500/20' : 'bg-red-100 dark:bg-red-500/20',
)}
>
{type === 'warning' ? (
<span className="text-yellow-600 dark:text-yellow-400 text-xl">⚠️</span>
) : (
<span className="text-red-600 dark:text-red-400 text-xl">❌</span>
)}
</div>
<div className="flex-1">
<div className="flex items-center justify-between mb-1">
<DialogTitle>{title}</DialogTitle>
{failedFilesList.length > 0 && (
<button
onClick={handleCopyErrorReport}
className="text-codinit-elements-textSecondary hover:text-codinit-elements-textPrimary transition-colors"
title="Copy error report"
>
<div className="i-ph:copy text-base"></div>
</button>
)}
</div>
{showStats && (
<div className="flex gap-3 mb-3 text-xs">
<span className="text-green-600 dark:text-green-400">✓ {successCount} succeeded</span>
<span className="text-red-600 dark:text-red-400">✗ {failedFilesList.length} failed</span>
<span className="text-codinit-elements-textSecondary">Total: {totalCount}</span>
</div>
)}
<DialogDescription className="mb-3">{message}</DialogDescription>
{failedFilesList.length > 0 && (
<div className="mt-4 mb-4">
<div className="flex items-center justify-between mb-2">
<p className="text-sm font-medium text-codinit-elements-textPrimary">
Failed files ({failedFilesList.length}):
</p>
{failedFilesList.length > 5 && (
<button
onClick={() => setIsExpanded(!isExpanded)}
className="text-xs text-codinit-elements-textSecondary hover:text-codinit-elements-textPrimary flex items-center gap-1"
>
<div
className={classNames(
'i-ph:caret-right transition-transform',
isExpanded ? 'rotate-90' : '',
)}
></div>
{isExpanded ? 'Collapse' : 'Expand All'}
</button>
)}
</div>
<div
className={classNames(
'rounded border border-codinit-elements-borderColor bg-codinit-elements-background-depth-1 p-2',
!isExpanded && failedFilesList.length > 5 ? 'max-h-[180px]' : 'max-h-[320px]',
'overflow-y-auto',
)}
>
{Object.entries(groupedErrors).map(([errorType, files]) => {
const errorInfo = ERROR_TYPE_INFO[errorType] || ERROR_TYPE_INFO.unknown;
return (
<div key={errorType} className="mb-3 last:mb-0">
<div className="flex items-center gap-2 mb-2">
<span
className={classNames(
'px-2 py-0.5 rounded text-xs font-medium flex items-center gap-1',
errorInfo.bgColor,
errorInfo.color,
)}
>
<div className={classNames(errorInfo.icon, 'text-sm')}></div>
{errorInfo.label} ({files.length})
</span>
</div>
<ul className="space-y-1.5">
{files.map((file, index) => (
<li
key={index}
className="text-xs bg-codinit-elements-background-depth-2 rounded p-2 flex items-start gap-2"
>
{hasExclusionFeature && (
<input
type="checkbox"
checked={selectedForExclusion.has(file.path)}
onChange={() => handleToggleExclusion(file.path)}
className="mt-0.5 flex-shrink-0"
title="Exclude from retry"
/>
)}
<div className="flex-1 min-w-0">
<div className="font-mono text-codinit-elements-textPrimary break-all">
{file.path}
</div>
<div className="text-codinit-elements-textSecondary mt-0.5">{file.error}</div>
</div>
</li>
))}
</ul>
</div>
);
})}
</div>
{hasExclusionFeature && selectedForExclusion.size > 0 && (
<div className="mt-2 text-xs text-codinit-elements-textSecondary">
{selectedForExclusion.size} file(s) selected for exclusion
</div>
)}
</div>
)}
</div>
</div>
<div className="flex justify-end gap-2 mt-6 flex-wrap">
{hasAIIntegration && failedFilesList.length > 0 && (
<Button
onClick={handleAskAI}
className="bg-codinit-elements-button-primary-background text-codinit-elements-button-primary-text hover:bg-codinit-elements-button-primary-backgroundHover flex items-center gap-1.5"
>
<div className="i-ph:chat-circle-duotone text-codinit-elements-textPrimary"></div>
Ask AI to Fix
</Button>
)}
{hasExclusionFeature && selectedForExclusion.size > 0 && (
<Button
variant="outline"
onClick={handleRetryWithExclusions}
className="border-codinit-elements-borderColor text-codinit-elements-textPrimary hover:bg-codinit-elements-item-backgroundActive"
>
Exclude & Retry ({selectedForExclusion.size})
</Button>
)}
{onRetry && (
<Button
variant="outline"
onClick={onRetry}
className="border-codinit-elements-borderColor text-codinit-elements-textPrimary hover:bg-codinit-elements-item-backgroundActive"
>
Retry Failed Files
</Button>
)}
{onContinue && (
<Button onClick={onContinue} className="bg-accent-500 text-white hover:bg-accent-600">
Continue Anyway
</Button>
)}
{onClose && !onContinue && !onRetry && (
<Button onClick={onClose} className="bg-accent-500 text-white hover:bg-accent-600">
OK
</Button>
)}
</div>
</div>
</Dialog>
</RadixDialog.Root>
);
},
);
ImportErrorModal.displayName = 'ImportErrorModal';