import type { ColumnDef } from '@tanstack/react-table';
import { ArrowDownCircle, Clock, Code, FileArchive, ToggleRight, type LucideIcon } from 'lucide-react';
import {
deleteScheduleTaskInput,
type Schedule,
type Task,
useDeleteServerScheduleTask,
} from '@/api/server/schedules/queries';
import { useCurrentServerUuid } from '@/api/server/queries';
import Can from '@/components/elements/Can';
import Icon from '@/components/elements/Icon';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
import { actionsColumn, DeleteAction, EditAction, RowActions } from '@/components/elements/table/RowActions';
import { Dialog } from '@/components/elements/dialog';
import TaskDetailsModal from '@/components/server/schedules/TaskDetailsModal';
const getActionDetails = (action: string): [string, LucideIcon] => {
switch (action) {
case 'command':
return ['Send Command', Code];
case 'power':
return ['Send Power Action', ToggleRight];
case 'backup':
return ['Create Backup', FileArchive];
default:
return ['Unknown Action', Code];
}
};
const TaskIdentityCell = ({ task }: { task: Task }) => {
const [title, icon] = getActionDetails(task.attributes.action);
return (
{title}
{task.attributes.payload ? (
{task.attributes.payload}
) : null}
);
};
const TaskActionsCell = ({ schedule, task }: { schedule: Schedule; task: Task }) => {
const uuid = useCurrentServerUuid()!;
const deleteTask = useDeleteServerScheduleTask(schedule);
const name = `task ${task.attributes.sequence_id} (${getActionDetails(task.attributes.action)[0]})`;
const onConfirmDeletion = async (close: () => void) => {
try {
await deleteTask.mutateAsync(deleteScheduleTaskInput(uuid, schedule, task.attributes.id));
close();
} catch {
// Error toast is handled by the mutation.
}
};
return (
}>
{({ open, onClose }) => (
)}
onConfirmDeletion(close)}
trigger={({ onClick }) => }
>
Are you sure you want to delete this task? This action cannot be undone.
);
};
export const scheduleTaskColumns = (schedule: Schedule): ColumnDef[] => [
{
id: 'action',
accessorFn: (task) => getActionDetails(task.attributes.action)[0],
header: ({ column }) => ,
cell: ({ row }) => ,
},
{
id: 'delay',
accessorFn: (task) => task.attributes.time_offset,
header: ({ column }) => ,
cell: ({ row }) =>
row.original.attributes.sequence_id > 1 && row.original.attributes.time_offset > 0 ? (
{row.original.attributes.time_offset}s later
) : (
'-'
),
meta: { headerClassName: 'hidden md:table-cell w-32', cellClassName: 'hidden md:table-cell w-32' },
},
{
id: 'continue_on_failure',
accessorFn: (task) => task.attributes.continue_on_failure,
header: 'Failure behavior',
cell: ({ row }) =>
row.original.attributes.continue_on_failure ? (
Continues
) : (
Stops
),
enableSorting: false,
meta: { headerClassName: 'hidden lg:table-cell w-36', cellClassName: 'hidden lg:table-cell w-36' },
},
actionsColumn(2, (task) => ),
];