import { useState } from 'react' import { useAuthStore } from '@/store/auth-store' import { supabase } from '@/lib/supabase' import { cn } from '@/lib/utils' export function ProfileCard() { const user = useAuthStore((s) => s.user) const [editingName, setEditingName] = useState(false) const [editingEmail, setEditingEmail] = useState(false) const [nameInput, setNameInput] = useState(user?.name ?? '') const [emailInput, setEmailInput] = useState(user?.email ?? '') const [saving, setSaving] = useState(false) const [error, setError] = useState('') async function saveName() { if (!nameInput.trim()) return setSaving(true) setError('') try { const { error: err } = await supabase.auth.updateUser({ data: { name: nameInput.trim() } }) if (err) throw err setEditingName(false) } catch { setError('Không thể lưu tên. Thử lại sau.') } finally { setSaving(false) } } async function saveEmail() { if (!emailInput.trim()) return setSaving(true) setError('') try { const { error: err } = await supabase.auth.updateUser({ email: emailInput.trim() }) if (err) throw err setEditingEmail(false) } catch { setError('Không thể lưu email. Thử lại sau.') } finally { setSaving(false) } } const initials = (user?.name ?? 'U').charAt(0).toUpperCase() return (

person Hồ sơ cá nhân

{/* Avatar */}
{initials}
{/* Fields */}
{/* Name */}

Họ và tên

{editingName ? ( setNameInput(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') saveName(); if (e.key === 'Escape') setEditingName(false) }} className="mt-0.5 text-base font-semibold w-full border border-blue-300 rounded-lg px-2 py-0.5 outline-none focus:ring-2 focus:ring-blue-200" /> ) : (

{user?.name ?? '—'}

)}
{editingName ? (
) : ( )}
{/* Email */}

Email

{editingEmail ? ( setEmailInput(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') saveEmail(); if (e.key === 'Escape') setEditingEmail(false) }} className="mt-0.5 text-base font-semibold w-full border border-blue-300 rounded-lg px-2 py-0.5 outline-none focus:ring-2 focus:ring-blue-200" /> ) : (

{user?.email ?? '—'}

)}
{editingEmail ? (
) : ( )}
{error &&

{error}

}
) }