'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 AvatarStack from './AvatarStack'; import { useI18n } from '@/i18n/I18nContext'; import { deleteTasting } from '@/services/delete-tasting'; import { useLiveQuery } from 'dexie-react-hooks'; import { db } from '@/lib/db'; 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_buddies?: { buddies: { id: string; name: string; } }[]; tasting_sessions?: { id: string; name: string; }; tasting_tags?: { tags: { id: string; name: string; category: string; is_system_default: boolean; } }[]; user_id: string; isPending?: boolean; } interface TastingListProps { initialTastings: Tasting[]; currentUserId?: string; bottleId?: string; } export default function TastingList({ initialTastings, currentUserId, bottleId }: TastingListProps) { const { t } = useI18n(); 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 (tastingId.startsWith('pending-')) return; 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 pendingTastings = useLiveQuery( () => bottleId ? db.pending_tastings.where('bottle_id').equals(bottleId).toArray() : db.pending_tastings.toArray(), [bottleId], [] ); const sortedTastings = useMemo(() => { const merged = [ ...initialTastings, ...(pendingTastings || []).map(p => ({ id: `pending-${p.id}`, rating: p.data.rating, nose_notes: p.data.nose_notes, palate_notes: p.data.palate_notes, finish_notes: p.data.finish_notes, is_sample: p.data.is_sample, bottle_id: p.bottle_id, created_at: p.tasted_at, user_id: currentUserId || '', isPending: true, tasting_buddies: [], tasting_sessions: undefined, tasting_tags: [] })) ]; return merged.sort((a, b) => { const timeA = new Date(a.created_at).getTime(); const timeB = new Date(b.created_at).getTime(); switch (sortBy) { case 'date-desc': return timeB - timeA; case 'date-asc': return timeA - timeB; case 'rating-desc': return b.rating - a.rating; case 'rating-asc': return a.rating - b.rating; default: return 0; } }); }, [initialTastings, pendingTastings, sortBy, currentUserId]); 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'} {note.isPending && (
Wartet auf Sync...
)}
{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) && !note.isPending && ( )}
{/* 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 && (
{note.tasting_tags.map(tt => ( {tt.tags.is_system_default ? t(`aroma.${tt.tags.name}`) : tt.tags.name} ))}
)} {note.tasting_buddies && note.tasting_buddies.length > 0 && (
{t('tasting.with') || 'Mit'}: tag.buddies.name)} />
)}
))}
); }