'use client'; import { useEffect, useState } from 'react'; import Link from 'next/link'; import { urlToFile } from '@/app/lib/url-to-file'; type SavedPhoto = { orderId: string; slot: number; name: string }; export function SavedPhotos({ onAdd, disabled = false }: { onAdd?: (file: File, name: string) => void; disabled?: boolean; }) { const [photos, setPhotos] = useState(null); const [error, setError] = useState(''); const [loadingPhoto, setLoadingPhoto] = useState(''); const [added, setAdded] = useState([]); useEffect(() => { const controller = new AbortController(); fetch('/api/orders/photos', { signal: controller.signal }) .then(async response => { if (!response.ok) throw new Error(response.status === 401 ? 'Sign in to use your saved photos.' : 'Could not load saved photos.'); const data = await response.json() as { photos: SavedPhoto[] }; setPhotos(data.photos); }) .catch(error => { if (!controller.signal.aborted) setError(error.message); }); return () => controller.abort(); }, []); const add = async (photo: SavedPhoto, url: string, id: string) => { setLoadingPhoto(id); setError(''); try { const file = await urlToFile(url, `saved-photo-${photo.slot + 1}.jpg`); onAdd?.(file, photo.name); setAdded(previous => [...previous, id]); } catch { setError('Could not add this photo. Try again.'); } finally { setLoadingPhoto(''); } }; return
{error &&

{error} {photos === null && Sign in}

} {photos === null && !error &&

Loading saved photos…

} {photos?.length === 0 &&

Photos from cancelled commissions will appear here.

} {photos && photos.length > 0 &&
    {photos.map((photo, index) => { const url = `/api/orders/photos?order=${encodeURIComponent(photo.orderId)}&slot=${photo.slot}`; const id = `${photo.orderId}:${photo.slot}`; return
  • {/* eslint-disable-next-line @next/next/no-img-element */} {photo.name {photo.name && {photo.name}} {onAdd ? : Use in a new order}
  • ; })}
}
; }