'use client' import { useAuth } from '@thedaviddias/auth' import { Badge } from '@thedaviddias/design-system/badge' import { Button } from '@thedaviddias/design-system/button' import { logger } from '@thedaviddias/logging' import { AlertCircle, CheckCircle, Edit, ExternalLink, Eye, EyeOff, Github, Settings, Trash2, User } from 'lucide-react' import Link from 'next/link' import { usePathname, useRouter, useSearchParams } from 'next/navigation' import { useEffect, useState } from 'react' import { toast } from 'sonner' import { EditProfileModal } from '@/components/profile/edit-profile-modal' import { Card } from '@/components/ui/card' import { UserMessageBanner } from '@/components/ui/user-message-banner' import { fetchWithCSRF } from '@/lib/csrf-client' export default function ProfileContent() { const { user, signOut, isLoaded } = useAuth() const router = useRouter() const pathname = usePathname() const searchParams = useSearchParams() const [isDeleting, setIsDeleting] = useState(false) const [showEditModal, setShowEditModal] = useState(false) // Check for success/error messages from URL params useEffect(() => { const message = searchParams.get('message') if (message) { if (message.includes('success') || message.includes('connected')) { toast.success(message) } else if (message.includes('error') || message.includes('failed')) { toast.error(message) } else { toast.info(message) } // Clean up URL using Next.js router const newSearchParams = new URLSearchParams(searchParams) newSearchParams.delete('message') const newUrl = pathname + (newSearchParams.toString() ? `?${newSearchParams.toString()}` : '') router.replace(newUrl, { scroll: false }) } }, [searchParams, pathname, router]) // Redirect if not authenticated (but only after auth has loaded) useEffect(() => { if (isLoaded && !user) { router.push('/login?message=Please sign in to view your profile') } }, [isLoaded, user, router]) /** * Handles account deletion with confirmation prompt */ const handleDeleteAccount = async () => { if (!confirm('Are you sure you want to delete your account? This action cannot be undone.')) { return } setIsDeleting(true) try { const response = await fetchWithCSRF('/api/user/delete-account', { method: 'DELETE' }) if (!response.ok) { throw new Error('Failed to delete account') } toast.success('Account deleted successfully') await signOut() router.push('/') } catch (error) { logger.error(error instanceof Error ? error : new Error(String(error)), { data: error, tags: { type: 'component', component: 'profile-content' } }) toast.error('Failed to delete account. Please try again.') } finally { setIsDeleting(false) } } // Show loading state while auth is loading or user is not yet available if (!isLoaded || !user) { return (
Loading...
) } const hasGitHubAuth = Boolean( user.externalAccounts?.some(account => account.provider === 'oauth_github') ) const displayName = user.publicMetadata?.user_name || user.email?.split('@')[0] || 'User' const accountType = hasGitHubAuth ? 'GitHub' : 'Email' const isProfilePrivate = user.publicMetadata?.isProfilePrivate === true return (

Profile Settings

Manage your account settings and preferences

{/* Account Overview */}
{hasGitHubAuth ? ( ) : ( )}

{displayName}

{user.email}

{accountType} Account {hasGitHubAuth && ( Verified )} {isProfilePrivate ? ( <> Private Profile ) : ( <> Public Profile )}
{/* Privacy Status */}
{/* Account Status */} {!hasGitHubAuth && ( )}
{/* GitHub Connection */}

GitHub Integration

{hasGitHubAuth ? 'Your GitHub account is connected and verified' : 'Connect GitHub to unlock premium features'}

{hasGitHubAuth ? ( <> Connected ) : ( )}
{/* Actions */}

Account Actions

{hasGitHubAuth && ( )}
{/* Danger Zone */}

Danger Zone

Once you delete your account, there is no going back. Please be certain.

{/* Edit Profile Modal */}
) }