import React, { useEffect, useState } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { useForm, Controller } from 'react-hook-form'; import dayjs from 'dayjs'; import { ArrowLeft, Pencil } from 'lucide-react'; import { useAdmin, useUpdateAdmin } from '@/hooks/useAdmins'; import { formatDisplayValue } from '@/lib/utils'; import { PageHeader } from '@/components/PageHeader'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Skeleton } from '@/components/ui/skeleton'; const isBadValue = (val: unknown) => val === '-' || val === 'undefined' || val === '' || val === null || val === undefined; const clean = (val: unknown) => (val === '-' || val === 'undefined' ? undefined : val); const roleBadgeVariant = (role: string) => { if (role === 'superadmin') return 'destructive' as const; if (role === 'admin') return 'default' as const; if (role === 'moderator') return 'secondary' as const; return 'outline' as const; }; interface AdminFormValues { nickname?: string; email?: string; role?: string; status?: string; timezone?: string; language?: string; phone?: string; } const DetailValue: React.FC<{ value: unknown; emptyLabel: string }> = ({ value, emptyLabel }) => { const text = formatDisplayValue(value); if (text === '-') return <>{emptyLabel}; if (typeof value === 'object' && value !== null && !Array.isArray(value)) { return
{text}
; } return <>{text}; }; const AdminDetailPage: React.FC = () => { const { t } = useTranslation(); const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const { data: admin, isLoading } = useAdmin(id || ''); const updateAdmin = useUpdateAdmin(); const [editModal, setEditModal] = useState(false); const form = useForm(); const emDash = t('common.emDash'); const formatDate = (dateStr: string) => { if (isBadValue(dateStr)) return emDash; const d = dayjs(dateStr); return d.isValid() ? d.format('DD.MM.YYYY HH:mm') : dateStr; }; useEffect(() => { if (editModal && admin) { form.reset({ nickname: clean(admin.nickname) as string | undefined, email: clean(admin.email) as string | undefined, role: clean(admin.role) as string | undefined, status: clean(admin.status) as string | undefined, timezone: clean(admin.timezone) as string | undefined, language: clean(admin.language) as string | undefined, phone: clean(admin.phone) as string | undefined, }); } }, [editModal, admin, form]); const onSave = (values: AdminFormValues) => { const payload = Object.fromEntries( Object.entries(values).filter(([_, v]) => v !== '' && v !== undefined && v !== null) ); updateAdmin.mutate( { id: id!, data: payload }, { onSuccess: () => { setEditModal(false); }, } ); }; if (isLoading) { return (
); } if (!admin) return

{t('admins.notFound')}

; const titleName = !isBadValue(admin.nickname) ? admin.nickname : admin.email || admin.id; return (
} />
{t('common.id')}
{admin.id}
{t('common.email')}
{admin.email}
{t('common.nickname')}
{isBadValue(admin.nickname) ? emDash : admin.nickname}
{t('common.role')}
{admin.role}
{t('common.status')}
{admin.status}
{t('common.timezone')}
{isBadValue(admin.timezone) ? emDash : admin.timezone}
{t('common.language')}
{isBadValue(admin.language) ? emDash : admin.language}
{t('common.phone')}
{isBadValue(admin.phone) ? emDash : admin.phone}
{t('profile.avatarUrl')}
{isBadValue(admin.avatar_url) ? emDash : admin.avatar_url}
{t('profile.preferencesJson')}
{t('common.lastLogin')}
{formatDate(admin.last_login)}
{t('common.createdAt')}
{formatDate(admin.created_at)}
{t('common.updatedAt')}
{formatDate(admin.updated_at)}
{t('admins.editTitle')}
( )} />
( )} />
( )} />
( )} />
); }; export default AdminDetailPage;