import { useRef, useState } from 'react' import { Camera, Image as ImageIcon, Loader2, X } from 'lucide-react' import type { AssetPhoto, PhotoType } from '../types' import { uploadAssetPhoto, deleteAssetPhoto, photoUrl } from '../api' // Camera/library capture split: one input with capture="environment" (opens the // device camera directly on mobile), one plain file input (opens the photo // library/file picker) — same split as hvac's DevicePhotoUpload. export default function AssetPhotoUpload({ assetId, photoType, photos, onChanged, canDelete }: { assetId: number photoType: PhotoType photos: AssetPhoto[] onChanged: () => void canDelete: boolean }) { const [uploading, setUploading] = useState(false) const [error, setError] = useState('') const [lightbox, setLightbox] = useState(null) const cameraInput = useRef(null) const libraryInput = useRef(null) const slotPhotos = photos.filter(p => p.photo_type === photoType) async function handleFile(file: File | undefined) { if (!file) return setUploading(true); setError('') try { await uploadAssetPhoto(assetId, file, photoType) onChanged() } catch (e) { setError(e instanceof Error ? e.message : 'Upload failed') } finally { setUploading(false) } } async function remove(id: number) { await deleteAssetPhoto(id).catch(() => {}) onChanged() } return (
{slotPhotos.map(p => (
setLightbox(photoUrl(p.file_path))} /> {canDelete && ( )}
))} {uploading && (
)}
{error &&
{error}
}
{ handleFile(e.target.files?.[0]); e.target.value = '' }} /> { handleFile(e.target.files?.[0]); e.target.value = '' }} />
{lightbox && (
setLightbox(null)}>
)}
) }