forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscriptView.tsx
More file actions
424 lines (390 loc) · 14.4 KB
/
Copy pathTranscriptView.tsx
File metadata and controls
424 lines (390 loc) · 14.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
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
'use client';
import { useMemo, useRef, useEffect, useState, useCallback } from 'react';
import { Tag, HelpCircle, Play, Pencil, Check, Loader2 } from 'lucide-react';
import { cn } from '@/lib/utils';
import type { TranscriptSegment } from '@/types/conversation';
import type { Person } from '@/types/user';
interface TranscriptViewProps {
segments: TranscriptSegment[];
userName?: string;
conversationId?: string;
people?: Person[];
editable?: boolean;
onSpeakerClick?: (segment: TranscriptSegment) => void;
/**
* Queue an edit to a single segment's text. Fire-and-forget: the parent shows
* the change optimistically and persists it (serialized). When omitted, text
* editing is disabled and only the speaker remains editable.
*/
onSegmentTextChange?: (segmentId: string, text: string) => void;
/** Id of the segment whose save is currently in flight (shows a spinner). */
savingSegmentId?: string | null;
/** When true, text editing is suppressed (e.g. while reprocessing). */
editingDisabled?: boolean;
/** Current audio playback time in seconds */
currentTime?: number;
/** Whether audio is available for this conversation */
hasAudio?: boolean;
/** Callback when user clicks on a segment to seek to that time */
onSeekTo?: (time: number) => void;
}
/**
* Format seconds to MM:SS
*/
function formatTimestamp(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
/**
* Get speaker display name
*/
function getSpeakerName(
segment: TranscriptSegment,
userName?: string,
people?: Person[],
): { name: string; isTagged: boolean } {
// Check if has person_id and find the person
if (segment.person_id && people) {
const person = people.find((p) => p.id === segment.person_id);
if (person) {
return { name: person.name, isTagged: true };
}
}
// Check if segment has a pre-populated speaker_name
if (segment.speaker_name) {
return { name: segment.speaker_name, isTagged: true };
}
// Check if it's the user
if (segment.is_user) {
return { name: userName || 'You', isTagged: true };
}
// Default: untagged speaker
return { name: `Speaker ${(segment.speaker_id ?? 0) + 1}`, isTagged: false };
}
// Speaker avatar colors matching mobile app
const SPEAKER_COLORS = [
'bg-white/[0.14] text-text-primary', // user
'bg-amber-700/30 text-amber-300',
'bg-blue-900/30 text-blue-300',
'bg-emerald-800/30 text-emerald-300',
'bg-rose-900/30 text-rose-300',
'bg-cyan-700/30 text-cyan-300',
'bg-lime-800/30 text-lime-300',
'bg-white/[0.14] text-text-secondary',
'bg-orange-800/30 text-orange-300',
];
/**
* Group consecutive segments by speaker for cleaner display
*/
function groupSegmentsBySpeaker(segments: TranscriptSegment[]): TranscriptSegment[][] {
const groups: TranscriptSegment[][] = [];
let currentGroup: TranscriptSegment[] = [];
let currentSpeaker: number | null = null;
for (const segment of segments) {
const speakerId = segment.speaker_id ?? null;
if (speakerId !== currentSpeaker) {
if (currentGroup.length > 0) {
groups.push(currentGroup);
}
currentGroup = [segment];
currentSpeaker = speakerId;
} else {
currentGroup.push(segment);
}
}
if (currentGroup.length > 0) {
groups.push(currentGroup);
}
return groups;
}
/**
* Check if a time falls within a segment group's range
*/
function isActiveGroup(
group: TranscriptSegment[],
currentTime: number | undefined,
): boolean {
if (currentTime === undefined) return false;
const firstSegment = group[0];
const lastSegment = group[group.length - 1];
return currentTime >= firstSegment.start && currentTime <= lastSegment.end;
}
/**
* Inline editor for a single transcript segment's text.
*
* The transcript is displayed as merged same-speaker blocks, but the backend
* edits one segment at a time (keyed by `segment.id`). So in edit mode each
* underlying segment gets its own field — the edit granularity matches the API.
* Mirrors the `EditableTitle` interaction: Enter / blur saves, Escape cancels.
*/
function SegmentTextEditor({
segment,
onEnqueueSave,
onExitEdit,
}: {
segment: TranscriptSegment;
onEnqueueSave: (segmentId: string, text: string) => void;
onExitEdit: () => void;
}) {
const [draft, setDraft] = useState(segment.text);
// Guard so Enter/Done (which close the editor) don't also commit a second time
// via the blur that firing focus-loss triggers as the field unmounts.
const committedRef = useRef(false);
const canEdit = Boolean(segment.id);
// Queue the edit (if changed) and close. The parent persists it serially and
// owns the saving/error UI, so the editor stays intentionally lightweight.
const commit = useCallback(() => {
if (committedRef.current) return;
committedRef.current = true;
const trimmed = draft.trim();
if (segment.id && trimmed && trimmed !== segment.text) {
onEnqueueSave(segment.id, trimmed);
}
onExitEdit();
}, [draft, segment.id, segment.text, onEnqueueSave, onExitEdit]);
const cancel = useCallback(() => {
committedRef.current = true; // stop the blur handler from committing
onExitEdit();
}, [onExitEdit]);
const handleKeyDown = (e: React.KeyboardEvent) => {
// Enter commits & closes; Shift+Enter inserts a newline; Escape cancels.
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
commit();
} else if (e.key === 'Escape') {
e.preventDefault();
cancel();
}
};
if (!canEdit) {
// Segments without an id cannot be targeted by the edit API.
return (
<p
className="text-text-primary leading-relaxed opacity-60"
title="This segment can't be edited"
>
{segment.text}
</p>
);
}
return (
<div className="space-y-1">
<textarea
value={draft}
onChange={(e) => setDraft(e.target.value)}
onKeyDown={handleKeyDown}
onBlur={commit}
rows={Math.min(6, Math.max(2, Math.ceil(draft.length / 60)))}
className={cn(
'w-full resize-y rounded-lg px-3 py-2 text-sm leading-relaxed',
'bg-bg-secondary border border-bg-quaternary text-text-primary',
'outline-none focus:ring-2 focus:ring-white/20',
)}
/>
<div className="text-xs text-text-quaternary">Enter to save · Esc to cancel</div>
</div>
);
}
export function TranscriptView({
segments,
userName,
conversationId,
people,
editable = false,
onSpeakerClick,
onSegmentTextChange,
savingSegmentId,
editingDisabled = false,
currentTime,
hasAudio = false,
onSeekTo,
}: TranscriptViewProps) {
const groupedSegments = useMemo(() => groupSegmentsBySpeaker(segments), [segments]);
const activeGroupRef = useRef<HTMLDivElement>(null);
const [editingGroupIndex, setEditingGroupIndex] = useState<number | null>(null);
const textEditable =
editable &&
Boolean(conversationId) &&
Boolean(onSegmentTextChange) &&
!editingDisabled;
// Close any open editor if text editing gets disabled (e.g. reprocess starts).
useEffect(() => {
if (editingDisabled) setEditingGroupIndex(null);
}, [editingDisabled]);
// Auto-scroll to keep active segment in view during playback
useEffect(() => {
if (activeGroupRef.current && currentTime !== undefined) {
activeGroupRef.current.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}
}, [currentTime]);
// Find the active group index for highlighting
const activeGroupIndex = useMemo(() => {
if (currentTime === undefined) return -1;
return groupedSegments.findIndex((group) => isActiveGroup(group, currentTime));
}, [groupedSegments, currentTime]);
if (!segments || segments.length === 0) {
return (
<div className="text-center py-8 text-text-tertiary">No transcript available</div>
);
}
const handleSpeakerClick = (segment: TranscriptSegment) => {
if (!editable || !conversationId || !onSpeakerClick) return;
onSpeakerClick(segment);
};
return (
<div className="space-y-4">
{groupedSegments.map((group, groupIndex) => {
const firstSegment = group[0];
const lastSegment = group[group.length - 1];
const { name: speakerName, isTagged } = getSpeakerName(
firstSegment,
userName,
people,
);
const isUser = firstSegment.is_user;
const combinedText = group.map((s) => s.text).join(' ');
const speakerColorIndex = isUser
? 0
: ((firstSegment.speaker_id ?? 0) % (SPEAKER_COLORS.length - 1)) + 1;
const isActive = groupIndex === activeGroupIndex;
const isEditingGroup = editingGroupIndex === groupIndex;
const isSavingGroup =
savingSegmentId != null && group.some((s) => s.id === savingSegmentId);
return (
<div
key={groupIndex}
ref={isActive ? activeGroupRef : undefined}
className={cn(
'group/segment rounded-xl p-4 transition-all duration-200',
isUser ? 'bg-white/[0.08] border border-white/25' : 'bg-bg-tertiary',
// Active segment highlighting during audio playback
isActive && 'ring-2 ring-white/25 bg-white/[0.08]',
)}
>
{/* Speaker header */}
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
{/* Avatar */}
<div
className={cn(
'w-6 h-6 rounded-full flex items-center justify-center text-xs font-medium',
SPEAKER_COLORS[speakerColorIndex],
)}
>
{speakerName.charAt(0).toUpperCase()}
</div>
{/* Speaker name - clickable when editable */}
{editable && conversationId && onSpeakerClick ? (
<button
onClick={() => handleSpeakerClick(firstSegment)}
className={cn(
'flex items-center gap-1.5 text-sm font-medium',
'hover:underline underline-offset-2',
isUser
? 'text-text-primary'
: isTagged
? 'text-text-secondary'
: 'text-text-tertiary',
)}
>
<span>{speakerName}</span>
{!isTagged && <HelpCircle className="w-3.5 h-3.5 text-warning" />}
{!isUser && <Tag className="w-3 h-3 opacity-50" />}
</button>
) : (
<span
className={cn(
'text-sm font-medium',
isUser ? 'text-text-primary' : 'text-text-secondary',
)}
>
{speakerName}
</span>
)}
</div>
<div className="flex items-center gap-2">
{/* Timestamp - clickable when audio is available */}
{hasAudio && onSeekTo ? (
<button
onClick={() => onSeekTo(firstSegment.start)}
className={cn(
'flex items-center gap-1.5 text-xs',
'text-text-quaternary hover:text-text-primary transition-colors',
'group',
)}
title="Click to play from here"
>
<Play className="w-3 h-3 opacity-0 group-hover:opacity-100 transition-opacity" />
<span>
{formatTimestamp(firstSegment.start)} -{' '}
{formatTimestamp(lastSegment.end)}
</span>
</button>
) : (
<span className="text-xs text-text-quaternary">
{formatTimestamp(firstSegment.start)} -{' '}
{formatTimestamp(lastSegment.end)}
</span>
)}
{/* Per-segment saving indicator (this block's edit is in flight) */}
{isSavingGroup && (
<span className="flex items-center gap-1 text-xs text-text-quaternary">
<Loader2 className="w-3.5 h-3.5 animate-spin" />
<span>Saving…</span>
</span>
)}
{/* Edit transcript text toggle */}
{textEditable &&
(isEditingGroup ? (
<button
onClick={() => setEditingGroupIndex(null)}
className={cn(
'flex items-center gap-1 text-xs font-medium',
'text-text-secondary hover:text-text-primary transition-colors',
)}
title="Done editing"
>
<Check className="w-3.5 h-3.5" />
<span>Done</span>
</button>
) : (
<button
onClick={() => setEditingGroupIndex(groupIndex)}
className={cn(
'p-1 rounded-md text-text-quaternary',
'opacity-0 group-hover/segment:opacity-100 focus:opacity-100',
'hover:text-text-primary hover:bg-bg-quaternary/50 transition-all',
)}
title="Edit transcript text"
aria-label="Edit transcript text"
>
<Pencil className="w-3.5 h-3.5" />
</button>
))}
</div>
</div>
{/* Transcript text */}
{isEditingGroup && onSegmentTextChange ? (
<div className="space-y-2">
{group.map((segment, i) => (
<SegmentTextEditor
key={segment.id ?? `${groupIndex}-${i}`}
segment={segment}
onEnqueueSave={onSegmentTextChange}
onExitEdit={() => setEditingGroupIndex(null)}
/>
))}
</div>
) : (
<p className="text-text-primary leading-relaxed">{combinedText}</p>
)}
</div>
);
})}
</div>
);
}