'use client' import { useState, useTransition } from 'react' import { useRouter } from 'next/navigation' import { IconDeviceLaptop as Laptop, IconMoon as Moon, IconSun as Sun, IconTrash as Trash2 } from '@tabler/icons-react' import { toast } from 'sonner' import { deleteAccount } from '@/lib/actions/account' import { signOut } from '@/lib/actions/auth' import type { AppUser } from '@/lib/auth/types' import { chatStore } from '@/lib/chat-store' import { useAuthCapabilities } from '@/lib/contexts/app-user-context' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Separator } from '@/components/ui/separator' import { Spinner } from '@/components/ui/spinner' import { useTheme } from '@/components/theme-provider' interface AccountSettingsDialogProps { open: boolean onOpenChange: (open: boolean) => void user: AppUser } const themeOptions = [ { value: 'light', label: 'Light', icon: Sun }, { value: 'dark', label: 'Dark', icon: Moon }, { value: 'system', label: 'System', icon: Laptop } ] export function AccountSettingsDialog({ open, onOpenChange, user }: AccountSettingsDialogProps) { const router = useRouter() const { setTheme, theme } = useTheme() const capabilities = useAuthCapabilities() const [isDeleting, startDeleteTransition] = useTransition() const [confirmOpen, setConfirmOpen] = useState(false) const activeTheme = theme ?? 'system' const userName = user.name || 'User' const handleDeleteAccount = () => { startDeleteTransition(async () => { const result = await deleteAccount() if (result.success) { chatStore.setUser(null) try { await signOut() } catch (error) { console.error('Failed to clear client session:', error) } toast.success('Account deleted') setConfirmOpen(false) onOpenChange(false) router.push('/') router.refresh() return } toast.error(result.error ?? 'Failed to delete account') }) } return ( { if (!isDeleting) { if (!nextOpen) { setConfirmOpen(false) } onOpenChange(nextOpen) } }} > Account Manage your account preferences and data.

Profile

{userName}

{user.email}

Theme

Choose how Morphic appears on this device.

{themeOptions.map(option => { const Icon = option.icon const selected = activeTheme === option.value return ( ) })}
{capabilities.deleteUser && ( <>

Delete account

Permanently delete your account, chat history, and uploaded files. This action cannot be undone.

{ if (!isDeleting) { setConfirmOpen(nextOpen) } }} > Delete your account? This action cannot be undone. Your account, chat history, and uploaded files will be permanently deleted. Cancel { event.preventDefault() handleDeleteAccount() }} className="bg-destructive text-destructive-foreground hover:bg-destructive/90" > {isDeleting ? : 'Delete account'}
)}
) }