import React, { useState } from 'react'; /** * Get the display URL for an image path or URL */ export const getImageSrc = (path: string, base?: string): string => { if (path.startsWith('http://') || path.startsWith('https://')) { return path; // Remote URL, use directly } let url = `/api/image?path=${encodeURIComponent(path)}`; if (base && !path.startsWith('/')) { url += `&base=${encodeURIComponent(base)}`; } return url; }; interface ImageThumbnailProps { path: string; size?: 'sm' | 'md' | 'lg'; onClick?: () => void; onRemove?: () => void; showRemove?: boolean; className?: string; } const SIZES = { sm: 'w-6 h-6', md: 'w-10 h-10', lg: 'w-16 h-16', }; export const ImageThumbnail: React.FC = ({ path, size = 'md', onClick, onRemove, showRemove = true, className = '', }) => { const [error, setError] = useState(false); const [loading, setLoading] = useState(true); const sizeClass = SIZES[size]; return (
{loading && !error && (
)} {error ? (
) : ( Attachment setLoading(false)} onError={() => { setError(true); setLoading(false); }} className={`${sizeClass} rounded object-cover border border-border ${onClick ? 'cursor-pointer hover:opacity-80' : ''}`} /> )} {/* Remove button */} {showRemove && onRemove && ( )}
); };