"use client"; import { useState } from "react"; import Link from "next/link"; import { authClient } from "@/lib/auth-client"; import { AdminUserRow } from "@/actions/admin/users"; import { logUserActivity } from "@/actions/admin/audit"; import { updateAdminProfileAction } from "@/actions/admin/profile"; type ProfilePageProps = { currentUser: AdminUserRow; }; export default function AdminMeClient({ currentUser }: ProfilePageProps) { const [editing, setEditing] = useState(false); const [displayName, setDisplayName] = useState(currentUser.display_name ?? ""); const [phoneNumber, setPhoneNumber] = useState(currentUser.phone_number ?? ""); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [emailChangeSent, setEmailChangeSent] = useState(false); const [changingEmail, setChangingEmail] = useState(false); const [newEmail, setNewEmail] = useState(""); const [emailError, setEmailError] = useState(null); async function handleSaveProfile(e: React.FormEvent) { e.preventDefault(); setSaving(true); setError(null); try { const result = await updateAdminProfileAction( currentUser.id, displayName || null, phoneNumber || null ); if (!result.success) { setError(result.error); return; } await logUserActivity({ user_id: currentUser.user_id, activity_type: "profile_update", details: { fields: ["display_name", "phone_number"] }, }); setEditing(false); } finally { setSaving(false); } } async function handleEmailChange(e: React.FormEvent) { e.preventDefault(); setChangingEmail(true); setEmailError(null); try { const { error: updateError } = await authClient.changeEmail({ newEmail: newEmail, }); if (updateError) { setEmailError(updateError.message ?? "Failed to change email"); return; } await logUserActivity({ user_id: currentUser.user_id, activity_type: "email_change", details: { new_email: newEmail }, }); setEmailChangeSent(true); setChangingEmail(false); } finally { setChangingEmail(false); } } return (
← Back to dashboard

My Profile

Manage your profile information and preferences.

{error && (
{error}
)} {/* Profile card */}

{currentUser.display_name || currentUser.email}

{currentUser.display_name && (

{currentUser.email}

)}
{currentUser.role.replace("_", " ")} {currentUser.brand_name && ( {currentUser.brand_name} )}
{!editing && ( )}
{/* Read-only info when not editing */} {!editing && (

Phone

{currentUser.phone_number ?? "—"}

Email

{currentUser.email}

)} {/* Edit form */} {editing && (
setDisplayName(e.target.value)} className="mt-1 w-full rounded-lg border px-3 py-2 text-sm focus:outline-none focus:ring-1" style={{ borderColor: "var(--admin-border)", color: "var(--admin-text-primary)", backgroundColor: "white" }} placeholder="Your name" />
setPhoneNumber(e.target.value)} className="mt-1 w-full rounded-lg border px-3 py-2 text-sm focus:outline-none focus:ring-1" style={{ borderColor: "var(--admin-border)", color: "var(--admin-text-primary)", backgroundColor: "white" }} placeholder="+1 (555) 000-0000" autoComplete="tel" />
)}
{/* Email change section */}

Email Address

Current: {currentUser.email}

{emailChangeSent ? (
A confirmation email has been sent to {newEmail}. Click the link in the email to confirm the change.
) : (
setNewEmail(e.target.value)} required aria-required="true" className="mt-1 w-full rounded-lg border px-3 py-2 text-sm focus:outline-none focus:ring-1" style={{ borderColor: "var(--admin-border)", color: "var(--admin-text-primary)", backgroundColor: "white" }} placeholder="new@example.com" autoComplete="email" />
{emailError && (
{emailError}
)}
)}
); }