🐛 fix: Optimize Task Manager to use milliseconds to calculate timers, and simplify import statements

This commit is contained in:
web 2025-09-14 02:58:44 -07:00
parent f700be095a
commit d8fa13bebc
2 changed files with 9 additions and 47 deletions

View File

@ -1,11 +1,7 @@
'use client'; 'use client';
import { ProTable } from '@/components/pro-table'; import { ProTable, ProTableActions } from '@/components/pro-table';
import { import { getBatchSendEmailTaskList, stopBatchSendEmailTask } from '@/services/admin/marketing';
getBatchSendEmailTaskList,
getBatchSendEmailTaskStatus,
stopBatchSendEmailTask,
} from '@/services/admin/marketing';
import { formatDate } from '@/utils/common'; import { formatDate } from '@/utils/common';
import { Badge } from '@workspace/ui/components/badge'; import { Badge } from '@workspace/ui/components/badge';
import { Button } from '@workspace/ui/components/button'; import { Button } from '@workspace/ui/components/button';
@ -26,45 +22,23 @@ import {
} from '@workspace/ui/components/sheet'; } from '@workspace/ui/components/sheet';
import { Icon } from '@workspace/ui/custom-components/icon'; import { Icon } from '@workspace/ui/custom-components/icon';
import { useTranslations } from 'next-intl'; import { useTranslations } from 'next-intl';
import { useState } from 'react'; import { useRef, useState } from 'react';
import { toast } from 'sonner'; import { toast } from 'sonner';
export default function EmailTaskManager() { export default function EmailTaskManager() {
const t = useTranslations('marketing'); const t = useTranslations('marketing');
const [refreshing, setRefreshing] = useState<Record<number, boolean>>({}); const ref = useRef<ProTableActions>(null);
const [selectedTask, setSelectedTask] = useState<API.BatchSendEmailTask | null>(null); const [selectedTask, setSelectedTask] = useState<API.BatchSendEmailTask | null>(null);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
// 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) => { const stopTask = async (taskId: number) => {
try { try {
await stopBatchSendEmailTask({ await stopBatchSendEmailTask({
id: taskId, id: taskId,
}); });
toast.success(t('taskStoppedSuccessfully')); toast.success(t('taskStoppedSuccessfully'));
await refreshTaskStatus(taskId); ref.current?.refresh();
} catch (error) { } catch (error) {
console.error('Failed to stop task:', error); console.error('Failed to stop task:', error);
toast.error(t('failedToStopTask')); toast.error(t('failedToStopTask'));
@ -111,6 +85,7 @@ export default function EmailTaskManager() {
<ScrollArea className='-mx-6 h-[calc(100dvh-48px-36px-env(safe-area-inset-top))] px-6'> <ScrollArea className='-mx-6 h-[calc(100dvh-48px-36px-env(safe-area-inset-top))] px-6'>
<div className='mt-4 space-y-4'> <div className='mt-4 space-y-4'>
<ProTable<API.BatchSendEmailTask, API.GetBatchSendEmailTaskListParams> <ProTable<API.BatchSendEmailTask, API.GetBatchSendEmailTaskListParams>
action={ref}
columns={[ columns={[
{ {
accessorKey: 'subject', accessorKey: 'subject',
@ -265,19 +240,6 @@ export default function EmailTaskManager() {
</ScrollArea> </ScrollArea>
</DialogContent> </DialogContent>
</Dialog>, </Dialog>,
<Button
key='refresh'
variant='outline'
size='icon'
onClick={() => refreshTaskStatus(row.id)}
disabled={refreshing[row.id]}
>
{refreshing[row.id] ? (
<Icon icon='mdi:loading' className='h-3 w-3 animate-spin' />
) : (
<Icon icon='mdi:refresh' className='h-3 w-3' />
)}
</Button>,
...([0, 1].includes(row.status) ...([0, 1].includes(row.status)
? [ ? [
<Button key='stop' variant='destructive' onClick={() => stopTask(row.id)}> <Button key='stop' variant='destructive' onClick={() => stopTask(row.id)}>

View File

@ -94,11 +94,11 @@ export default function QuotaBroadcastForm() {
let end_time: number = 0; let end_time: number = 0;
if (formData.start_time) { if (formData.start_time) {
start_time = Math.floor(new Date(formData.start_time).getTime() / 1000); start_time = new Date(formData.start_time).getTime();
} }
if (formData.end_time) { if (formData.end_time) {
end_time = Math.floor(new Date(formData.end_time).getTime() / 1000); end_time = new Date(formData.end_time).getTime();
} }
const response = await queryQuotaTaskPreCount({ const response = await queryQuotaTaskPreCount({