'use client'; import { useState } from 'react'; import { motion } from 'framer-motion'; import { User, Mail, Key, LogOut, Trash2, Camera, Check, X, Loader2 } from 'lucide-react'; import { useAuth } from '@/lib/contexts/AuthContext'; import { updateProfile, changePassword, logoutAll, UpdateProfileRequest, ChangePasswordRequest } from '@/lib/auth'; export function AccountTab() { const { user, logout, refreshUser } = useAuth(); const [isEditingProfile, setIsEditingProfile] = useState(false); const [isChangingPassword, setIsChangingPassword] = useState(false); const [profileForm, setProfileForm] = useState({ name: user?.name || '' }); const [passwordForm, setPasswordForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }); const [loading, setLoading] = useState(null); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const handleProfileSave = async () => { if (!profileForm.name.trim()) { setError('Имя не может быть пустым'); return; } setLoading('profile'); setError(null); try { await updateProfile({ name: profileForm.name } as UpdateProfileRequest); await refreshUser(); setIsEditingProfile(false); setSuccess('Профиль обновлён'); setTimeout(() => setSuccess(null), 3000); } catch (err) { setError(err instanceof Error ? err.message : 'Ошибка обновления профиля'); } finally { setLoading(null); } }; const handlePasswordChange = async () => { if (passwordForm.newPassword !== passwordForm.confirmPassword) { setError('Пароли не совпадают'); return; } if (passwordForm.newPassword.length < 8) { setError('Пароль должен быть минимум 8 символов'); return; } setLoading('password'); setError(null); try { await changePassword({ currentPassword: passwordForm.currentPassword, newPassword: passwordForm.newPassword, } as ChangePasswordRequest); setIsChangingPassword(false); setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }); setSuccess('Пароль изменён'); setTimeout(() => setSuccess(null), 3000); } catch (err) { setError(err instanceof Error ? err.message : 'Ошибка смены пароля'); } finally { setLoading(null); } }; const handleLogoutAll = async () => { setLoading('logoutAll'); try { await logoutAll(); window.location.href = '/'; } catch { setError('Ошибка выхода'); } finally { setLoading(null); } }; const handleLogout = async () => { setLoading('logout'); try { await logout(); window.location.href = '/'; } catch { setError('Ошибка выхода'); } finally { setLoading(null); } }; if (!user) return null; return (
{error && ( {error} )} {success && ( {success} )} {/* Profile Section */}
{user.avatar ? ( {user.name} ) : ( {user.name.charAt(0).toUpperCase()} )}
{isEditingProfile ? (
setProfileForm({ ...profileForm, name: e.target.value })} className="w-full px-3 py-2 bg-surface border border-border rounded-lg text-sm text-primary focus:outline-none focus:border-accent" placeholder="Ваше имя" />
) : ( <>

{user.name}

{user.tier === 'business' ? 'Business' : user.tier === 'pro' ? 'Pro' : 'Free'}

{user.email}

)}
{/* Email Section */}

{user.email}

{user.emailVerified ? ( Подтверждён ) : ( Не подтверждён )}

{!user.emailVerified && ( )}
{/* Password Section */}
{isChangingPassword ? (
setPasswordForm({ ...passwordForm, currentPassword: e.target.value })} className="w-full px-3 py-2 bg-surface border border-border rounded-lg text-sm text-primary focus:outline-none focus:border-accent" placeholder="Текущий пароль" /> setPasswordForm({ ...passwordForm, newPassword: e.target.value })} className="w-full px-3 py-2 bg-surface border border-border rounded-lg text-sm text-primary focus:outline-none focus:border-accent" placeholder="Новый пароль" /> setPasswordForm({ ...passwordForm, confirmPassword: e.target.value })} className="w-full px-3 py-2 bg-surface border border-border rounded-lg text-sm text-primary focus:outline-none focus:border-accent" placeholder="Подтвердите пароль" />
) : (

Пароль

••••••••

)}
{/* Session Section */}
{/* Delete Account */}
); } function Section({ title, icon: Icon, children }: { title: string; icon: React.ElementType; children: React.ReactNode }) { return (

{title}

{children}
); }