-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathAnnotationPanel.tsx
More file actions
497 lines (470 loc) · 20.1 KB
/
AnnotationPanel.tsx
File metadata and controls
497 lines (470 loc) · 20.1 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import React, { useState, useRef, useEffect } from 'react';
import { Annotation, AnnotationType, Block, type EditorAnnotation } from '../types';
import { isCurrentUser } from '../utils/identity';
import { ImageThumbnail } from './ImageThumbnail';
import { EditorAnnotationCard } from './EditorAnnotationCard';
import { useIsMobile } from '../hooks/useIsMobile';
import { OverlayScrollArea } from './OverlayScrollArea';
interface PanelProps {
isOpen: boolean;
annotations: Annotation[];
blocks: Block[];
onSelect: (id: string) => void;
onDelete: (id: string) => void;
onEdit?: (id: string, updates: Partial<Annotation>) => void;
selectedId: string | null;
sharingEnabled?: boolean;
width?: number;
editorAnnotations?: EditorAnnotation[];
onDeleteEditorAnnotation?: (id: string) => void;
onClose?: () => void;
onQuickCopy?: () => Promise<void>;
onShare?: () => void;
otherFileAnnotations?: { count: number; files: number };
onOtherFileAnnotationsClick?: () => void;
}
export const AnnotationPanel: React.FC<PanelProps> = ({
isOpen,
annotations,
blocks,
onSelect,
onDelete,
onEdit,
selectedId,
sharingEnabled = true,
width,
editorAnnotations,
onDeleteEditorAnnotation,
onClose,
onQuickCopy,
onShare,
otherFileAnnotations,
onOtherFileAnnotationsClick,
}) => {
const isMobile = useIsMobile();
const [copiedText, setCopiedText] = useState(false);
const listRef = useRef<HTMLDivElement>(null);
const sortedAnnotations = [...annotations].sort((a, b) => a.createdA - b.createdA);
const totalCount = annotations.length + (editorAnnotations?.length ?? 0);
// Scroll selected annotation card into view
useEffect(() => {
if (!selectedId || !listRef.current) return;
const card = listRef.current.querySelector(`[data-annotation-id="${selectedId}"]`);
if (card) {
card.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, [selectedId]);
if (!isOpen) return null;
const panel = (
<aside
data-annotation-panel="true"
className={`border-l border-border/50 bg-card/30 backdrop-blur-sm flex flex-col flex-shrink-0 ${
isMobile ? 'fixed top-12 bottom-0 right-0 z-[60] w-full max-w-sm shadow-2xl bg-card' : ''
}`}
style={isMobile ? undefined : { width: width ?? 288 }}
>
{/* Header */}
<div className="p-3 border-b border-border/50">
<div className="flex items-center justify-between">
<h2 className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
Annotations
</h2>
<div className="flex items-center gap-2">
<span className="text-[10px] font-mono bg-muted px-1.5 py-0.5 rounded text-muted-foreground">
{totalCount}
</span>
{isMobile && onClose && (
<button
onClick={onClose}
className="p-1 rounded-md text-muted-foreground hover:text-foreground hover:bg-muted transition-colors"
title="Close panel"
>
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
)}
</div>
</div>
{otherFileAnnotations && otherFileAnnotations.count > 0 && (
<button
onClick={onOtherFileAnnotationsClick}
className="mt-1.5 text-[10px] text-primary/70 hover:text-primary transition-colors cursor-pointer"
title="Show annotated files in sidebar"
>
+{otherFileAnnotations.count} in {otherFileAnnotations.files} other file{otherFileAnnotations.files === 1 ? '' : 's'}
</button>
)}
</div>
{/* List */}
<OverlayScrollArea className="flex-1 min-h-0">
<div ref={listRef} className="p-2 space-y-1.5">
{totalCount === 0 ? (
<div className="flex flex-col items-center justify-center h-40 text-center px-4">
<div className="w-10 h-10 rounded-full bg-muted/50 flex items-center justify-center mb-3">
<svg className="w-5 h-5 text-muted-foreground" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={1.5}>
<path strokeLinecap="round" strokeLinejoin="round" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
</svg>
</div>
<p className="text-xs text-muted-foreground">
Select text to add annotations
</p>
</div>
) : (
<>
{sortedAnnotations.map(ann => (
<AnnotationCard
key={ann.id}
annotation={ann}
isSelected={selectedId === ann.id}
onSelect={() => onSelect(ann.id)}
onDelete={() => onDelete(ann.id)}
onEdit={onEdit ? (updates: Partial<Annotation>) => onEdit(ann.id, updates) : undefined}
/>
))}
{editorAnnotations && editorAnnotations.length > 0 && (
<>
{sortedAnnotations.length > 0 && (
<div className="flex items-center gap-2 pt-2 pb-1">
<div className="flex-1 border-t border-border/30" />
<span className="text-[9px] font-semibold uppercase tracking-wider text-muted-foreground/60">Editor</span>
<div className="flex-1 border-t border-border/30" />
</div>
)}
{editorAnnotations.map(ann => (
<EditorAnnotationCard
key={ann.id}
annotation={ann}
onDelete={() => onDeleteEditorAnnotation?.(ann.id)}
/>
))}
</>
)}
</>
)}
</div>
</OverlayScrollArea>
{/* Quick Actions Footer */}
{totalCount > 0 && (
<div className="p-2 border-t border-border/50 flex gap-1.5">
{onQuickCopy && (
<button
onClick={async () => {
await onQuickCopy();
setCopiedText(true);
setTimeout(() => setCopiedText(false), 2000);
}}
className="flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-xs font-medium transition-all text-muted-foreground hover:text-foreground hover:bg-muted/50"
>
{copiedText ? (
<>
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
</svg>
Copied
</>
) : (
<>
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
</svg>
Copy
</>
)}
</button>
)}
{sharingEnabled && onShare && (
<button
onClick={onShare}
className="flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-xs font-medium transition-all text-muted-foreground hover:text-foreground hover:bg-muted/50"
>
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1" />
</svg>
Share
</button>
)}
</div>
)}
</aside>
);
if (isMobile) {
return (
<>
<div
className="fixed inset-0 z-[59] bg-background/60 backdrop-blur-sm"
onClick={onClose}
/>
{panel}
</>
);
}
return panel;
};
function formatTimestamp(ts: number): string {
const now = Date.now();
const diff = now - ts;
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (seconds < 60) return 'now';
if (minutes < 60) return `${minutes}m`;
if (hours < 24) return `${hours}h`;
if (days < 7) return `${days}d`;
return new Date(ts).toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
}
const AnnotationCard: React.FC<{
annotation: Annotation;
isSelected: boolean;
onSelect: () => void;
onDelete: () => void;
onEdit?: (updates: Partial<Annotation>) => void;
}> = ({ annotation, isSelected, onSelect, onDelete, onEdit }) => {
const [isEditing, setIsEditing] = useState(false);
const [editText, setEditText] = useState(annotation.text || '');
const textareaRef = useRef<HTMLTextAreaElement>(null);
useEffect(() => {
if (isEditing && textareaRef.current) {
textareaRef.current.focus();
textareaRef.current.select();
}
}, [isEditing]);
// Update editText when annotation.text changes
useEffect(() => {
if (!isEditing) {
setEditText(annotation.text || '');
}
}, [annotation.text, isEditing]);
const handleStartEdit = (e: React.MouseEvent) => {
e.stopPropagation();
setEditText(annotation.text || '');
setIsEditing(true);
};
const handleSaveEdit = () => {
if (onEdit) {
onEdit({ text: editText });
}
setIsEditing(false);
};
const handleCancelEdit = () => {
setEditText(annotation.text || '');
setIsEditing(false);
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && !e.nativeEvent.isComposing) {
e.preventDefault();
handleSaveEdit();
} else if (e.key === 'Escape') {
e.preventDefault();
handleCancelEdit();
}
};
const typeConfig = {
[AnnotationType.DELETION]: {
label: 'Delete',
color: 'text-destructive',
bg: 'bg-destructive/10',
icon: (
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
)
},
[AnnotationType.COMMENT]: {
label: 'Comment',
color: 'text-accent',
bg: 'bg-accent/10',
icon: (
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
</svg>
)
},
[AnnotationType.GLOBAL_COMMENT]: {
label: 'Global',
color: 'text-secondary',
bg: 'bg-secondary/10',
icon: (
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M12 21a9.004 9.004 0 008.716-6.747M12 21a9.004 9.004 0 01-8.716-6.747M12 21c2.485 0 4.5-4.03 4.5-9S14.485 3 12 3m0 18c-2.485 0-4.5-4.03-4.5-9S9.515 3 12 3m0 0a8.997 8.997 0 017.843 4.582M12 3a8.997 8.997 0 00-7.843 4.582m15.686 0A11.953 11.953 0 0112 10.5c-2.998 0-5.74-1.1-7.843-2.918m15.686 0A8.959 8.959 0 0121 12c0 .778-.099 1.533-.284 2.253m0 0A17.919 17.919 0 0112 16.5c-3.162 0-6.133-.815-8.716-2.247m0 0A9.015 9.015 0 013 12c0-1.605.42-3.113 1.157-4.418" />
</svg>
)
}
};
// Fallback for unknown types (forward compatibility)
const config = typeConfig[annotation.type] || {
label: 'Note',
color: 'text-muted-foreground',
bg: 'bg-muted/50',
icon: (
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
)
};
return (
<div
data-annotation-id={annotation.id}
onClick={onSelect}
className={`
group relative p-2.5 rounded-lg border cursor-pointer transition-all
${isSelected
? 'bg-primary/5 border-primary/30 shadow-sm'
: 'border-transparent hover:bg-muted/50 hover:border-border/50'
}
`}
>
{/* Author */}
{annotation.author && (
<div className={`flex items-center gap-1.5 text-[10px] font-mono truncate mb-1.5 ${isCurrentUser(annotation.author) ? 'text-muted-foreground/60' : 'text-muted-foreground'}`}>
<svg className="w-3 h-3 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
<span className="truncate">{annotation.author}{isCurrentUser(annotation.author) && ' (me)'}</span>
</div>
)}
{/* Type Badge + Timestamp + Actions */}
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
<div className={`flex items-center gap-1.5 ${config.color}`}>
<span className={`p-1 rounded ${config.bg}`}>
{config.icon}
</span>
<span className="text-[10px] font-semibold uppercase tracking-wide">
{config.label}
</span>
</div>
{annotation.diffContext && (
<span className="text-[9px] px-1.5 py-0.5 rounded font-medium bg-muted text-muted-foreground">
diff
</span>
)}
<span className="text-[10px] text-muted-foreground/50">
{formatTimestamp(annotation.createdA)}
</span>
</div>
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100 transition-all">
{onEdit && annotation.type !== AnnotationType.DELETION && !isEditing && (
<button
onClick={handleStartEdit}
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground transition-all"
title="Edit annotation"
>
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
</button>
)}
<button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); onDelete(); }}
className="p-1 rounded hover:bg-destructive/10 text-muted-foreground hover:text-destructive transition-all"
title="Delete annotation"
>
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
{/* Global Comment - show text directly */}
{annotation.type === AnnotationType.GLOBAL_COMMENT ? (
isEditing ? (
<div className="space-y-2">
<textarea
ref={textareaRef}
value={editText}
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setEditText(e.target.value)}
onKeyDown={handleKeyDown}
onClick={(e: React.MouseEvent<HTMLTextAreaElement>) => e.stopPropagation()}
className="w-full text-xs text-foreground/90 pl-2 border-l-2 border-purple-500/50 bg-background border border-border rounded px-2 py-1.5 resize-none focus:outline-none focus:ring-2 focus:ring-primary/50"
rows={Math.min(editText.split('\n').length + 1, 8)}
/>
<div className="flex items-center gap-2 text-[10px] text-muted-foreground">
<span>Press Cmd+Enter to save, Esc to cancel</span>
</div>
<div className="flex items-center gap-2">
<button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); handleSaveEdit(); }}
className="px-2 py-1 text-[10px] font-medium rounded bg-primary text-primary-foreground hover:bg-primary/90 transition-colors"
>
Save
</button>
<button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); handleCancelEdit(); }}
className="px-2 py-1 text-[10px] font-medium rounded bg-muted text-muted-foreground hover:bg-muted/80 transition-colors"
>
Cancel
</button>
</div>
</div>
) : (
<div className="text-xs text-foreground/90 pl-2 border-l-2 border-purple-500/50 whitespace-pre-wrap">
{annotation.text}
</div>
)
) : (
<>
{/* Original Text */}
<div className="text-[11px] font-mono text-muted-foreground bg-muted/50 rounded px-2 py-1.5 whitespace-pre-wrap max-h-24 overflow-y-auto">
"{annotation.originalText}"
</div>
{/* Comment/Replacement Text */}
{annotation.type !== AnnotationType.DELETION && (
isEditing ? (
<div className="mt-2 space-y-2">
<textarea
ref={textareaRef}
value={editText}
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setEditText(e.target.value)}
onKeyDown={handleKeyDown}
onClick={(e: React.MouseEvent<HTMLTextAreaElement>) => e.stopPropagation()}
className="w-full text-xs text-foreground/90 pl-2 border-l-2 border-primary/50 bg-background border border-border rounded px-2 py-1.5 resize-none focus:outline-none focus:ring-2 focus:ring-primary/50"
rows={Math.min(editText.split('\n').length + 1, 8)}
/>
<div className="flex items-center gap-2 text-[10px] text-muted-foreground">
<span>Press Cmd+Enter to save, Esc to cancel</span>
</div>
<div className="flex items-center gap-2">
<button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); handleSaveEdit(); }}
className="px-2 py-1 text-[10px] font-medium rounded bg-primary text-primary-foreground hover:bg-primary/90 transition-colors"
>
Save
</button>
<button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); handleCancelEdit(); }}
className="px-2 py-1 text-[10px] font-medium rounded bg-muted text-muted-foreground hover:bg-muted/80 transition-colors"
>
Cancel
</button>
</div>
</div>
) : (
annotation.text && (
<div className="mt-2 text-xs text-foreground/90 pl-2 border-l-2 border-primary/50 whitespace-pre-wrap">
{annotation.text}
</div>
)
)
)}
</>
)}
{/* Attached Images */}
{annotation.images && annotation.images.length > 0 && (
<div className="mt-2 flex flex-wrap gap-1.5">
{annotation.images.map((img, idx) => (
<div key={idx} className="text-center">
<ImageThumbnail
path={img.path}
size="sm"
showRemove={false}
/>
<div className="text-[9px] text-muted-foreground truncate max-w-[3rem]" title={img.name}>{img.name}</div>
</div>
))}
</div>
)}
</div>
);
};