import React, { useState, useEffect } from "react"; import { motion } from "framer-motion"; import { Settings, Save, Trash2, HardDrive, AlertCircle } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { SelectComponent, type SelectOption } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import { api, type CheckpointStrategy } from "@/lib/api"; import { cn } from "@/lib/utils"; import { useTranslation } from "@/hooks/useTranslation"; interface CheckpointSettingsProps { sessionId: string; projectId: string; projectPath: string; onClose?: () => void; className?: string; } /** * CheckpointSettings component for managing checkpoint configuration * * @example * */ export const CheckpointSettings: React.FC = ({ sessionId, projectId, projectPath, onClose, className, }) => { const { t } = useTranslation(); const [autoCheckpointEnabled, setAutoCheckpointEnabled] = useState(true); const [checkpointStrategy, setCheckpointStrategy] = useState("smart"); const [totalCheckpoints, setTotalCheckpoints] = useState(0); const [keepCount, setKeepCount] = useState(10); const [isLoading, setIsLoading] = useState(false); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const [successMessage, setSuccessMessage] = useState(null); const strategyOptions: SelectOption[] = [ { value: "manual", label: t('checkpoint.manualOnly') }, { value: "per_prompt", label: t('checkpoint.afterEachPrompt') }, { value: "per_tool_use", label: t('checkpoint.afterToolUse') }, { value: "smart", label: t('checkpoint.smart') }, ]; useEffect(() => { loadSettings(); }, [sessionId, projectId, projectPath]); const loadSettings = async () => { try { setIsLoading(true); setError(null); const settings = await api.getCheckpointSettings(sessionId, projectId, projectPath); setAutoCheckpointEnabled(settings.auto_checkpoint_enabled); setCheckpointStrategy(settings.checkpoint_strategy); setTotalCheckpoints(settings.total_checkpoints); } catch (err) { console.error("Failed to load checkpoint settings:", err); setError(t('checkpoint.checkpointSettingsFailed')); } finally { setIsLoading(false); } }; const handleSaveSettings = async () => { try { setIsSaving(true); setError(null); setSuccessMessage(null); await api.updateCheckpointSettings( sessionId, projectId, projectPath, autoCheckpointEnabled, checkpointStrategy ); setSuccessMessage(t('messages.saveSuccess')); setTimeout(() => setSuccessMessage(null), 3000); } catch (err) { console.error("Failed to save checkpoint settings:", err); setError(t('checkpoint.saveCheckpointSettingsFailed')); } finally { setIsSaving(false); } }; const handleCleanup = async () => { try { setIsLoading(true); setError(null); setSuccessMessage(null); const removed = await api.cleanupOldCheckpoints( sessionId, projectId, projectPath, keepCount ); setSuccessMessage(t('checkpoint.removedOldCheckpoints', { count: removed })); setTimeout(() => setSuccessMessage(null), 3000); // Reload settings to get updated count await loadSettings(); } catch (err) { console.error("Failed to cleanup checkpoints:", err); setError(t('checkpoint.cleanupCheckpointsFailed')); } finally { setIsLoading(false); } }; return (

{t('checkpoint.checkpointSettingsTitle')}

{onClose && ( )}
{/* Experimental Feature Warning */}

{t('checkpoint.experimentalFeature')}

{t('checkpoint.checkpointWarning')}

{error && (
{error}
)} {successMessage && ( {successMessage} )}
{/* Auto-checkpoint toggle */}

{t('checkpoint.automaticCheckpointsDesc')}

{/* Checkpoint strategy */}
setCheckpointStrategy(value as CheckpointStrategy)} options={strategyOptions} disabled={isLoading || !autoCheckpointEnabled} />

{checkpointStrategy === "manual" && t('checkpoint.manualOnlyDesc')} {checkpointStrategy === "per_prompt" && t('checkpoint.afterEachPromptDesc')} {checkpointStrategy === "per_tool_use" && t('checkpoint.afterToolUseDesc')} {checkpointStrategy === "smart" && t('checkpoint.smartDesc')}

{/* Save button */}

{t('checkpoint.totalCheckpoints')}: {totalCheckpoints}

{/* Cleanup settings */}
setKeepCount(parseInt(e.target.value) || 10)} disabled={isLoading} className="flex-1" />

{t('checkpoint.removeOldCheckpoints', { count: keepCount })}

); };