import { ActionIcon, Badge, Button, Code, Group, Select, Stack, Text, TextInput, ThemeIcon, } from '@mantine/core'; import { useMediaQuery } from '@mantine/hooks'; import { modals } from '@mantine/modals'; import { showNotification } from '@mantine/notifications'; import { IconAlertTriangle, IconClock, IconRefresh, IconRun, IconSearch, IconTools, IconTrash, } from '@tabler/icons-react'; import { __, _n, sprintf } from '@wordpress/i18n'; import { useContextMenu } from 'mantine-contextmenu'; import { DataTable } from 'mantine-datatable'; import { useMemo, useState } from 'react'; import useSWR from 'swr'; import { post } from '../../utils/post'; import CronAction from './cron-actions'; interface CronRecord { id: string; hook_name: string; signature: string; next_run: string; next_run_timestamp: number; schedule: string; schedule_key: string; interval: number; args: any[]; } const scheduleColors: Record = { single: 'gray', hourly: 'blue', twicedaily: 'teal', daily: 'green', weekly: 'violet', }; function getScheduleColor(key: string): string { return scheduleColors[key] || 'gray'; } function getTimeStatus(timestamp: number) { const now = Date.now() / 1000; const diff = timestamp - now; if (diff < 0) { const ago = Math.abs(diff); if (ago < 60) { return { label: __('just now', 'scotty'), color: 'red' }; } if (ago < 3600) { return { label: `${Math.floor(ago / 60)}m ${__('ago', 'scotty')}`, color: 'red', }; } if (ago < 86400) { return { label: `${Math.floor(ago / 3600)}h ${__('ago', 'scotty')}`, color: 'red', }; } return { label: `${Math.floor(ago / 86400)}d ${__('ago', 'scotty')}`, color: 'red', }; } if (diff < 3600) { return { label: `${__('in', 'scotty')} ${Math.floor(diff / 60)}m`, color: 'orange', }; } if (diff < 86400) { return { label: `${__('in', 'scotty')} ${Math.floor(diff / 3600)}h`, color: 'blue', }; } return { label: `${__('in', 'scotty')} ${Math.floor(diff / 86400)}d`, color: 'dimmed', }; } function formatInterval(seconds: number): string { if (seconds === 0) { return '-'; } if (seconds < 3600) { return `${Math.floor(seconds / 60)} min`; } if (seconds < 86400) { return `${Math.floor(seconds / 3600)} h`; } return `${Math.floor(seconds / 86400)} d`; } export function Cron() { const { data, isLoading, isValidating, mutate } = useSWR('cron', post); const [selectedRecords, setSelectedRecords] = useState([]); const { showContextMenu } = useContextMenu(); const isTouch = useMediaQuery('(pointer: coarse)'); const [bulkAction, setBulkAction] = useState(''); const [search, setSearch] = useState(''); const [expandedIds, setExpandedIds] = useState([]); const [sortStatus, setSortStatus] = useState<{ columnAccessor: string; direction: 'asc' | 'desc'; }>({ columnAccessor: 'next_run_timestamp', direction: 'asc', }); const filteredAndSorted = useMemo(() => { if (!data) { return []; } let result = data; if (search) { const q = search.toLowerCase(); result = result.filter((r) => r.hook_name.toLowerCase().includes(q)); } result = [...result].sort((a, b) => { const col = sortStatus.columnAccessor; const dir = sortStatus.direction === 'asc' ? 1 : -1; const aVal = a[col as keyof CronRecord]; const bVal = b[col as keyof CronRecord]; if (typeof aVal === 'number' && typeof bVal === 'number') { return (aVal - bVal) * dir; } return String(aVal).localeCompare(String(bVal)) * dir; }); return result; }, [data, search, sortStatus]); const handleExecuteCron = async (record: CronRecord) => { await CronAction.cron({ hook_name: record.hook_name, signature: record.signature, }).execute(); showNotification({ // translators: %s: cron hook name. message: sprintf(__('Executed cron %s', 'scotty'), record.hook_name), withBorder: true, color: 'green', }); await mutate(undefined, { revalidate: true }); }; const handleDeleteCron = async (record: CronRecord) => { modals.openConfirmModal({ title: __('Delete cron job', 'scotty'), size: 'sm', radius: 'md', centered: true, children: ( {__('Are you sure you want to delete this cron job?', 'scotty')} {record.hook_name} {'\n'} {record.schedule} {__('This action cannot be undone.', 'scotty')} ), labels: { confirm: __('Delete', 'scotty'), cancel: __('Cancel', 'scotty'), }, confirmProps: { color: 'red' }, onConfirm: async () => { await CronAction.cron({ hook_name: record.hook_name, signature: record.signature, }).delete(); showNotification({ // translators: %s: cron hook name. message: sprintf(__('Deleted cron %s', 'scotty'), record.hook_name), withBorder: true, color: 'orange', icon: , }); await mutate(undefined, { revalidate: true }); }, }); }; const handleBulkAction = async () => { switch (bulkAction) { case 'run': for (const record of selectedRecords) { await handleExecuteCron(record); } break; case 'delete': for (const record of selectedRecords) { await CronAction.cron({ hook_name: record.hook_name, signature: record.signature, }).delete(); } showNotification({ message: sprintf( // translators: %d: number of cron jobs deleted. _n('Deleted %d cron job', 'Deleted %d cron jobs', selectedRecords.length, 'scotty'), selectedRecords.length ), withBorder: true, color: 'orange', icon: , }); break; default: break; } await mutate(undefined, { revalidate: true }); setBulkAction(''); setSelectedRecords([]); }; return (