'use client'; import { ProTable } from '@/components/pro-table'; import { getBatchSendEmailTaskList, getBatchSendEmailTaskStatus, stopBatchSendEmailTask, } from '@/services/admin/marketing'; import { Badge } from '@workspace/ui/components/badge'; import { Button } from '@workspace/ui/components/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from '@workspace/ui/components/dialog'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { Icon } from '@workspace/ui/custom-components/icon'; import { formatDate } from '@workspace/ui/utils'; import { useTranslations } from 'next-intl'; import { useState } from 'react'; import { toast } from 'sonner'; export default function EmailTaskManager() { const t = useTranslations('marketing'); const [refreshing, setRefreshing] = useState>({}); const [selectedTask, setSelectedTask] = useState(null); // Get task status const refreshTaskStatus = async (taskId: number) => { setRefreshing((prev) => ({ ...prev, [taskId]: true })); try { const response = await getBatchSendEmailTaskStatus({ id: taskId, }); const taskStatus = response.data?.data; if (taskStatus) { // Just show success message, ProTable will auto-refresh toast.success(t('taskStatusRefreshed')); } } catch (error) { console.error('Failed to refresh task status:', error); toast.error(t('failedToRefreshTaskStatus')); } finally { setRefreshing((prev) => ({ ...prev, [taskId]: false })); } }; // Stop task const stopTask = async (taskId: number) => { try { await stopBatchSendEmailTask({ id: taskId, }); toast.success(t('taskStoppedSuccessfully')); await refreshTaskStatus(taskId); } catch (error) { console.error('Failed to stop task:', error); toast.error(t('failedToStopTask')); } }; const getStatusBadge = (status: number) => { const statusConfig = { 0: { label: t('notStarted'), variant: 'secondary' as const }, 1: { label: t('inProgress'), variant: 'default' as const }, 2: { label: t('completed'), variant: 'default' as const }, }; const config = statusConfig[status as keyof typeof statusConfig] || { label: `${t('status')} ${status}`, variant: 'secondary' as const, }; return {config.label}; }; return (

{t('emailTaskManager')}

{t('viewAndManageEmailBroadcastTasks')}

{t('emailBroadcastTasks')}
columns={[ { accessorKey: 'subject', header: t('subject'), size: 200, cell: ({ row }) => (
{row.getValue('subject') as string}
), }, { accessorKey: 'scope', header: t('recipientType'), size: 120, cell: ({ row }) => { const scope = row.getValue('scope') as string; const scopeLabels = { all: t('allUsers'), active: t('subscribedUsers'), expired: t('expiredUsers'), none: t('nonSubscribers'), skip: t('specificUsers'), }; return scopeLabels[scope as keyof typeof scopeLabels] || scope; }, }, { accessorKey: 'status', header: t('status'), size: 100, cell: ({ row }) => getStatusBadge(row.getValue('status') as number), }, { accessorKey: 'progress', header: t('progress'), size: 150, cell: ({ row }) => { const task = row.original as API.BatchSendEmailTask; const progress = task.total > 0 ? (task.current / task.total) * 100 : 0; return (
{task.current} / {task.total} {progress.toFixed(1)}%
); }, }, { accessorKey: 'created_at', header: t('createdAt'), size: 150, cell: ({ row }) => { const createdAt = row.getValue('created_at') as number; return formatDate(createdAt); }, }, { accessorKey: 'scheduled', header: t('sendTime'), size: 150, cell: ({ row }) => { const scheduled = row.getValue('scheduled') as number; return scheduled && scheduled > 0 ? formatDate(scheduled) : '--'; }, }, ]} request={async (pagination, filters) => { const response = await getBatchSendEmailTaskList({ ...filters, page: pagination.page, size: pagination.size, }); return { list: response.data?.data?.list || [], total: response.data?.data?.total || 0, }; }} params={[ { key: 'status', placeholder: t('status'), options: [ { label: t('notStarted'), value: '0' }, { label: t('inProgress'), value: '1' }, { label: t('completed'), value: '2' }, ], }, { key: 'scope', placeholder: t('sendScope'), options: [ { label: t('allUsers'), value: 'all' }, { label: t('subscribedUsers'), value: 'active' }, { label: t('expiredUsers'), value: 'expired' }, { label: t('nonSubscribers'), value: 'none' }, { label: t('specificUsers'), value: 'skip' }, ], }, ]} actions={{ render: (row) => { return [ {t('emailContent')} {selectedTask && (

{t('subject')}

{selectedTask.subject}

{t('content')}

{selectedTask.additional && (

{t('additionalRecipients')}

{selectedTask.additional}

)}
)}
, , ...([0, 1].includes(row.status) ? [ , ] : []), ]; }, }} />
); }