'use client'; import { useRouter } from 'next/navigation'; import { ArrowLeft, Calendar, Plus, GlassWater, Loader2, ChevronRight, Users, Sparkles, Clock, Trash2 } from 'lucide-react'; import { useI18n } from '@/i18n/I18nContext'; import { useEffect, useState } from 'react'; import { createClient } from '@/lib/supabase/client'; import Link from 'next/link'; import { useSession } from '@/context/SessionContext'; import { useAuth } from '@/context/AuthContext'; import { deleteSession } from '@/services/delete-session'; import AvatarStack from '@/components/AvatarStack'; interface Session { id: string; name: string; scheduled_at: string; ended_at?: string; participant_count?: number; whisky_count?: number; participants?: string[]; } export default function SessionsPage() { const router = useRouter(); const { t, locale } = useI18n(); const supabase = createClient(); const { activeSession, setActiveSession } = useSession(); const { user, isLoading: isAuthLoading } = useAuth(); const [sessions, setSessions] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isCreating, setIsCreating] = useState(false); const [isDeleting, setIsDeleting] = useState(null); const [newName, setNewName] = useState(''); const [showCreateForm, setShowCreateForm] = useState(false); useEffect(() => { if (!isAuthLoading && user) { fetchSessions(); } }, [user, isAuthLoading]); const fetchSessions = async () => { setIsLoading(true); const { data, error } = await supabase .from('tasting_sessions') .select(` *, session_participants (buddies(name)), tastings (count) `) .order('scheduled_at', { ascending: false }); if (error) { console.error('Error fetching sessions:', error); // Fallback without tastings join const { data: fallbackData } = await supabase .from('tasting_sessions') .select(`*, session_participants (buddies(name))`) .order('scheduled_at', { ascending: false }); if (fallbackData) { setSessions(fallbackData.map(s => { const participants = (s.session_participants as any[])?.filter(p => p.buddies).map(p => p.buddies.name) || []; return { ...s, participant_count: participants.length, participants, whisky_count: 0 }; })); } } else { setSessions(data?.map(s => { const participants = (s.session_participants as any[])?.filter(p => p.buddies).map(p => p.buddies.name) || []; return { ...s, participant_count: participants.length, participants, whisky_count: (s.tastings as any[])?.[0]?.count || 0 }; }) || []); } setIsLoading(false); }; const handleCreateSession = async (e: React.FormEvent) => { e.preventDefault(); if (!newName.trim()) return; setIsCreating(true); const { data, error } = await supabase .from('tasting_sessions') .insert({ name: newName.trim(), scheduled_at: new Date().toISOString() }) .select() .single(); if (!error && data) { setSessions(prev => [{ ...data, participant_count: 0, whisky_count: 0 }, ...prev]); setNewName(''); setShowCreateForm(false); setActiveSession({ id: data.id, name: data.name }); } setIsCreating(false); }; const handleDeleteSession = async (id: string) => { setIsDeleting(id); const result = await deleteSession(id); if (result.success) { setSessions(prev => prev.filter(s => s.id !== id)); if (activeSession?.id === id) { setActiveSession(null); } } setIsDeleting(null); }; const formatDate = (date: string) => { return new Date(date).toLocaleDateString(locale === 'de' ? 'de-DE' : 'en-US', { day: 'numeric', month: 'short', year: 'numeric' }); }; const activeSessions = sessions.filter(s => !s.ended_at); const pastSessions = sessions.filter(s => s.ended_at); return (
{/* Header */}

{t('session.title')}

{sessions.length} Sessions

{/* Create Form */} {showCreateForm && (
setNewName(e.target.value)} placeholder={t('session.sessionName')} className="w-full bg-zinc-950 border border-zinc-800 rounded-xl px-4 py-3 text-white placeholder-zinc-600 focus:outline-none focus:border-orange-600 mb-3" autoFocus />
)} {/* Active Session Banner */} {activeSession && (

Live Session

{activeSession.name}

)} {/* Sessions List */} {isLoading ? (
) : sessions.length === 0 ? (

No Sessions Yet

Start your first tasting session to track what you're drinking.

) : (
{sessions.map(session => (

{session.name}

{formatDate(session.scheduled_at)} {session.whisky_count ? ( {session.whisky_count} Whiskys ) : null}
{session.participants && session.participants.length > 0 && ( )}
))}
)}
); }