import { useEffect, useState } from "react" import { BadgeCheck, CalendarDays, Clock3, Mail, Phone, Shield, User, } from "lucide-react" import { toast } from "sonner" import { getTeamMe, updateTeamMe } from "../api/team.api" import { Badge } from "../components/ui/badge" import { Button } from "../components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "../components/ui/card" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "../components/ui/dialog" import { Input } from "../components/ui/input" import { SpinnerIcon } from "../components/ui/spinner-icon" import { Textarea } from "../components/ui/textarea" import { cn } from "../lib/utils" import type { TeamMeProfile } from "../types/team.types" import { ProfileAvatarUpload } from "../components/profile/ProfileAvatarUpload" import { PersonaProfilePictureUploadField } from "./personas/components/PersonaProfilePictureUploadField" function formatDate(dateStr: string | null | undefined): string { if (!dateStr) return "—" const date = new Date(dateStr) if (Number.isNaN(date.getTime())) return dateStr return date.toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric", }) } function formatDateTime(dateStr: string | null | undefined): string { if (!dateStr) return "—" const date = new Date(dateStr) if (Number.isNaN(date.getTime())) return dateStr return date.toLocaleString(undefined, { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }) } function formatRoleLabel(role: string): string { const value = role.trim() if (!value) return "—" return value .split("_") .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()) .join(" ") } function formatStatusLabel(status: string): string { const value = status.trim() if (!value) return "—" return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase() } function displayValue(value: string | null | undefined): string { const trimmed = value?.trim() return trimmed ? trimmed : "—" } function ReadOnlyField({ label, value }: { label: string; value: string }) { return (

{label}

{value}

) } function LoadingSkeleton() { return (
{[1, 2].map((item) => (
))}
) } type EditFormState = { first_name: string last_name: string phone_number: string department: string job_title: string profile_picture_url: string bio: string work_phone: string } function profileToEditForm(profile: TeamMeProfile): EditFormState { return { first_name: profile.first_name, last_name: profile.last_name, phone_number: profile.phone_number, department: profile.department ?? "", job_title: profile.job_title ?? "", profile_picture_url: profile.profile_picture_url ?? "", bio: profile.bio ?? "", work_phone: profile.work_phone ?? "", } } export function ProfilePage() { const [profile, setProfile] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [editing, setEditing] = useState(false) const [saving, setSaving] = useState(false) const [uploadingPicture, setUploadingPicture] = useState(false) const [editForm, setEditForm] = useState({ first_name: "", last_name: "", phone_number: "", department: "", job_title: "", profile_picture_url: "", bio: "", work_phone: "", }) const busy = saving || uploadingPicture const loadProfile = async () => { setLoading(true) setError(null) try { const res = await getTeamMe() setProfile(res.data.data) } catch (err) { console.error("Failed to fetch profile", err) setProfile(null) setError("Failed to load profile. Please try again later.") } finally { setLoading(false) } } useEffect(() => { void loadProfile() }, []) const startEditing = () => { if (!profile) return setEditForm(profileToEditForm(profile)) setUploadingPicture(false) setEditing(true) } const handleSave = async () => { if (!profile) return const firstName = editForm.first_name.trim() const lastName = editForm.last_name.trim() if (!firstName || !lastName) { toast.error("First name and last name are required") return } setSaving(true) try { const res = await updateTeamMe({ first_name: firstName, last_name: lastName, phone_number: editForm.phone_number.trim(), department: editForm.department.trim(), job_title: editForm.job_title.trim(), profile_picture_url: editForm.profile_picture_url.trim(), bio: editForm.bio.trim(), work_phone: editForm.work_phone.trim(), }) setProfile(res.data.data) setEditing(false) toast.success(res.data.message || "Profile updated successfully") } catch (err) { console.error("Failed to update profile", err) const message = (err as { response?: { data?: { message?: string } } })?.response?.data ?.message ?? "Failed to update profile" toast.error(message) } finally { setSaving(false) } } if (loading) return if (error || !profile) { return (

{error || "Profile not available"}

Please check your connection and try again.

) } const fullName = `${profile.first_name} ${profile.last_name}`.trim() const initials = `${profile.first_name?.[0] ?? ""}${profile.last_name?.[0] ?? ""}`.toUpperCase() const isActive = profile.status.toLowerCase() === "active" const handleProfileUpdate = (nextProfile: TeamMeProfile) => { setProfile(nextProfile) setEditForm((prev) => ({ ...prev, profile_picture_url: nextProfile.profile_picture_url ?? "", })) } return (

Account

My profile

Your team account details from the admin directory.

{fullName}

{displayValue(profile.job_title)}

{formatStatusLabel(profile.status)} {profile.email_verified ? "Email verified" : "Email not verified"}
{profile.bio?.trim() ? (

{profile.bio.trim()}

) : null}
Work details Contact
{displayValue(profile.email)}
{displayValue(profile.phone_number)}
{profile.work_phone?.trim() ? (
Work: {profile.work_phone.trim()}
) : null}
Account activity
!busy && setEditing(open)}> Edit profile
setEditForm((prev) => ({ ...prev, first_name: e.target.value })) } />
setEditForm((prev) => ({ ...prev, last_name: e.target.value })) } />
setEditForm((prev) => ({ ...prev, phone_number: e.target.value })) } />
setEditForm((prev) => ({ ...prev, work_phone: e.target.value })) } />
setEditForm((prev) => ({ ...prev, department: e.target.value })) } />
setEditForm((prev) => ({ ...prev, job_title: e.target.value })) } />

Email is managed by your administrator.

setEditForm((prev) => ({ ...prev, profile_picture_url })) } />