"use client";
import { useEffect, useState, useCallback } from "react";
import {
Mail,
Phone,
Smartphone,
MapPin,
Briefcase,
Shield,
ShieldCheck,
Clock,
Globe,
Calendar,
User,
Building,
RefreshCw,
} from "lucide-react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
import { Skeleton } from "@/components/ui/skeleton";
import { getCurrentUserProfile } from "@/lib/usersService";
import { UserProfile, formatLastLogin } from "@/types/user";
interface UserProfileDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
}
// Componente para mostrar un campo de información
function InfoField({
icon: Icon,
label,
value,
className = "",
}: {
icon: React.ComponentType<{ className?: string }>;
label: string;
value: string | null | undefined;
className?: string;
}) {
if (!value) return null;
return (
);
}
// Skeleton para estado de carga
function ProfileSkeleton() {
return (
{/* Header skeleton */}
{/* Content skeleton */}
{Array.from({ length: 6 }).map((_, i) => (
))}
);
}
// Estado de error
function ErrorState({ onRetry }: { onRetry: () => void }) {
return (
Error al cargar el perfil
No se pudo cargar la información del usuario. Por favor, intenta de nuevo.
);
}
export function UserProfileDialog({ open, onOpenChange }: UserProfileDialogProps) {
const [profile, setProfile] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const fetchProfile = useCallback(async () => {
setIsLoading(true);
setError(null);
try {
const data = await getCurrentUserProfile();
setProfile(data);
} catch (err) {
console.error("Error fetching profile:", err);
setError("No se pudo cargar el perfil");
} finally {
setIsLoading(false);
}
}, []);
// Cargar perfil cuando se abre el dialog
useEffect(() => {
if (open && !profile) {
fetchProfile();
}
}, [open, profile, fetchProfile]);
// Formatear dirección completa
const formatAddress = (p: UserProfile): string | null => {
const parts = [p.address, p.postalCode, p.city, p.country].filter(Boolean);
return parts.length > 0 ? parts.join(", ") : null;
};
return (
);
}