import React, { useState, useEffect } from "react"; import MDEditor from "@uiw/react-md-editor"; import { motion } from "framer-motion"; import { ArrowLeft, Save, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Toast, ToastContainer } from "@/components/ui/toast"; import { api, type ClaudeMdFile } from "@/lib/api"; import { cn } from "@/lib/utils"; import { useTranslation } from "@/hooks/useTranslation"; interface ClaudeFileEditorProps { /** * The CLAUDE.md file to edit */ file: ClaudeMdFile; /** * Callback to go back to the previous view */ onBack: () => void; /** * Optional className for styling */ className?: string; } /** * ClaudeFileEditor component for editing project-specific CLAUDE.md files * * @example * setEditingFile(null)} * /> */ export const ClaudeFileEditor: React.FC = ({ file, onBack, className, }) => { const { t } = useTranslation(); const [content, setContent] = useState(""); const [originalContent, setOriginalContent] = useState(""); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [toast, setToast] = useState<{ message: string; type: "success" | "error" } | null>(null); const hasChanges = content !== originalContent; // Load the file content on mount useEffect(() => { loadFileContent(); }, [file.absolute_path]); const loadFileContent = async () => { try { setLoading(true); setError(null); const fileContent = await api.readClaudeMdFile(file.absolute_path); setContent(fileContent); setOriginalContent(fileContent); } catch (err) { console.error("Failed to load file:", err); setError(t('loadFileFailed')); } finally { setLoading(false); } }; const handleSave = async () => { try { setSaving(true); setError(null); setToast(null); await api.saveClaudeMdFile(file.absolute_path, content); setOriginalContent(content); setToast({ message: t('fileSavedSuccess'), type: "success" }); } catch (err) { console.error("Failed to save file:", err); setError(t('saveFileFailed')); setToast({ message: t('saveFileFailed'), type: "error" }); } finally { setSaving(false); } }; const handleBack = () => { if (hasChanges) { const confirmLeave = window.confirm( t('unsavedChangesConfirm') ); if (!confirmLeave) return; } onBack(); }; return (
{/* Header */}

{file.relative_path}

{t('editProjectSpecificPrompt')}

{/* Error display */} {error && ( {error} )} {/* Editor */}
{loading ? (
) : (
setContent(val || "")} preview="edit" height="100%" visibleDragbar={false} />
)}
{/* Toast Notification */} {toast && ( setToast(null)} /> )}
); };