-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathClaudeFileEditor.tsx
More file actions
180 lines (168 loc) · 5.32 KB
/
ClaudeFileEditor.tsx
File metadata and controls
180 lines (168 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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";
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
* <ClaudeFileEditor
* file={claudeMdFile}
* onBack={() => setEditingFile(null)}
* />
*/
export const ClaudeFileEditor: React.FC<ClaudeFileEditorProps> = ({
file,
onBack,
className,
}) => {
const [content, setContent] = useState<string>("");
const [originalContent, setOriginalContent] = useState<string>("");
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(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("Failed to load CLAUDE.md file");
} finally {
setLoading(false);
}
};
const handleSave = async () => {
try {
setSaving(true);
setError(null);
setToast(null);
await api.saveClaudeMdFile(file.absolute_path, content);
setOriginalContent(content);
setToast({ message: "File saved successfully", type: "success" });
} catch (err) {
console.error("Failed to save file:", err);
setError("Failed to save CLAUDE.md file");
setToast({ message: "Failed to save file", type: "error" });
} finally {
setSaving(false);
}
};
const handleBack = () => {
if (hasChanges) {
const confirmLeave = window.confirm(
"You have unsaved changes. Are you sure you want to leave?"
);
if (!confirmLeave) return;
}
onBack();
};
return (
<div className={cn("flex flex-col h-full bg-background", className)}>
<div className="w-full max-w-5xl mx-auto flex flex-col h-full">
{/* Header */}
<motion.div
initial={{ opacity: 0, y: -20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.3 }}
className="flex items-center justify-between p-4 border-b border-border"
>
<div className="flex items-center space-x-3">
<Button
variant="ghost"
size="icon"
onClick={handleBack}
className="h-8 w-8"
>
<ArrowLeft className="h-4 w-4" />
<span className="sr-only">Back</span>
</Button>
<div className="min-w-0 flex-1">
<h2 className="text-lg font-semibold truncate">{file.relative_path}</h2>
<p className="text-xs text-muted-foreground">
Edit project-specific Claude Code system prompt
</p>
</div>
</div>
<Button
onClick={handleSave}
disabled={!hasChanges || saving}
size="sm"
>
{saving ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Save className="mr-2 h-4 w-4" />
)}
{saving ? "Saving..." : "Save"}
</Button>
</motion.div>
{/* Error display */}
{error && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
className="mx-4 mt-4 rounded-lg border border-destructive/50 bg-destructive/10 p-3 text-xs text-destructive"
>
{error}
</motion.div>
)}
{/* Editor */}
<div className="flex-1 p-4 overflow-hidden">
{loading ? (
<div className="flex items-center justify-center h-full">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : (
<div className="h-full rounded-lg border border-border overflow-hidden shadow-sm" data-color-mode="dark">
<MDEditor
value={content}
onChange={(val) => setContent(val || "")}
preview="edit"
height="100%"
visibleDragbar={false}
/>
</div>
)}
</div>
</div>
{/* Toast Notification */}
<ToastContainer>
{toast && (
<Toast
message={toast.message}
type={toast.type}
onDismiss={() => setToast(null)}
/>
)}
</ToastContainer>
</div>
);
};