'use client' import { useState, useEffect, useRef, useCallback } from 'react' import { Input, toast, Spinner } from '@ciphera-net/ui' import { useAuth } from '@/lib/auth/context' import { updateDisplayName } from '@/lib/api/user' import { deleteAccount } from '@/lib/api/user' import { getAuthErrorMessage } from '@ciphera-net/ui' import { DangerZone } from '@/components/settings/unified/DangerZone' export default function AccountProfileTab({ onDirtyChange, onRegisterSave }: { onDirtyChange?: (dirty: boolean) => void; onRegisterSave?: (fn: () => Promise) => void }) { const { user, refresh, logout } = useAuth() const [displayName, setDisplayName] = useState('') const initialRef = useRef('') const hasInitialized = useRef(false) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [deleteText, setDeleteText] = useState('') const [deletePassword, setDeletePassword] = useState('') const [deleting, setDeleting] = useState(false) useEffect(() => { if (!user || hasInitialized.current) return setDisplayName(user.display_name || '') initialRef.current = user.display_name || '' hasInitialized.current = true }, [user]) // Track dirty state useEffect(() => { if (!hasInitialized.current) return onDirtyChange?.(displayName !== initialRef.current) }, [displayName, onDirtyChange]) const handleSave = useCallback(async () => { try { await updateDisplayName(displayName.trim()) await refresh() initialRef.current = displayName.trim() onDirtyChange?.(false) toast.success('Profile updated') } catch (err) { toast.error(getAuthErrorMessage(err as Error) || 'Failed to update profile') } }, [displayName, refresh, onDirtyChange]) useEffect(() => { onRegisterSave?.(handleSave) }, [handleSave, onRegisterSave]) const handleDelete = async () => { if (deleteText !== 'DELETE' || !deletePassword) return setDeleting(true) try { await deleteAccount(deletePassword) logout() } catch (err) { toast.error(getAuthErrorMessage(err as Error) || 'Failed to delete account') setDeleting(false) } } if (!user) return
return (

Profile

Manage your personal account settings.

{/* Display Name */}
setDisplayName(e.target.value)} placeholder="Your name" />

Email changes require password verification. Use Ciphera Auth to change your email.

{/* Danger Zone */} setShowDeleteConfirm(prev => !prev), }, ]} /> {showDeleteConfirm && (

This will permanently delete:

  • Your account and all personal data
  • All sessions and trusted devices
  • You will be removed from all organizations
setDeletePassword(e.target.value)} placeholder="Enter your password" />
setDeleteText(e.target.value)} placeholder="DELETE" />
)}
) }