import React from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Image as ImageIcon, RefreshCcw, Search, Filter } from 'lucide-react'; import { YStack, XStack } from '@tamagui/stacks'; import { SizableText as Text } from '@tamagui/text'; import { Pressable } from '@tamagui/react-native-web-lite'; import { MobileShell } from './components/MobileShell'; import { MobileCard, PillBadge, CTAButton } from './components/Primitives'; import { getEventPhotos, updatePhotoVisibility, featurePhoto, unfeaturePhoto, TenantPhoto } from '../api'; import toast from 'react-hot-toast'; import { isAuthError } from '../auth/tokens'; import { getApiErrorMessage } from '../lib/apiError'; import { MobileSheet } from './components/Sheet'; import { useEventContext } from '../context/EventContext'; import { useTheme } from '@tamagui/core'; type FilterKey = 'all' | 'featured' | 'hidden'; export default function MobileEventPhotosPage() { const { slug: slugParam } = useParams<{ slug?: string }>(); const { activeEvent, selectEvent } = useEventContext(); const slug = slugParam ?? activeEvent?.slug ?? null; const navigate = useNavigate(); const { t } = useTranslation('management'); const [photos, setPhotos] = React.useState([]); const [filter, setFilter] = React.useState('all'); const [page, setPage] = React.useState(1); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); const [busyId, setBusyId] = React.useState(null); const [totalCount, setTotalCount] = React.useState(0); const [hasMore, setHasMore] = React.useState(false); const [search, setSearch] = React.useState(''); const [showFilters, setShowFilters] = React.useState(false); const [uploaderFilter, setUploaderFilter] = React.useState(''); const [onlyFeatured, setOnlyFeatured] = React.useState(false); const [onlyHidden, setOnlyHidden] = React.useState(false); const [lightbox, setLightbox] = React.useState(null); const theme = useTheme(); const text = String(theme.color?.val ?? '#111827'); const muted = String(theme.gray?.val ?? '#4b5563'); const border = String(theme.borderColor?.val ?? '#e5e7eb'); const infoBg = String(theme.blue3?.val ?? '#e8f1ff'); const infoBorder = String(theme.blue6?.val ?? '#bfdbfe'); const danger = String(theme.red10?.val ?? '#b91c1c'); const surface = String(theme.surface?.val ?? '#ffffff'); const backdrop = String(theme.gray12?.val ?? '#0f172a'); const baseInputStyle = React.useMemo( () => ({ width: '100%', height: 38, borderRadius: 10, border: `1px solid ${border}`, padding: '0 12px', fontSize: 13, background: surface, color: text, }), [border, surface, text], ); React.useEffect(() => { if (slugParam && activeEvent?.slug !== slugParam) { selectEvent(slugParam); } }, [slugParam, activeEvent?.slug, selectEvent]); const load = React.useCallback(async () => { if (!slug) return; setLoading(true); setError(null); try { const result = await getEventPhotos(slug, { page, perPage: 20, sort: 'desc', featured: filter === 'featured' || onlyFeatured, status: filter === 'hidden' || onlyHidden ? 'hidden' : undefined, search: search || undefined, }); setPhotos((prev) => (page === 1 ? result.photos : [...prev, ...result.photos])); setTotalCount(result.meta?.total ?? result.photos.length); const lastPage = result.meta?.last_page ?? 1; setHasMore(page < lastPage); } catch (err) { if (!isAuthError(err)) { setError(getApiErrorMessage(err, t('mobilePhotos.loadFailed', 'Fotos konnten nicht geladen werden.'))); } } finally { setLoading(false); } }, [slug, filter, t, page]); React.useEffect(() => { void load(); }, [load]); React.useEffect(() => { setPage(1); }, [filter, slug]); async function toggleVisibility(photo: TenantPhoto) { if (!slug) return; setBusyId(photo.id); try { const updated = await updatePhotoVisibility(slug, photo.id, photo.status === 'hidden'); setPhotos((prev) => prev.map((p) => (p.id === photo.id ? updated : p))); setLightbox((prev) => (prev && prev.id === photo.id ? updated : prev)); toast.success( updated.status === 'hidden' ? t('mobilePhotos.hideSuccess', 'Photo hidden') : t('mobilePhotos.showSuccess', 'Photo shown'), ); } catch (err) { if (!isAuthError(err)) { setError(getApiErrorMessage(err, t('mobilePhotos.visibilityFailed', 'Sichtbarkeit konnte nicht geändert werden.'))); toast.error(t('mobilePhotos.visibilityFailed', 'Sichtbarkeit konnte nicht geändert werden.')); } } finally { setBusyId(null); } } async function toggleFeature(photo: TenantPhoto) { if (!slug) return; setBusyId(photo.id); try { const updated = photo.is_featured ? await unfeaturePhoto(slug, photo.id) : await featurePhoto(slug, photo.id); setPhotos((prev) => prev.map((p) => (p.id === photo.id ? updated : p))); setLightbox((prev) => (prev && prev.id === photo.id ? updated : prev)); toast.success( updated.is_featured ? t('mobilePhotos.featureSuccess', 'Als Highlight markiert') : t('mobilePhotos.unfeatureSuccess', 'Highlight entfernt'), ); } catch (err) { if (!isAuthError(err)) { setError(getApiErrorMessage(err, t('mobilePhotos.featureFailed', 'Feature konnte nicht geändert werden.'))); toast.error(t('mobilePhotos.featureFailed', 'Feature konnte nicht geändert werden.')); } } finally { setBusyId(null); } } return ( navigate(-1)} headerActions={ setShowFilters(true)}> load()}> } > {error ? ( {error} ) : null} { setSearch(e.target.value); setPage(1); }} placeholder={t('photos.filters.search', 'Search uploads …')} style={{ ...baseInputStyle, marginBottom: 12 }} /> {(['all', 'featured', 'hidden'] as FilterKey[]).map((key) => ( setFilter(key)} style={{ flex: 1 }}> {key === 'all' ? t('common.all', 'All') : key === 'featured' ? t('photos.filters.featured', 'Featured') : t('photos.filters.hidden', 'Hidden')} ))} {loading ? ( {Array.from({ length: 4 }).map((_, idx) => ( ))} ) : photos.length === 0 ? ( {t('mobilePhotos.empty', 'No photos found.')} ) : ( {t('mobilePhotos.count', '{{count}} photos', { count: totalCount })}
{photos.map((photo) => ( setLightbox(photo)}> {photo.caption {photo.is_featured ? {t('photos.filters.featured', 'Featured')} : null} {photo.status === 'hidden' ? {t('photos.filters.hidden', 'Hidden')} : null} ))}
{hasMore ? ( setPage((prev) => prev + 1)} /> ) : null}
)} {lightbox ? (
{lightbox.caption {lightbox.uploader_name || t('events.members.roles.guest', 'Guest')} ❤️ {lightbox.likes_count ?? 0} toggleFeature(lightbox)} style={{ flex: 1, minWidth: 140 }} /> toggleVisibility(lightbox)} style={{ flex: 1, minWidth: 140 }} /> setLightbox(null)} />
) : null} setShowFilters(false)} title={t('mobilePhotos.filtersTitle', 'Filter')} footer={ { setPage(1); setShowFilters(false); void load(); }} /> } > setUploaderFilter(e.target.value)} placeholder={t('mobilePhotos.uploaderPlaceholder', 'Name or email')} style={baseInputStyle} /> { setUploaderFilter(''); setOnlyFeatured(false); setOnlyHidden(false); }} />
); } function Field({ label, color, children }: { label: string; color: string; children: React.ReactNode }) { return ( {label} {children} ); }