File size: 9,889 Bytes
d02c2a6 | 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 | import React, { useEffect, useState, ReactNode, KeyboardEvent, useRef } from 'react';
import { FaTrashAlt } from 'react-icons/fa';
import { openConfirm } from './ConfirmModal';
import classNames from 'classnames';
import { apiClient } from '@/utils/api';
import AudioPlayer from './AudioPlayer';
import { isVideo, isAudio } from '@/utils/basic';
import useCaptionBatch, { setCachedCaption } from '@/hooks/useCaptionBatch';
interface DatasetImageCardProps {
imageUrl: string;
alt: string;
isAutoCaptioning: boolean;
children?: ReactNode;
className?: string;
onDelete?: () => void;
onImageClick?: () => void;
captionRefreshKey?: number;
observerRoot?: Element | null;
rootMargin?: string;
}
const DatasetImageCard: React.FC<DatasetImageCardProps> = ({
imageUrl,
alt,
isAutoCaptioning,
children,
className = '',
onDelete = () => {},
onImageClick,
captionRefreshKey = 0,
observerRoot = null,
rootMargin = '200px 0px',
}) => {
const [loaded, setLoaded] = useState<boolean>(false);
const [showAudioPlayer, setShowAudioPlayer] = useState(true);
const [pollTick, setPollTick] = useState(0);
const [blobUrl, setBlobUrl] = useState<string | null>(null);
const [isVisible, setIsVisible] = useState(false);
const cardRef = useRef<HTMLDivElement>(null);
const isItAVideo = isVideo(imageUrl);
const isItAudio = isAudio(imageUrl);
const isItImage = !isItAVideo && !isItAudio;
// Track actual viewport visibility — Virtuoso keeps a buffer of cards mounted
// outside the visible region, so we can't rely on mount/unmount alone.
useEffect(() => {
const el = cardRef.current;
if (!el) return;
const observer = new IntersectionObserver(
entries => {
for (const entry of entries) {
if (entry.target === el) {
setIsVisible(entry.isIntersecting);
}
}
},
{
root: observerRoot ?? null,
threshold: 0.01,
rootMargin,
},
);
observer.observe(el);
return () => observer.disconnect();
}, [observerRoot, rootMargin]);
// Drive image loads through fetch + AbortController so scrolling past actually
// cancels in-flight requests. Debounced 80ms so fast scroll-throughs never
// start a request.
useEffect(() => {
if (!isItImage) return;
if (!isVisible) return;
const controller = new AbortController();
let cancelled = false;
let objectUrl: string | null = null;
const timer = window.setTimeout(() => {
fetch(`/api/img/${encodeURIComponent(imageUrl)}`, { signal: controller.signal })
.then(r => {
if (!r.ok) throw new Error(`HTTP ${r.status}`);
return r.blob();
})
.then(blob => {
if (cancelled) return;
objectUrl = URL.createObjectURL(blob);
setBlobUrl(objectUrl);
setLoaded(true);
})
.catch(err => {
if (err?.name !== 'AbortError') console.error('Dataset image fetch failed:', err);
});
}, 80);
return () => {
cancelled = true;
clearTimeout(timer);
controller.abort();
if (objectUrl) URL.revokeObjectURL(objectUrl);
setBlobUrl(null);
setLoaded(false);
};
}, [imageUrl, isItImage, isVisible]);
const combinedRefreshKey = captionRefreshKey + pollTick;
const { caption: fetchedCaption, isLoaded: isCaptionLoaded } = useCaptionBatch(
isVisible ? imageUrl : null,
combinedRefreshKey,
);
const [caption, setCaption] = useState<string>('');
const [savedCaption, setSavedCaption] = useState<string>('');
const dirtyRef = useRef<boolean>(false);
// Sync from the fetched caption, but don't clobber unsaved local edits.
useEffect(() => {
if (!isCaptionLoaded) return;
if (dirtyRef.current) return;
setCaption(fetchedCaption);
setSavedCaption(fetchedCaption.trim());
}, [fetchedCaption, isCaptionLoaded]);
// Poll while auto-captioning so backend-written captions show up.
useEffect(() => {
if (!isAutoCaptioning) return;
const interval = setInterval(() => setPollTick(t => t + 1), 5000);
return () => clearInterval(interval);
}, [isAutoCaptioning]);
const saveCaption = () => {
const trimmedCaption = caption.trim();
if (trimmedCaption === savedCaption) {
dirtyRef.current = false;
return;
}
apiClient
.post('/api/img/caption', { imgPath: imageUrl, caption: trimmedCaption })
.then(() => {
setSavedCaption(trimmedCaption);
setCachedCaption(imageUrl, trimmedCaption);
dirtyRef.current = false;
})
.catch(error => {
console.error('Error saving caption:', error);
});
};
// Save any pending edit if the card unmounts (e.g. scrolled out of the virtualized window).
const latestRef = useRef({ caption, savedCaption, imageUrl });
useEffect(() => {
latestRef.current = { caption, savedCaption, imageUrl };
});
useEffect(() => {
return () => {
if (!dirtyRef.current) return;
const { caption: c, savedCaption: s, imageUrl: url } = latestRef.current;
const trimmed = c.trim();
if (trimmed === s) return;
apiClient
.post('/api/img/caption', { imgPath: url, caption: trimmed })
.then(() => setCachedCaption(url, trimmed))
.catch(err => console.error('Error saving caption on unmount:', err));
};
}, []);
const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>): void => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
saveCaption();
}
};
const handleCaptionChange = (value: string) => {
dirtyRef.current = value.trim() !== savedCaption;
setCaption(value);
};
const isCaptionCurrent = caption.trim() === savedCaption;
return (
<div ref={cardRef} className={`flex flex-col ${className}`}>
<div className="relative w-full" style={{ paddingBottom: '100%' }}>
<div
className={classNames('absolute inset-0 rounded-t-lg shadow-md bg-gray-900', {
'animate-pulse': isItImage && !loaded,
})}
>
{isItAVideo && (
<video
src={`/api/img/${encodeURIComponent(imageUrl)}`}
className={`w-full h-full object-contain`}
autoPlay={false}
loop
muted
controls
/>
)}
{isItAudio && !showAudioPlayer && (
<div
className="w-full h-full cursor-pointer flex items-center justify-center bg-gray-900"
onClick={() => setShowAudioPlayer(true)}
>
<img
src={`/api/audio/art/${encodeURIComponent(imageUrl)}`}
alt={alt}
className="w-full h-full object-contain"
onError={e => {
(e.target as HTMLImageElement).style.display = 'none';
}}
/>
</div>
)}
{isItAudio && showAudioPlayer && (
<AudioPlayer src={`/api/img/${encodeURIComponent(imageUrl)}`} title={imageUrl.replace(/^.*[\\/]/, '')} />
)}
{isItImage && blobUrl && (
<img
src={blobUrl}
alt={alt}
decoding="async"
onClick={onImageClick}
className={classNames('w-full h-full object-contain transition-opacity duration-300', {
'opacity-100': loaded,
'opacity-0': !loaded,
'cursor-zoom-in': !!onImageClick,
})}
/>
)}
{children && <div className="absolute inset-0 flex items-center justify-center">{children}</div>}
<div className="absolute top-1 right-1 flex space-x-2 z-10">
<button
className="bg-gray-800 rounded-full p-2"
onClick={() => {
openConfirm({
title: `Delete ${isItAVideo ? 'video' : 'image'}`,
message: `Are you sure you want to delete this ${isItAVideo ? 'video' : 'image'}? This action cannot be undone.`,
type: 'warning',
confirmText: 'Delete',
onConfirm: () => {
apiClient
.post('/api/img/delete', { imgPath: imageUrl })
.then(() => {
console.log('Image deleted:', imageUrl);
onDelete();
})
.catch(error => {
console.error('Error deleting image:', error);
});
},
});
}}
>
<FaTrashAlt />
</button>
</div>
</div>
</div>
<div
className={classNames('w-full p-2 bg-gray-800 text-white text-sm rounded-b-lg h-[75px]', {
'border-blue-500 border-2': !isCaptionCurrent,
'border-transparent border-2': isCaptionCurrent,
})}
>
{isCaptionLoaded ? (
<form
onSubmit={e => {
e.preventDefault();
saveCaption();
}}
onBlur={saveCaption}
>
<textarea
className={classNames('w-full bg-transparent resize-none outline-none focus:ring-0 focus:outline-none', {
'opacity-50 cursor-not-allowed': isAutoCaptioning,
})}
value={caption}
rows={3}
readOnly={isAutoCaptioning}
onChange={e => handleCaptionChange(e.target.value)}
onKeyDown={handleKeyDown}
/>
</form>
) : (
<div className="w-full h-full flex items-center justify-center text-gray-400">Loading caption...</div>
)}
</div>
</div>
);
};
export default DatasetImageCard;
|