import { useEffect, useState } from "react";
import { Pencil, User } from "lucide-react";
import { toast } from "sonner";
import { notifyApiError } from "../lib/apiErrors";
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,
DialogDescription,
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";
import { UnassignedLabel, isUnassignedLabel, DisplayValue } from "../lib/displayValue"
function formatDate(dateStr: string | null | undefined): string {
if (!dateStr) return "unassigned";
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 "unassigned";
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 "unassigned";
return value
.split("_")
.map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
.join(" ");
}
function displayValue(value: string | null | undefined): string {
const trimmed = value?.trim();
return trimmed ? trimmed : "unassigned";
}
function FieldGroup({
label,
value,
mono,
}: {
label: string;
value: React.ReactNode;
mono?: boolean;
}) {
return (
{label}
{typeof value === "string" && isUnassignedLabel(value) ? (
) : (
value
)}
);
}
function SectionCard({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
{title}
{children}
);
}
function LoadingSkeleton() {
return (
);
}
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);
notifyApiError(err, "Failed to update profile");
} 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 handleProfileUpdate = (nextProfile: TeamMeProfile) => {
setProfile(nextProfile);
setEditForm((prev) => ({
...prev,
profile_picture_url: nextProfile.profile_picture_url ?? "",
}));
};
return (
My Profile
{/* Hero */}
{fullName}
{formatRoleLabel(profile.team_role)}
{profile.job_title?.trim() && (
{profile.job_title}
)}
{profile.email_verified ? "Verified" : "Unverified"}
{/* Personal Information */}
{/* Work Details */}
}
/>
}
/>
}
/>
{/* Account Activity */}
{/* Edit dialog */}
);
}