import { App, Button, Table, Tag, Tooltip, Typography } from 'antd'; import type { TableColumnsType } from 'antd'; import { useEffect, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { colors } from '@/design/tokens/colors'; import { formatDateTime } from '@/utils/formatDate'; import { adsApi } from '@/services/adsApi'; import EmptyValue from '@/components/ui/EmptyValue'; import { AD_STATE_LABELS, AD_TYPE_LABELS, truncateText, truncateUuid } from '@/pages/Billboards/adLabels'; import type { Ad } from '@/types/ad'; const { Text } = Typography; const PAGE_SIZE = 10; interface FetchResult { page: number; status: 'success' | 'error'; ads: Ad[]; total: number; } const Billboards = () => { const { message } = App.useApp(); const navigate = useNavigate(); const [page, setPage] = useState(1); const [result, setResult] = useState(null); useEffect(() => { adsApi .getAdminAds({ limit: PAGE_SIZE, offset: (page - 1) * PAGE_SIZE }) .then(({ results, count }) => { setResult({ page, status: 'success', ads: results, total: count }); }) .catch(() => { setResult({ page, status: 'error', ads: [], total: 0 }); message.error('دریافت اطلاعات بیلبوردها با خطا مواجه شد'); }); }, [page, message]); const isCurrent = result?.page === page; const isLoading = !isCurrent; const ads = isCurrent ? result.ads : []; const total = isCurrent ? result.total : 0; const columns: TableColumnsType = [ { title: 'شناسه', dataIndex: 'uuid', key: 'uuid', render: (value: string) => ( {truncateUuid(value)} ), }, { title: 'کاربر (ثبت‌کننده)', dataIndex: 'user', key: 'user', render: (value: string) => ( {truncateUuid(value)} ), }, { title: 'عنوان', dataIndex: 'title', key: 'title', ellipsis: true, render: (value: string | null) => value ?? , }, { title: 'نوع', dataIndex: 'type', key: 'type', render: (value: number) => {AD_TYPE_LABELS[value] ?? value}, }, { title: 'توضیحات', dataIndex: 'description', key: 'description', render: (value: string | null) => value ? ( {truncateText(value)} ) : ( ), }, { title: 'برچسب‌ها', dataIndex: 'tags', key: 'tags', render: (tags: string[]) => tags?.length ? (
{tags.map((tag) => ( {tag} ))}
) : ( ), }, { title: 'تاریخ ایجاد', dataIndex: 'created_at', key: 'created_at', render: formatDateTime }, { title: 'تاریخ آخرین ویرایش', dataIndex: 'updated_at', key: 'updated_at', render: formatDateTime }, { title: 'وضعیت', dataIndex: 'state', key: 'state', render: (value: number) => { const state = AD_STATE_LABELS[value]; return {state?.label ?? value}; }, }, { title: 'تعداد نظرات', dataIndex: 'comments_count', key: 'comments_count' }, { title: 'تعداد لایک‌ها', dataIndex: 'likes_count', key: 'likes_count' }, { title: 'فعال/غیرفعال', dataIndex: 'is_active', key: 'is_active', render: (value: boolean) => {value ? 'فعال' : 'غیرفعال'}, }, { title: 'جزئیات', key: 'details', render: (_, record) => ( ), }, ]; return ( ); }; export default Billboards;