import { useCallback, useEffect, useMemo, useState } from 'react'; import { createShare, getShare, pushProof, revokeShare } from '../lib/api'; import type { ShareState } from '../lib/api'; import { DEFAULT_EXPORT, renderPhotoBlob } from '../lib/export'; import type { ExportOptions } from '../lib/export'; import type { PhotoRecord } from '../types'; interface Props { event: { id: string; name: string }; photos: PhotoRecord[]; onClose: () => void; onToast: (message: string) => void; /** Update the local record after a rendered proof is accepted by the server. */ onProofPublished?: (photoId: string, thumbUrl: string, proofAt: number) => void; } const PROOF_EXPORT: ExportOptions = { ...DEFAULT_EXPORT, format: 'jpeg', quality: 92, resize: 'longEdge', longEdge: 2800, zip: false, metadata: false, outputSharpen: 8, watermark: { ...DEFAULT_EXPORT.watermark, enabled: false }, }; function proofBase64(blob: Blob): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const value = typeof reader.result === 'string' ? reader.result : ''; resolve(value.includes(',') ? value.slice(value.indexOf(',') + 1) : value); }; reader.onerror = () => reject(reader.error ?? new Error('Could not read the proof')); reader.readAsDataURL(blob); }); } /** * Owner-side control for a proofing link: turn it on, copy it, see who has * picked what, and revoke it. Picks live on the server keyed by the link, so * anyone with the URL can choose photos without an account. */ export default function ShareModal({ event, photos, onClose, onToast, onProofPublished }: Props) { const [share, setShare] = useState(null); const [busy, setBusy] = useState(false); const [publishing, setPublishing] = useState(false); const [published, setPublished] = useState(0); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); const refresh = useCallback(async () => { try { setShare(await getShare(event.id)); setError(null); } catch (err) { setError(err instanceof Error ? err.message : 'Could not read the proofing link'); } }, [event.id]); useEffect(() => { void refresh(); }, [refresh]); useEffect(() => { if (!share?.token) return; const timer = setInterval(() => void refresh(), 20000); return () => clearInterval(timer); }, [refresh, share?.token]); const url = useMemo(() => { if (!share?.token) return ''; return `${window.location.origin}/g/${share.token}`; }, [share]); const picksByPhoto = useMemo(() => { const map = new Map(); for (const pick of share?.picks ?? []) { const names = map.get(pick.photoId) ?? []; names.push(pick.clientName || 'Client'); map.set(pick.photoId, names); } return map; }, [share]); const names = useMemo(() => { const set = new Set(); for (const pick of share?.picks ?? []) set.add(pick.clientName || 'Client'); return [...set]; }, [share]); const namesByPhoto = useMemo(() => new Map(photos.map((p) => [p.id, p.name])), [photos]); const publishProofs = async () => { if (!photos.length || publishing) return 0; setPublishing(true); setPublished(0); setError(null); let done = 0; try { for (const photo of photos) { const blob = await renderPhotoBlob(photo, PROOF_EXPORT); const result = await pushProof(photo.id, await proofBase64(blob)); onProofPublished?.(photo.id, result.thumbUrl, result.proofAt); done += 1; setPublished(done); } onToast(`Published ${done} edited proof${done === 1 ? '' : 's'}`); return done; } catch (err) { setError(err instanceof Error ? err.message : 'Could not publish the edited proofs'); return done; } finally { setPublishing(false); } }; const enable = async () => { setBusy(true); setError(null); try { await createShare(event.id); await refresh(); // A gallery without rendered proofs would show the unedited originals. await publishProofs(); } catch (err) { setError(err instanceof Error ? err.message : 'Could not create the link'); } finally { setBusy(false); } }; const copy = async () => { if (!url) return; try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1800); } catch { // Clipboard needs permission; selecting the text still lets them copy it. setError('Could not copy automatically — select the link and copy it.'); } }; const revoke = async () => { if (!window.confirm('Turn off this proofing link? Anyone using it will lose access and its picks will be cleared.')) { return; } setBusy(true); try { await revokeShare(event.id); setShare({ token: null, picks: [] }); onToast('Proofing link turned off'); } catch (err) { setError(err instanceof Error ? err.message : 'Could not turn off the link'); } finally { setBusy(false); } }; return (
e.target === e.currentTarget && !busy && onClose()} >

Client proofing

Share “{event.name}” as a public gallery. Clients pick favourites without an account, and their choices show up here and in the library.

{error &&
{error}
} {!share ? (

Reading the link…

) : share.token ? ( <>
e.target.select()} />

Anyone with this link can view the {photos.length} photos in this event and pick favourites. Links can be turned off at any time.

{share.picks.length} picks
{names.length} {names.length === 1 ? 'client' : 'clients'}
{new Set(share.picks.map((p) => p.photoId)).size} photos chosen
Edited proofs

Generate the gallery from the current crop, grade and retouching. The originals and editable proxies stay untouched.

{(share.feedback ?? []).length > 0 && (
Client feedback
    {(share.feedback ?? []).slice(0, 40).map((item, index) => (
  • {namesByPhoto.get(item.photoId) ?? 'Photo'} {item.kind === 'approved' ? 'Approved' : item.kind === 'changes' ? 'Changes requested' : item.clientName || 'Client'} {item.body &&

    {item.body}

    } {item.kind === 'comment' && ( {item.clientName || 'Client'} )}
  • ))}
)} {share.picks.length ? (
Client picks
    {[...picksByPhoto.entries()] .sort((a, b) => b[1].length - a[1].length) .map(([photoId, who]) => (
  • {namesByPhoto.get(photoId) ?? 'Photo'} {who.join(', ')}
  • ))}
) : (

No picks yet. Client hearts appear here as they work through the gallery.

)}
Clears the gallery and its picks.
) : (

This event has no proofing link yet. Creating one lets a client review the shoot and mark favourites.

)}
); }