import { useState } from 'react'; import { useNavigate } from 'react-router'; import { useTranslation } from 'react-i18next'; import { useQuery } from '@tanstack/react-query'; import i18n from '../i18n'; import { promoOffersApi, PromoOfferLog, OFFER_TYPE_CONFIG, OfferType } from '../api/promoOffers'; import { usePlatform } from '../platform/hooks/usePlatform'; import { BackIcon, EditIcon, SendIcon, ClockIcon, UserIcon } from '@/components/icons'; // Helper functions const formatDateTime = (date: string | null): string => { if (!date) return '-'; const localeMap: Record = { ru: 'ru-RU', en: 'en-US', zh: 'zh-CN', fa: 'fa-IR' }; const locale = localeMap[i18n.language] || 'ru-RU'; return new Date(date).toLocaleString(locale, { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', }); }; const getActionLabel = (action: string): string => { const labels: Record = { created: i18n.t('admin.promoOffers.actions.created'), claimed: i18n.t('admin.promoOffers.actions.claimed'), consumed: i18n.t('admin.promoOffers.actions.consumed'), disabled: i18n.t('admin.promoOffers.actions.disabled'), }; return labels[action] || action; }; const getActionColor = (action: string): string => { const colors: Record = { created: 'bg-accent-500/20 text-accent-400', claimed: 'bg-success-500/20 text-success-400', consumed: 'bg-accent-500/20 text-accent-400', disabled: 'bg-dark-600 text-dark-400', }; return colors[action] || 'bg-dark-600 text-dark-400'; }; const getOfferTypeIcon = (offerType: string): string => { return OFFER_TYPE_CONFIG[offerType as OfferType]?.icon || '🎁'; }; const getOfferTypeLabel = (offerType: string): string => { const config = OFFER_TYPE_CONFIG[offerType as OfferType]; return config ? i18n.t(config.labelKey) : offerType; }; export default function AdminPromoOffers() { const { t } = useTranslation(); const navigate = useNavigate(); const { capabilities } = usePlatform(); const [activeTab, setActiveTab] = useState<'templates' | 'logs'>('templates'); // Queries const { data: templatesData, isLoading: templatesLoading } = useQuery({ queryKey: ['admin-promo-templates'], queryFn: promoOffersApi.getTemplates, }); const { data: logsData, isLoading: logsLoading } = useQuery({ queryKey: ['admin-promo-logs'], queryFn: () => promoOffersApi.getLogs({ limit: 100 }), enabled: activeTab === 'logs', }); const templates = templatesData?.items || []; const logs = logsData?.items || []; return (
{/* Header */}
{/* Show back button only on web, not in Telegram Mini App */} {!capabilities.hasBackButton && ( )}

{t('admin.promoOffers.title')}

{t('admin.promoOffers.subtitle')}

{/* Tabs */}
{/* Templates Tab */} {activeTab === 'templates' && ( <> {templatesLoading ? (
) : templates.length === 0 ? (

{t('admin.promoOffers.noData.templates')}

) : (
{templates.map((template) => (
{getOfferTypeIcon(template.offer_type)}

{template.name}

{getOfferTypeLabel(template.offer_type)}
{template.discount_percent > 0 && (
{t('admin.promoOffers.table.discount')}: {template.discount_percent}%
)}
{t('admin.promoOffers.table.offerDuration')}: {t('admin.promoOffers.table.hoursShort', { hours: template.valid_hours })}
{template.active_discount_hours && (
{t('admin.promoOffers.table.discountDuration')}: {t('admin.promoOffers.table.hoursShort', { hours: template.active_discount_hours, })}
)} {template.test_duration_hours && (
{t('admin.promoOffers.table.testAccess')}: {t('admin.promoOffers.table.hoursShort', { hours: template.test_duration_hours, })}
)}
{template.is_active ? ( {t('admin.promoOffers.status.active')} ) : ( {t('admin.promoOffers.status.inactive')} )}
))}
)} )} {/* Logs Tab */} {activeTab === 'logs' && ( <> {logsLoading ? (
) : logs.length === 0 ? (

{t('admin.promoOffers.noData.logs')}

) : (
{logs.map((log: PromoOfferLog) => (
{log.user?.full_name || log.user?.username || `User #${log.user_id}`} {getActionLabel(log.action)}
{log.source && {getOfferTypeLabel(log.source)}} {log.percent && log.percent > 0 && ( {log.percent}% )}
{formatDateTime(log.created_at)}
))}
)} )}
); }