import { Card, CardContent } from "@/components/ui/card"; import { Checkbox } from "../ui/checkbox"; import type { DatabaseCleanupConfig } from "@/types/config"; import { formatDate } from "@/lib/utils"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../ui/select"; import { RefreshCw, Database } from "lucide-react"; interface DatabaseCleanupConfigFormProps { config: DatabaseCleanupConfig; setConfig: React.Dispatch>; onAutoSave?: (config: DatabaseCleanupConfig) => void; isAutoSaving?: boolean; } // Helper to calculate cleanup interval in hours (should match backend logic) function calculateCleanupInterval(retentionSeconds: number): number { const retentionDays = retentionSeconds / (24 * 60 * 60); if (retentionDays <= 1) { return 6; } else if (retentionDays <= 3) { return 12; } else if (retentionDays <= 7) { return 24; } else if (retentionDays <= 30) { return 48; } else { return 168; } } export function DatabaseCleanupConfigForm({ config, setConfig, onAutoSave, isAutoSaving = false, }: DatabaseCleanupConfigFormProps) { // Optimistically update nextRun when enabled or retention changes const handleChange = ( e: React.ChangeEvent ) => { const { name, value, type } = e.target; let newConfig = { ...config, [name]: type === "checkbox" ? (e.target as HTMLInputElement).checked : value, }; // If enabling or changing retention, recalculate nextRun if ( (name === "enabled" && (e.target as HTMLInputElement).checked) || (name === "retentionDays" && config.enabled) ) { const now = new Date(); const retentionSeconds = name === "retentionDays" ? Number(value) : Number(newConfig.retentionDays); const intervalHours = calculateCleanupInterval(retentionSeconds); const nextRun = new Date(now.getTime() + intervalHours * 60 * 60 * 1000); newConfig = { ...newConfig, nextRun, }; } // If disabling, clear nextRun if (name === "enabled" && !(e.target as HTMLInputElement).checked) { newConfig = { ...newConfig, nextRun: undefined, }; } setConfig(newConfig); if (onAutoSave) { onAutoSave(newConfig); } }; // Predefined retention periods (in seconds, like schedule intervals) const retentionOptions: { value: number; label: string }[] = [ { value: 86400, label: "1 day" }, // 24 * 60 * 60 { value: 259200, label: "3 days" }, // 3 * 24 * 60 * 60 { value: 604800, label: "7 days" }, // 7 * 24 * 60 * 60 { value: 1209600, label: "14 days" }, // 14 * 24 * 60 * 60 { value: 2592000, label: "30 days" }, // 30 * 24 * 60 * 60 { value: 5184000, label: "60 days" }, // 60 * 24 * 60 * 60 { value: 7776000, label: "90 days" }, // 90 * 24 * 60 * 60 ]; return ( {isAutoSaving && (
Auto-saving...
)}
handleChange({ target: { name: "enabled", type: "checkbox", checked: Boolean(checked), value: "", }, } as React.ChangeEvent) } />
{config.enabled && (

Activities and events older than this period will be automatically deleted.

Cleanup Frequency: The cleanup process runs automatically at optimal intervals: shorter retention periods trigger more frequent cleanups, longer periods trigger less frequent cleanups.

)}
{config.lastRun ? formatDate(config.lastRun) : "Never"}
{config.enabled && (
{config.nextRun ? formatDate(config.nextRun) : config.enabled ? "Calculating..." : "Never"}
)}
); }