'use client'; import { useRouter } from 'next/navigation'; import { ArrowLeft, Users, UserPlus, Loader2, Trash2, Link2, Search } from 'lucide-react'; import { useI18n } from '@/i18n/I18nContext'; import { useEffect, useState } from 'react'; import { createClient } from '@/lib/supabase/client'; import { useAuth } from '@/context/AuthContext'; import { addBuddy, deleteBuddy } from '@/services/buddy'; import BuddyHandshake from '@/components/BuddyHandshake'; interface Buddy { id: string; name: string; buddy_profile_id: string | null; } export default function BuddiesPage() { const router = useRouter(); const { t } = useI18n(); const supabase = createClient(); const { user, isLoading: isAuthLoading } = useAuth(); const [buddies, setBuddies] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isAdding, setIsAdding] = useState(false); const [newName, setNewName] = useState(''); const [searchQuery, setSearchQuery] = useState(''); const [isHandshakeOpen, setIsHandshakeOpen] = useState(false); useEffect(() => { if (!isAuthLoading && user) { fetchBuddies(); } }, [user, isAuthLoading]); const fetchBuddies = async () => { setIsLoading(true); const { data, error } = await supabase .from('buddies') .select('*') .order('name'); if (!error) { setBuddies(data || []); } setIsLoading(false); }; const handleAddBuddy = async (e: React.FormEvent) => { e.preventDefault(); if (!newName.trim()) return; setIsAdding(true); const result = await addBuddy({ name: newName.trim() }); if (result.success && result.data) { setBuddies(prev => [...[result.data], ...prev].sort((a, b) => a.name.localeCompare(b.name))); setNewName(''); } setIsAdding(false); }; const handleDeleteBuddy = async (id: string) => { const result = await deleteBuddy(id); if (result.success) { setBuddies(prev => prev.filter(b => b.id !== id)); } }; const filteredBuddies = buddies.filter(b => b.name.toLowerCase().includes(searchQuery.toLowerCase()) ); const linkedBuddies = filteredBuddies.filter(b => b.buddy_profile_id); const unlinkedBuddies = filteredBuddies.filter(b => !b.buddy_profile_id); return (
{/* Header */}

{t('buddy.title')}

{buddies.length} Buddies

{/* Add Buddy Form */}
setNewName(e.target.value)} placeholder={t('buddy.placeholder')} className="flex-1 bg-zinc-900 border border-zinc-800 rounded-xl px-4 py-3 text-white placeholder-zinc-600 focus:outline-none focus:border-orange-600 transition-colors" />
{/* Search */} {buddies.length > 5 && (
setSearchQuery(e.target.value)} className="w-full pl-9 pr-4 py-2.5 bg-zinc-900 border border-zinc-800 rounded-xl text-sm text-white placeholder-zinc-600 focus:outline-none focus:border-orange-600/50" />
)} {/* Buddies List */} {isLoading ? (
) : buddies.length === 0 ? (

{t('buddy.noBuddies')}

Add your tasting friends to share sessions and compare notes.

) : (
{/* Linked Buddies */} {linkedBuddies.length > 0 && (

Linked Accounts

{linkedBuddies.map(buddy => ( ))}
)} {/* Unlinked Buddies */} {unlinkedBuddies.length > 0 && (
{linkedBuddies.length > 0 && (

Other Buddies

)}
{unlinkedBuddies.map(buddy => ( ))}
)}
)}
{/* Buddy Handshake Dialog */} setIsHandshakeOpen(false)} onSuccess={() => { setIsHandshakeOpen(false); fetchBuddies(); }} />
); } function BuddyCard({ buddy, onDelete }: { buddy: Buddy; onDelete: (id: string) => void }) { const { t } = useI18n(); const [isDeleting, setIsDeleting] = useState(false); const handleDelete = async () => { setIsDeleting(true); await onDelete(buddy.id); setIsDeleting(false); }; return (
{buddy.name[0].toUpperCase()}

{buddy.name}

{buddy.buddy_profile_id && (

{t('common.link')}

)}
); }