import { AtSign, KeyRound, Save, ShieldCheck, UserRound } from 'lucide-react' import { FormEvent, useState } from 'react' import { Spinner } from '../components/ui' import { useAuth } from '../context/AuthContext' import { useToast } from '../context/ToastContext' import { api } from '../lib/api' import { displayName, getInitials } from '../lib/format' export function ProfilePage() { const { user, refreshProfile } = useAuth() const { showToast } = useToast() const [firstName, setFirstName] = useState(user?.firstName ?? '') const [lastName, setLastName] = useState(user?.lastName ?? '') const [email, setEmail] = useState(user?.email ?? '') const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [savingProfile, setSavingProfile] = useState(false) const [savingPassword, setSavingPassword] = useState(false) const [profileError, setProfileError] = useState('') const [passwordError, setPasswordError] = useState('') const handleProfile = async (event: FormEvent) => { event.preventDefault() setSavingProfile(true) setProfileError('') try { await api.updateProfile({ email: email.trim(), firstName: firstName.trim(), lastName: lastName.trim() }) await refreshProfile() showToast('success', 'Profile saved', 'Your account details are up to date.') } catch (caught) { setProfileError(caught instanceof Error ? caught.message : 'Your profile could not be saved.') } finally { setSavingProfile(false) } } const handlePassword = async (event: FormEvent) => { event.preventDefault() setSavingPassword(true) setPasswordError('') try { await api.updateProfile({ currentPassword, newPassword }) setCurrentPassword('') setNewPassword('') showToast('success', 'Password updated', 'Use your new password the next time you sign in.') } catch (caught) { setPasswordError(caught instanceof Error ? caught.message : 'Your password could not be changed.') } finally { setSavingPassword(false) } } const isAdmin = user?.roles.some((role) => role === 'Site Admin' || role === 'Admin') return (

Your Keeply account

Profile & settings

Keep your personal details and sign-in information current.

Personal details

Used across your household and pantry.

void handleProfile(event)}>
{profileError &&
{profileError}
}

Change password

Choose at least 8 characters with upper, lower and a number.

void handlePassword(event)}>
{passwordError &&
{passwordError}
}
) }