import React from 'react'; import { Link } from 'react-router-dom'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { getDeviceId } from '../lib/device'; import { usePollGalleryDelta } from '../polling/usePollGalleryDelta'; type Props = { slug: string }; export default function GalleryPreview({ slug }: Props) { const { photos, loading } = usePollGalleryDelta(slug); const [mode, setMode] = React.useState<'latest' | 'popular' | 'myphotos'>('latest'); const items = React.useMemo(() => { let arr = photos.slice(); // MyPhotos filter (requires session_id matching) if (mode === 'myphotos') { const deviceId = getDeviceId(); arr = arr.filter((photo: any) => photo.session_id === deviceId); } // Sorting if (mode === 'popular') { arr.sort((a: any, b: any) => (b.likes_count ?? 0) - (a.likes_count ?? 0)); } else { arr.sort((a: any, b: any) => new Date(b.created_at ?? 0).getTime() - new Date(a.created_at ?? 0).getTime()); } return arr.slice(0, 4); // 2x2 = 4 items }, [photos, mode]); // Helper function to generate photo title (must be before return) function getPhotoTitle(photo: any): string { if (photo.task_id) { return `Task: ${photo.task_title || 'Unbekannte Aufgabe'}`; } if (photo.emotion_id) { return `Emotion: ${photo.emotion_name || 'Gefühl'}`; } // Fallback based on creation time or placeholder const now = new Date(); const created = new Date(photo.created_at || now); const hours = created.getHours(); if (hours < 12) return 'Morgenmoment'; if (hours < 18) return 'Nachmittagslicht'; return 'Abendstimmung'; } return (
Alle ansehen →
{loading &&

Lädt…

} {!loading && items.length === 0 && ( Noch keine Fotos. Starte mit deinem ersten Upload! )}
{items.map((p: any) => (
{p.title {/* Photo Title */}
{p.title || getPhotoTitle(p)}
{p.likes_count > 0 && (
❤️ {p.likes_count}
)}
))}
); }