import { useState, useEffect } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { useAuthStore } from '@/store/auth' import { iamApi, type User, type UpdateUserRequest } from '@/api/iam' import { Button } from '@/components/ui/button' import { ArrowLeft, Save, Mail, User as UserIcon, Shield, Calendar, Clock, Edit2, X } from 'lucide-react' export default function Profile() { const { id } = useParams<{ id?: string }>() const navigate = useNavigate() const { user: currentUser } = useAuthStore() const queryClient = useQueryClient() const [isEditing, setIsEditing] = useState(false) const [editForm, setEditForm] = useState({ email: '', full_name: '', }) // Determine which user to show const targetUserId = id || currentUser?.id // Check permission: only allow if viewing own profile or user is admin const canView = !!currentUser && !!targetUserId && ( targetUserId === currentUser.id || currentUser.roles.includes('admin') ) const { data: profileUser, isLoading } = useQuery({ queryKey: ['iam-user', targetUserId], queryFn: () => iamApi.getUser(targetUserId!), enabled: canView, }) const updateMutation = useMutation({ mutationFn: (data: UpdateUserRequest) => iamApi.updateUser(targetUserId!, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['iam-user', targetUserId] }) queryClient.invalidateQueries({ queryKey: ['iam-users'] }) setIsEditing(false) // If updating own profile, refresh auth store if (targetUserId === currentUser?.id) { queryClient.invalidateQueries({ queryKey: ['auth-me'] }) } }, }) useEffect(() => { if (profileUser) { setEditForm({ email: profileUser.email || '', full_name: profileUser.full_name || '', }) } }, [profileUser]) if (!canView) { return (

Access Denied

You don't have permission to view this profile.

) } if (isLoading) { return (

Loading profile...

) } if (!profileUser) { return (

User not found

) } const isOwnProfile = targetUserId === currentUser?.id const canEdit = isOwnProfile || currentUser?.roles.includes('admin') const handleSave = () => { updateMutation.mutate({ email: editForm.email, full_name: editForm.full_name, }) } const formatDate = (dateString: string) => { return new Date(dateString).toLocaleString() } const formatLastLogin = (lastLoginAt: string | null) => { if (!lastLoginAt) return 'Never' return formatDate(lastLoginAt) } const getAvatarInitials = () => { if (profileUser?.full_name) { return profileUser.full_name .split(' ') .map((n: string) => n[0]) .join('') .substring(0, 2) .toUpperCase() } return profileUser?.username?.substring(0, 2).toUpperCase() || 'U' } return (
{/* Header */}

User Profile

{isOwnProfile ? 'Your profile information' : `Viewing profile for ${profileUser.username}`}

{canEdit && (
{isEditing ? ( <> ) : ( )}
)}
{/* Profile Card */}
{/* Profile Header */}
{getAvatarInitials()}

{profileUser.full_name || profileUser.username}

@{profileUser.username}

{profileUser.is_active ? 'Active' : 'Inactive'}
{profileUser.is_system && (
System User
)}
{/* Profile Content */}
{/* Basic Information */}

Basic Information

{profileUser.username}

Username cannot be changed

{isEditing ? ( setEditForm({ ...editForm, email: e.target.value })} className="w-full bg-[#0f161d] border border-border-dark rounded-lg px-4 py-3 text-white focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="email@example.com" /> ) : (
{profileUser.email || '-'}
)}
{isEditing ? ( setEditForm({ ...editForm, full_name: e.target.value })} className="w-full bg-[#0f161d] border border-border-dark rounded-lg px-4 py-3 text-white focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="Full Name" /> ) : (
{profileUser.full_name || '-'}
)}
{/* Account Details */}

Account Details

{profileUser.roles && profileUser.roles.length > 0 ? (
{profileUser.roles.map((role) => ( {role} ))}
) : ( No roles assigned )}
{profileUser.permissions && profileUser.permissions.length > 0 ? (
{profileUser.permissions.map((perm) => ( {perm} ))}
) : ( No permissions assigned )}
{formatLastLogin(profileUser.last_login_at)}
{formatDate(profileUser.created_at)}
) }