'use client'; import { useState, useEffect } from 'react'; import { useAuth } from '@/context/AuthContext'; import { storage, db } from '@/lib/firebase'; import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'; import { doc, getDoc, updateDoc } from 'firebase/firestore'; import { useTheme } from 'next-themes'; interface UserProfile { avatarUrl?: string; theme?: string; categories?: string[]; } export default function Profile() { const { user } = useAuth(); const { theme, setTheme } = useTheme(); const [profile, setProfile] = useState({}); const [uploading, setUploading] = useState(false); const [newCategory, setNewCategory] = useState(''); useEffect(() => { if (user) { loadProfile(); } }, [user]); const loadProfile = async () => { if (!user) return; try { const docRef = doc(db, 'users', user.uid); const docSnap = await getDoc(docRef); if (docSnap.exists()) { setProfile(docSnap.data() as UserProfile); } } catch (error) { console.error('Error loading profile:', error); } }; const handleAvatarUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file || !user) return; setUploading(true); try { const storageRef = ref(storage, `avatars/${user.uid}`); await uploadBytes(storageRef, file); const url = await getDownloadURL(storageRef); await updateDoc(doc(db, 'users', user.uid), { avatarUrl: url }); setProfile(prev => ({ ...prev, avatarUrl: url })); } catch (error) { console.error('Error uploading avatar:', error); } finally { setUploading(false); } }; const handleThemeChange = async (newTheme: string) => { setTheme(newTheme); if (user) { await updateDoc(doc(db, 'users', user.uid), { theme: newTheme }); setProfile(prev => ({ ...prev, theme: newTheme })); } }; const addCategory = async () => { if (!newCategory.trim() || !user) return; const updatedCategories = [...(profile.categories || []), newCategory.trim()]; await updateDoc(doc(db, 'users', user.uid), { categories: updatedCategories }); setProfile(prev => ({ ...prev, categories: updatedCategories })); setNewCategory(''); }; const removeCategory = async (category: string) => { if (!user) return; const updatedCategories = (profile.categories || []).filter(c => c !== category); await updateDoc(doc(db, 'users', user.uid), { categories: updatedCategories }); setProfile(prev => ({ ...prev, categories: updatedCategories })); }; return (

Profile

{/* Avatar */}
{profile.avatarUrl && ( Avatar )} {uploading &&

Uploading...

}
{/* Theme */}
{/* Categories */}
setNewCategory(e.target.value)} placeholder="Add category" className="flex-1 p-3 bg-gray-700 border border-gray-600 rounded" />
{(profile.categories || []).map(category => ( {category} ))}
); }