import InlineCode from '@/components/InlineCode'; import { txToast } from '@/components/TxToaster'; import { Button } from '@/components/ui/button'; import { useOpenPromptDialog } from '@/hooks/dialogs'; import { useCloseAllSheets } from '@/hooks/sheets'; import { useGlobalStatus } from '@/hooks/status'; import { useBackendApi } from '@/hooks/fetch'; import { cn } from '@/lib/utils'; import { msToDuration } from '@/lib/dateTime'; import { PenLineIcon, PlayCircleIcon, PlusCircleIcon, XCircleIcon } from 'lucide-react'; import { useAdminPerms } from '@/hooks/auth'; //Prompt props const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; const timezoneDiffMessage = (

Server's timezone: {window.txConsts.serverTimezone}
Your timezone: {browserTimezone}
Either use relative times, or make sure the scheduled is based on the server timezone.

) const promptCommonProps = { suggestions: ['+5', '+10', '+15', '+30'], title: 'When should the server restart?', message: (<>

Possible formats:

{browserTimezone !== window.txConsts.serverTimezone && timezoneDiffMessage} ), placeholder: '+15', required: true, isWide: true, }; //Validate schedule time input for 24h format or relative time const validateSchedule = (input: string) => { if (input.startsWith('+')) { const minutes = parseInt(input.substring(1)); if (isNaN(minutes) || minutes < 1 || minutes >= 1440) { return false; } } else { const [hours, minutes] = input.split(':', 2).map(x => parseInt(x)); if ( typeof hours === 'undefined' || isNaN(hours) || hours < 0 || hours > 23 || typeof minutes === 'undefined' || isNaN(minutes) || minutes < 0 || minutes > 59 ) { return false; } } return true; } export default function ServerSchedule() { const closeAllSheets = useCloseAllSheets(); const openPromptDialog = useOpenPromptDialog(); const { hasPerm } = useAdminPerms(); const schedulerApi = useBackendApi({ method: 'POST', path: '/fxserver/schedule' }); const globalStatus = useGlobalStatus(); if (!globalStatus) { return

Next Restart:

loading...
} //Processing status const { scheduler } = globalStatus; let nextScheduledText = 'nothing scheduled'; let nextScheduledClasses = 'text-muted-foreground italic'; let disableAddEditBtn = false; let showCancelBtn = false; let showEnableBtn = false; const hasScheduledRestart = typeof scheduler.nextRelativeMs === 'number'; if (hasScheduledRestart) { const tempFlag = (scheduler.nextIsTemp) ? '(temp)' : ''; const relativeTime = msToDuration(scheduler.nextRelativeMs, { units: ['h', 'm'] }); const isLessThanMinute = scheduler.nextRelativeMs < 60_000; if (isLessThanMinute) { disableAddEditBtn = true; nextScheduledText = `right now ${tempFlag}`; } else { nextScheduledText = `in ${relativeTime} ${tempFlag}`; } if (scheduler.nextSkip) { nextScheduledClasses = 'text-muted-foreground line-through'; if (!isLessThanMinute) { showEnableBtn = true; } } else { nextScheduledClasses = 'text-warning-inline'; if (!isLessThanMinute) { showCancelBtn = true; } } } //Handlers const onScheduleSubmit = (input: string) => { closeAllSheets(); if (input.includes(',')) { txToast.error({ title: 'Invalid scheduled restart time.', msg: 'It looks like you are trying to schedule multiple restart times, which can only be done in the Settings page.\nThis input is for just the next (not persistent) restart.', }, { duration: 10000 }); return; } if (!validateSchedule(input)) { txToast.error(`Invalid schedule time: ${input}`) return; } schedulerApi({ data: { action: 'setNextTempSchedule', parameter: input }, toastLoadingMessage: 'Scheduling server restart...', }); } const handleEdit = () => { openPromptDialog({ ...promptCommonProps, onSubmit: onScheduleSubmit, submitLabel: 'Edit', }); } const handleAddSchedule = () => { openPromptDialog({ ...promptCommonProps, onSubmit: onScheduleSubmit, submitLabel: 'Schedule', }); } const handleCancel = () => { closeAllSheets(); schedulerApi({ data: { action: 'setNextSkip', parameter: true }, toastLoadingMessage: 'Cancelling next server restart...', }); } const handleEnable = () => { closeAllSheets(); schedulerApi({ data: { action: 'setNextSkip', parameter: false }, toastLoadingMessage: 'Enabling next server restart...', }); } const hasSchedulePerms = hasPerm('control.server'); return

Next Restart:

{nextScheduledText}
{hasScheduledRestart ? ( ) : ( )} {showCancelBtn && ( )} {showEnableBtn && ( )}
}