2025-09-13 21:45:34 +08:00
|
|
|
import React, { useEffect, useState } from "react";
|
2025-09-06 23:13:01 +08:00
|
|
|
import JsonEditor from "../JsonEditor";
|
2025-09-17 09:47:44 +08:00
|
|
|
import { X, Save } from "lucide-react";
|
2025-09-06 23:13:01 +08:00
|
|
|
|
|
|
|
|
interface ClaudeConfigEditorProps {
|
|
|
|
|
value: string;
|
|
|
|
|
onChange: (value: string) => void;
|
2025-09-16 22:59:00 +08:00
|
|
|
useCommonConfig: boolean;
|
|
|
|
|
onCommonConfigToggle: (checked: boolean) => void;
|
|
|
|
|
commonConfigSnippet: string;
|
|
|
|
|
onCommonConfigSnippetChange: (value: string) => void;
|
|
|
|
|
commonConfigError: string;
|
2025-09-06 23:13:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ClaudeConfigEditor: React.FC<ClaudeConfigEditorProps> = ({
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
2025-09-16 22:59:00 +08:00
|
|
|
useCommonConfig,
|
|
|
|
|
onCommonConfigToggle,
|
|
|
|
|
commonConfigSnippet,
|
|
|
|
|
onCommonConfigSnippetChange,
|
|
|
|
|
commonConfigError,
|
2025-09-06 23:13:01 +08:00
|
|
|
}) => {
|
2025-09-13 21:45:34 +08:00
|
|
|
const [isDarkMode, setIsDarkMode] = useState(false);
|
2025-09-16 22:59:00 +08:00
|
|
|
const [isCommonConfigModalOpen, setIsCommonConfigModalOpen] = useState(false);
|
2025-09-13 21:45:34 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// 检测暗色模式
|
|
|
|
|
const checkDarkMode = () => {
|
|
|
|
|
setIsDarkMode(document.documentElement.classList.contains("dark"));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
checkDarkMode();
|
|
|
|
|
|
|
|
|
|
// 监听暗色模式变化
|
|
|
|
|
const observer = new MutationObserver((mutations) => {
|
|
|
|
|
mutations.forEach((mutation) => {
|
|
|
|
|
if (mutation.attributeName === "class") {
|
|
|
|
|
checkDarkMode();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observer.observe(document.documentElement, {
|
|
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: ["class"],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, []);
|
2025-09-16 22:59:00 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (commonConfigError && !isCommonConfigModalOpen) {
|
|
|
|
|
setIsCommonConfigModalOpen(true);
|
|
|
|
|
}
|
|
|
|
|
}, [commonConfigError, isCommonConfigModalOpen]);
|
|
|
|
|
|
2025-09-17 09:47:44 +08:00
|
|
|
// 支持按下 ESC 关闭弹窗
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isCommonConfigModalOpen) return;
|
|
|
|
|
|
|
|
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
closeModal();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener("keydown", onKeyDown);
|
|
|
|
|
return () => window.removeEventListener("keydown", onKeyDown);
|
|
|
|
|
}, [isCommonConfigModalOpen]);
|
|
|
|
|
|
2025-09-16 22:59:00 +08:00
|
|
|
const closeModal = () => {
|
|
|
|
|
setIsCommonConfigModalOpen(false);
|
|
|
|
|
};
|
2025-09-06 23:13:01 +08:00
|
|
|
return (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="settingsConfig"
|
2025-09-13 21:45:34 +08:00
|
|
|
className="block text-sm font-medium text-gray-900 dark:text-gray-100"
|
2025-09-06 23:13:01 +08:00
|
|
|
>
|
|
|
|
|
Claude Code 配置 (JSON) *
|
|
|
|
|
</label>
|
2025-09-13 21:45:34 +08:00
|
|
|
<label className="inline-flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400 cursor-pointer">
|
2025-09-06 23:13:01 +08:00
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
2025-09-16 22:59:00 +08:00
|
|
|
checked={useCommonConfig}
|
|
|
|
|
onChange={(e) => onCommonConfigToggle(e.target.checked)}
|
2025-09-13 21:45:34 +08:00
|
|
|
className="w-4 h-4 text-blue-500 bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700 rounded focus:ring-blue-500 dark:focus:ring-blue-400 focus:ring-2"
|
2025-09-06 23:13:01 +08:00
|
|
|
/>
|
2025-09-16 22:59:00 +08:00
|
|
|
写入通用配置
|
2025-09-06 23:13:01 +08:00
|
|
|
</label>
|
|
|
|
|
</div>
|
2025-09-16 22:59:00 +08:00
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setIsCommonConfigModalOpen(true)}
|
|
|
|
|
className="text-xs text-blue-500 dark:text-blue-400 hover:underline"
|
|
|
|
|
>
|
|
|
|
|
编辑通用配置
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{commonConfigError && !isCommonConfigModalOpen && (
|
|
|
|
|
<p className="text-xs text-red-500 dark:text-red-400 text-right">
|
|
|
|
|
{commonConfigError}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-09-06 23:13:01 +08:00
|
|
|
<JsonEditor
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={onChange}
|
2025-09-13 21:45:34 +08:00
|
|
|
darkMode={isDarkMode}
|
2025-09-06 23:13:01 +08:00
|
|
|
placeholder={`{
|
|
|
|
|
"env": {
|
2025-09-12 12:04:19 +08:00
|
|
|
"ANTHROPIC_BASE_URL": "https://your-api-endpoint.com",
|
|
|
|
|
"ANTHROPIC_AUTH_TOKEN": "your-api-key-here"
|
2025-09-06 23:13:01 +08:00
|
|
|
}
|
|
|
|
|
}`}
|
|
|
|
|
rows={12}
|
|
|
|
|
/>
|
2025-09-13 21:45:34 +08:00
|
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">
|
2025-09-06 23:13:01 +08:00
|
|
|
完整的 Claude Code settings.json 配置内容
|
|
|
|
|
</p>
|
2025-09-16 22:59:00 +08:00
|
|
|
{isCommonConfigModalOpen && (
|
2025-09-17 09:47:44 +08:00
|
|
|
<div
|
|
|
|
|
className="fixed inset-0 z-50 flex items-center justify-center"
|
|
|
|
|
onMouseDown={(e) => {
|
|
|
|
|
if (e.target === e.currentTarget) closeModal();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{/* Backdrop - 统一背景样式 */}
|
|
|
|
|
<div className="absolute inset-0 bg-black/50 dark:bg-black/70 backdrop-blur-sm" />
|
|
|
|
|
|
|
|
|
|
{/* Modal - 统一窗口样式 */}
|
|
|
|
|
<div className="relative bg-white dark:bg-gray-900 rounded-xl shadow-lg max-w-2xl w-full mx-4 max-h-[90vh] overflow-hidden flex flex-col">
|
|
|
|
|
{/* Header - 统一标题栏样式 */}
|
|
|
|
|
<div className="flex items-center justify-between p-6 border-b border-gray-200 dark:border-gray-800">
|
|
|
|
|
<h2 className="text-xl font-semibold text-gray-900 dark:text-gray-100">
|
|
|
|
|
编辑通用配置片段
|
|
|
|
|
</h2>
|
2025-09-16 22:59:00 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={closeModal}
|
2025-09-17 09:47:44 +08:00
|
|
|
className="p-1 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-md transition-colors"
|
|
|
|
|
aria-label="关闭"
|
2025-09-16 22:59:00 +08:00
|
|
|
>
|
2025-09-17 09:47:44 +08:00
|
|
|
<X size={18} />
|
2025-09-16 22:59:00 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-09-17 09:47:44 +08:00
|
|
|
|
|
|
|
|
{/* Content - 统一内容区域样式 */}
|
|
|
|
|
<div className="flex-1 overflow-auto p-6 space-y-4">
|
|
|
|
|
<p className="text-sm text-gray-500 dark:text-gray-400">
|
|
|
|
|
该片段会在勾选"写入通用配置"时合并到 settings.json 中
|
|
|
|
|
</p>
|
2025-09-16 22:59:00 +08:00
|
|
|
<JsonEditor
|
|
|
|
|
value={commonConfigSnippet}
|
|
|
|
|
onChange={onCommonConfigSnippetChange}
|
|
|
|
|
darkMode={isDarkMode}
|
|
|
|
|
rows={12}
|
|
|
|
|
/>
|
|
|
|
|
{commonConfigError && (
|
2025-09-17 09:47:44 +08:00
|
|
|
<p className="text-sm text-red-500 dark:text-red-400">
|
2025-09-16 22:59:00 +08:00
|
|
|
{commonConfigError}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-09-17 09:47:44 +08:00
|
|
|
|
|
|
|
|
{/* Footer - 统一底部按钮样式 */}
|
|
|
|
|
<div className="flex items-center justify-end gap-3 p-6 border-t border-gray-200 dark:border-gray-800 bg-gray-100 dark:bg-gray-800">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={closeModal}
|
|
|
|
|
className="px-4 py-2 text-sm font-medium text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 hover:bg-white dark:hover:bg-gray-700 rounded-lg transition-colors"
|
|
|
|
|
>
|
|
|
|
|
取消
|
|
|
|
|
</button>
|
2025-09-16 22:59:00 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={closeModal}
|
2025-09-17 09:47:44 +08:00
|
|
|
className="px-4 py-2 bg-blue-500 dark:bg-blue-600 text-white rounded-lg hover:bg-blue-600 dark:hover:bg-blue-700 transition-colors text-sm font-medium flex items-center gap-2"
|
2025-09-16 22:59:00 +08:00
|
|
|
>
|
2025-09-17 09:47:44 +08:00
|
|
|
<Save className="w-4 h-4" />
|
|
|
|
|
保存
|
2025-09-16 22:59:00 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-09-06 23:13:01 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ClaudeConfigEditor;
|