Spaces:
Sleeping
Sleeping
File size: 13,468 Bytes
256cef9 |
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 |
import React, { useState, useEffect } from 'react';
import { TaskSegment, Interaction, InteractionType } from '../types';
import { MousePointerClick, Type, Pencil } from './Icons';
type HighlightPoint = { x: number; y: number; isEditing: boolean } | null;
type CoordinatePickerCallback = ((coords: { x: number; y: number }) => void) | null;
const interactionIcons: Record<InteractionType, React.ReactNode> = {
click: <MousePointerClick className="w-4 h-4 text-sky-400" />,
type: <Type className="w-4 h-4 text-lime-400" />,
};
interface TaskSegmentCardProps {
task: TaskSegment;
videoDuration: number;
totalFrames: number;
onSeekToTime?: (time: number) => void;
onUpdateInteraction: (taskId: number, interactionIndex: number, updatedInteraction: Interaction) => void;
onHighlightPoint: (point: HighlightPoint) => void;
onSetCoordinatePicker: (callback: CoordinatePickerCallback) => void;
}
const formatTime = (seconds: number) => {
if (isNaN(seconds) || seconds < 0) return '0.0s';
return `${seconds.toFixed(1)}s`;
};
const formatCoords = (x?: number, y?: number) => {
if (x === undefined || y === undefined) return '';
return `(x: ${x.toFixed(2)}, y: ${y.toFixed(2)})`;
}
export const TaskSegmentCard: React.FC<TaskSegmentCardProps> = ({ task, videoDuration, totalFrames, onSeekToTime, onUpdateInteraction, onHighlightPoint, onSetCoordinatePicker }) => {
const [editingIndex, setEditingIndex] = useState<number | null>(null);
const [editingInteractionType, setEditingInteractionType] = useState<InteractionType | null>(null);
const [editDetails, setEditDetails] = useState('');
const [editTime, setEditTime] = useState('');
const [editX, setEditX] = useState('');
const [editY, setEditY] = useState('');
// Live seek video when editing timestamp
useEffect(() => {
if (editingIndex !== null && onSeekToTime) {
const timeInSeconds = parseFloat(editTime);
if (!isNaN(timeInSeconds) && timeInSeconds >= 0 && timeInSeconds <= videoDuration) {
onSeekToTime(timeInSeconds);
}
}
}, [editTime, editingIndex, onSeekToTime, videoDuration]);
// Live update highlight marker when editing coordinates
useEffect(() => {
if (editingInteractionType === 'click' && editingIndex !== null) {
const x = parseFloat(editX);
const y = parseFloat(editY);
if (!isNaN(x) && !isNaN(y)) {
onHighlightPoint({ x, y, isEditing: true });
}
}
}, [editX, editY, editingInteractionType, editingIndex, onHighlightPoint]);
const calculateTime = (frameIndex: number): number | null => {
if (!videoDuration || !totalFrames || videoDuration === 0 || totalFrames === 0) return null;
return (frameIndex / totalFrames) * videoDuration;
};
const handleInteractionClick = (interaction: Interaction) => {
const time = calculateTime(interaction.frameIndex);
if (time !== null && onSeekToTime) {
onSeekToTime(time);
}
};
const handleEditClick = (interaction: Interaction, index: number) => {
setEditingIndex(index);
setEditingInteractionType(interaction.type);
setEditDetails(interaction.details);
const time = calculateTime(interaction.frameIndex);
setEditTime(time !== null ? time.toFixed(1) : '');
if (interaction.type === 'click') {
setEditX(interaction.x?.toFixed(4) || '0.5');
setEditY(interaction.y?.toFixed(4) || '0.5');
onSetCoordinatePicker((coords) => {
setEditX(coords.x.toFixed(4));
setEditY(coords.y.toFixed(4));
});
}
};
const handleCancelEdit = () => {
setEditingIndex(null);
setEditingInteractionType(null);
setEditDetails('');
setEditTime('');
setEditX('');
setEditY('');
onHighlightPoint(null);
onSetCoordinatePicker(null);
};
const handleSaveEdit = () => {
if (editingIndex === null) return;
const timeInSeconds = parseFloat(editTime);
if (isNaN(timeInSeconds) || !videoDuration || !totalFrames) {
console.error("Cannot save edit due to invalid time or video data.");
return;
}
const newFrameIndex = Math.round((timeInSeconds / videoDuration) * totalFrames);
const originalInteraction = task.interactions[editingIndex];
const updatedInteraction: Interaction = {
...originalInteraction,
details: editDetails,
frameIndex: newFrameIndex,
};
if (updatedInteraction.type === 'click') {
updatedInteraction.x = parseFloat(editX) || 0;
updatedInteraction.y = parseFloat(editY) || 0;
}
onUpdateInteraction(task.id, editingIndex, updatedInteraction);
handleCancelEdit();
};
const handleInteractionMouseEnter = (interaction: Interaction) => {
if (interaction.type === 'click' && interaction.x !== undefined && interaction.y !== undefined) {
onHighlightPoint({ x: interaction.x, y: interaction.y, isEditing: false });
}
};
const handleInteractionMouseLeave = () => {
onHighlightPoint(null);
};
const segmentStartTime = formatTime(calculateTime(task.startFrame) ?? 0);
const segmentEndTime = formatTime(calculateTime(task.endFrame) ?? 0);
return (
<div className="bg-slate-900/70 border border-slate-700 rounded-xl p-4 transition-all hover:border-slate-600">
<div className="flex items-start gap-4">
<div className="flex-shrink-0 bg-slate-700 text-indigo-300 font-bold text-sm w-8 h-8 flex items-center justify-center rounded-full">
{task.id}
</div>
<div className="flex-grow">
<div className="flex justify-between items-start gap-2">
<p className="font-semibold text-slate-200 pr-2">{task.description}</p>
{(calculateTime(task.startFrame) !== null) && (
<span className="text-xs font-mono text-slate-400 bg-slate-800 px-2 py-1 rounded-md whitespace-nowrap">
{segmentStartTime} - {segmentEndTime}
</span>
)}
</div>
{task.interactions && task.interactions.length > 0 && (
<div className="mt-3 space-y-1">
{task.interactions.map((interaction, index) => {
if (editingIndex === index) {
// EDITING VIEW
return (
<div key={index} className="bg-slate-800 p-3 -mx-3 rounded-lg ring-2 ring-indigo-500">
<div className="flex items-start gap-3">
<div className="flex-shrink-0 pt-1">
{interactionIcons[interaction.type] || <div className="w-4 h-4" />}
</div>
<div className="flex-grow space-y-3">
<div>
<label className="text-xs text-slate-400">Description</label>
<input
type="text"
value={editDetails}
onChange={(e) => setEditDetails(e.target.value)}
className="w-full bg-slate-900 border border-slate-600 rounded-md px-2 py-1 text-sm text-slate-200 focus:ring-1 focus:ring-indigo-400 focus:border-indigo-400"
/>
</div>
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
<div>
<label className="text-xs text-slate-400">Timestamp (s)</label>
<input
type="number"
step="0.1"
value={editTime}
onChange={(e) => setEditTime(e.target.value)}
className="w-full bg-slate-900 border border-slate-600 rounded-md px-2 py-1 text-sm text-slate-200 focus:ring-1 focus:ring-indigo-400 focus:border-indigo-400"
/>
</div>
{interaction.type === 'click' && (
<>
<div>
<label className="text-xs text-slate-400">Coord. X</label>
<input
type="number"
step="0.01"
value={editX}
onChange={(e) => setEditX(e.target.value)}
className="w-full bg-slate-900 border border-slate-600 rounded-md px-2 py-1 text-sm text-slate-200 focus:ring-1 focus:ring-indigo-400 focus:border-indigo-400"
/>
</div>
<div>
<label className="text-xs text-slate-400">Coord. Y</label>
<input
type="number"
step="0.01"
value={editY}
onChange={(e) => setEditY(e.target.value)}
className="w-full bg-slate-900 border border-slate-600 rounded-md px-2 py-1 text-sm text-slate-200 focus:ring-1 focus:ring-indigo-400 focus:border-indigo-400"
/>
</div>
</>
)}
</div>
{interaction.type === 'click' && (
<p className="text-xs text-slate-400 italic">Click on the video player to update coordinates.</p>
)}
</div>
</div>
<div className="flex justify-end items-center gap-2 mt-3">
<button onClick={handleCancelEdit} className="px-3 py-1 text-sm font-semibold text-slate-300 hover:bg-slate-700 rounded-md">Cancel</button>
<button onClick={handleSaveEdit} className="px-3 py-1 text-sm font-semibold text-white bg-indigo-600 hover:bg-indigo-500 rounded-md">Save</button>
</div>
</div>
);
}
// DISPLAY VIEW
const interactionTime = calculateTime(interaction.frameIndex);
const interactionCoords = formatCoords(interaction.x, interaction.y);
return (
<div
key={index}
onMouseEnter={() => handleInteractionMouseEnter(interaction)}
onMouseLeave={handleInteractionMouseLeave}
className="group flex items-start gap-3 p-1.5 -mx-1.5 rounded-md transition-colors hover:bg-slate-800/60"
>
<div
className="flex-shrink-0 pt-1 cursor-pointer"
onClick={() => handleInteractionClick(interaction)}
role="button"
tabIndex={0}
onKeyPress={(e) => (e.key === 'Enter' || e.key === ' ') && handleInteractionClick(interaction)}
>
{interactionIcons[interaction.type] || <div className="w-4 h-4" />}
</div>
<div
className="flex-grow text-sm text-slate-400 cursor-pointer"
onClick={() => handleInteractionClick(interaction)}
role="button"
>
<span>{interaction.details}</span>
{interaction.type === 'click' && interactionCoords && (
<span className="ml-2 font-mono text-xs text-cyan-400">{interactionCoords}</span>
)}
</div>
<div className="flex items-center gap-2">
{interactionTime !== null && (
<span className="text-xs font-mono text-slate-500 whitespace-nowrap hidden group-hover:inline">
(~{formatTime(interactionTime)})
</span>
)}
<button
onClick={() => handleEditClick(interaction, index)}
className="hidden group-hover:block p-1 text-slate-500 hover:text-slate-300 transition-colors"
aria-label="Edit interaction"
>
<Pencil className="w-3.5 h-3.5" />
</button>
</div>
</div>
);
})}
</div>
)}
</div>
</div>
</div>
);
};
|