import { Upload } from 'lucide-react' import MediaUploadModal from './MediaUploadModal' import AuthImage from '../../components/AuthImage' import { useState, useEffect } from 'react' import { getServerUrl } from '../../hooks/getConstants' import { useAccount } from '../../contexts/useAccount' import { useNotifier } from '../../contexts/useNotifier' export default function MediaList({ albumId, albumName }) { const { getAccessToken } = useAccount() const [open, setOpen] = useState(false) const [media, setMedia] = useState([]) const [aspectRatios, setAspectRatios] = useState({}) const { showError } = useNotifier() useEffect(() => { let ignore = false; const getMedia = async () => { const response = await fetch(`${getServerUrl()}/api/media/getAllMediaInAlbum?albumId=${albumId}`, { method: 'GET', headers: { 'Authorization': getAccessToken(), }, }) const data = await response.json() if (ignore) return if (data.error) { showError(data.error) } else { setMedia(data.media) } } getMedia() return () => { ignore = true; } }, [albumId]) const handleImageLoad = (id, event) => { const { naturalWidth, naturalHeight } = event.target if (naturalWidth && naturalHeight) { setAspectRatios(prev => ({ ...prev, [id]: naturalWidth / naturalHeight })) } } return (

Media

setOpen(true)} />
{/* Media Grid */}
{media.length === 0 && (

No media in this album

)} {media.map((item) => { let ar = 1; // Try to get aspect ratio from metadata (new uploads) or fallback to loaded state (old uploads) if (item.Metadata && item.Metadata.width && item.Metadata.height) { ar = item.Metadata.width / item.Metadata.height; } else if (aspectRatios[item.ID]) { ar = aspectRatios[item.ID]; } return (
handleImageLoad(item.ID, e)} />
) })} {/* Spacer to prevent the last row from expanding to fill width if it has few items */}
) }