import { useState, useEffect, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Switch } from "@/components/ui/switch"; import { User, Mail, Shield, LogOut } from "lucide-react"; import { Separator } from "@/components/ui/separator"; import { toast } from "@/components/ui/sonner"; import { storage, STORAGE_KEYS } from "@/lib/storage"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; // Use the ESM build of multiavatar to generate SVG strings import multiavatar from "@multiavatar/multiavatar/esm"; interface ProfileModalProps { isOpen: boolean; onClose: () => void; } interface ProfileSettings { name: string; email: string; avatarUrl?: string; // now optional saveHistory: boolean; } const PROFILE_STORAGE_KEY = STORAGE_KEYS.PROFILE_STORAGE_KEY; export const ProfileModal = ({ isOpen, onClose }: ProfileModalProps) => { const [signOutDialogOpen, setSignOutDialogOpen] = useState(false); const [profileSettings, setProfileSettings] = useState(() => { // load, or default (no external URL here) return ( storage.get(PROFILE_STORAGE_KEY) || { name: "John Doe", email: "john.doe@example.com", avatarUrl: undefined, saveHistory: true, } ); }); // reload from storage whenever modal re-opens useEffect(() => { if (isOpen) { const saved = storage.get(PROFILE_STORAGE_KEY); if (saved) setProfileSettings(saved); } }, [isOpen]); // memoize the SVG data URI so it only regenerates when the name changes const defaultAvatarDataUri = useMemo(() => { const svg = multiavatar(profileSettings.name); return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`; }, [profileSettings.name]); // pick either the custom URL or the inline SVG const avatarSrc = profileSettings.avatarUrl || defaultAvatarDataUri; const handleSaveChanges = () => { storage.set(PROFILE_STORAGE_KEY, profileSettings); toast.success("Profile settings saved successfully"); onClose(); }; const handleSignOut = () => { toast.success("Signed out successfully"); setSignOutDialogOpen(false); onClose(); // clear auth tokens, etc. }; return ( <> Profile Settings Manage your profile information and preferences.
{/* Profile section */}

{profileSettings.name}

{profileSettings.email}

{/* Name */}
setProfileSettings({ ...profileSettings, name: e.target.value }) } className="col-span-3" />
{/* Email */}
setProfileSettings({ ...profileSettings, email: e.target.value }) } className="col-span-3" />
{/* Privacy */}

Privacy and Data

Keep your chat history saved

setProfileSettings({ ...profileSettings, saveHistory }) } />
{/* Sign-out confirmation */} Sign Out Are you sure you want to sign out? You will need to sign in again to access your account. Cancel Sign Out ); };