'use client'; import React, { useState, useMemo } from 'react'; import { Calendar, Star, ArrowUpDown, Clock, Trash2, Loader2, Users, GlassWater } from 'lucide-react'; import Link from 'next/link'; import { deleteTasting } from '@/services/delete-tasting'; interface Tasting { id: string; rating: number; nose_notes?: string; palate_notes?: string; finish_notes?: string; is_sample?: boolean; bottle_id: string; created_at: string; tasting_tags?: { buddies: { id: string; name: string; } }[]; tasting_sessions?: { id: string; name: string; }; user_id: string; } interface TastingListProps { initialTastings: Tasting[]; currentUserId?: string; } export default function TastingList({ initialTastings, currentUserId }: TastingListProps) { const [sortBy, setSortBy] = useState<'date-desc' | 'date-asc' | 'rating-desc' | 'rating-asc'>('date-desc'); const [isDeleting, setIsDeleting] = useState(null); const handleDelete = async (tastingId: string, bottleId: string) => { if (!confirm('Bist du sicher, dass du diese Notiz löschen möchtest?')) return; setIsDeleting(tastingId); try { const res = await deleteTasting(tastingId, bottleId); if (!res.success) { alert(res.error || 'Fehler beim Löschen'); } } catch (err) { alert('Löschen fehlgeschlagen'); } finally { setIsDeleting(null); } }; const sortedTastings = useMemo(() => { const result = [...initialTastings]; return result.sort((a, b) => { switch (sortBy) { case 'date-desc': return new Date(b.created_at).getTime() - new Date(a.created_at).getTime(); case 'date-asc': return new Date(a.created_at).getTime() - new Date(b.created_at).getTime(); case 'rating-desc': return b.rating - a.rating; case 'rating-asc': return a.rating - b.rating; default: return 0; } }); }, [initialTastings, sortBy]); if (!initialTastings || initialTastings.length === 0) { return (

Noch keine Tasting Notes vorhanden. Zeit für ein Glas? 🥃

); } return (
{sortedTastings.map((note) => (
{note.rating}/100
{note.is_sample ? 'Sample' : 'Bottle'}
{new Date(note.created_at).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' })}
{note.tasting_sessions && ( {note.tasting_sessions.name} )}
{new Date(note.created_at).toLocaleDateString('de-DE')}
{(!currentUserId || note.user_id === currentUserId) && ( )}
{/* Visual Divider for MD and up */}
{note.nose_notes && (
Nose

{note.nose_notes}

)} {note.palate_notes && (
Palate

{note.palate_notes}

)} {note.finish_notes && (
Finish

{note.finish_notes}

)}
{note.tasting_tags && note.tasting_tags.length > 0 && (
Gekostet mit: {note.tasting_tags.map((tag) => ( {tag.buddies.name} ))}
)}
))}
); }