2025-10-16 20:32:11 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-11-21 09:30:30 +08:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import { FullScreenPanel } from "@/components/common/FullScreenPanel";
|
2025-10-16 20:32:11 +08:00
|
|
|
import { Label } from "@/components/ui/label";
|
2025-11-01 18:59:19 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
2025-11-21 09:30:30 +08:00
|
|
|
import { Save } from "lucide-react";
|
|
|
|
|
import JsonEditor from "@/components/JsonEditor";
|
2025-10-16 20:32:11 +08:00
|
|
|
|
|
|
|
|
interface CommonConfigEditorProps {
|
|
|
|
|
value: string;
|
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
|
useCommonConfig: boolean;
|
|
|
|
|
onCommonConfigToggle: (checked: boolean) => void;
|
|
|
|
|
commonConfigSnippet: string;
|
|
|
|
|
onCommonConfigSnippetChange: (value: string) => void;
|
|
|
|
|
commonConfigError: string;
|
|
|
|
|
onEditClick: () => void;
|
|
|
|
|
isModalOpen: boolean;
|
|
|
|
|
onModalClose: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CommonConfigEditor({
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
useCommonConfig,
|
|
|
|
|
onCommonConfigToggle,
|
|
|
|
|
commonConfigSnippet,
|
|
|
|
|
onCommonConfigSnippetChange,
|
|
|
|
|
commonConfigError,
|
|
|
|
|
onEditClick,
|
|
|
|
|
isModalOpen,
|
|
|
|
|
onModalClose,
|
|
|
|
|
}: CommonConfigEditorProps) {
|
|
|
|
|
const { t } = useTranslation();
|
2025-11-21 09:30:30 +08:00
|
|
|
const [isDarkMode, setIsDarkMode] = useState(false);
|
2025-10-16 20:32:11 +08:00
|
|
|
|
2025-11-21 09:30:30 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
setIsDarkMode(document.documentElement.classList.contains("dark"));
|
2025-11-01 21:05:01 +08:00
|
|
|
|
2025-11-21 09:30:30 +08:00
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
|
setIsDarkMode(document.documentElement.classList.contains("dark"));
|
|
|
|
|
});
|
2025-11-01 21:05:01 +08:00
|
|
|
|
2025-11-21 09:30:30 +08:00
|
|
|
observer.observe(document.documentElement, {
|
|
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: ["class"],
|
|
|
|
|
});
|
2025-11-01 21:05:01 +08:00
|
|
|
|
2025-11-21 09:30:30 +08:00
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, []);
|
2025-11-01 21:05:01 +08:00
|
|
|
|
2025-10-16 20:32:11 +08:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<div className="flex items-center justify-between">
|
2025-10-24 13:02:35 +08:00
|
|
|
<Label htmlFor="settingsConfig">{t("provider.configJson")}</Label>
|
2025-10-16 20:32:11 +08:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<label className="inline-flex items-center gap-2 text-sm text-muted-foreground cursor-pointer">
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
id="useCommonConfig"
|
|
|
|
|
checked={useCommonConfig}
|
|
|
|
|
onChange={(e) => onCommonConfigToggle(e.target.checked)}
|
2025-10-21 10:49:30 +08:00
|
|
|
className="w-4 h-4 text-blue-500 bg-white dark:bg-gray-800 border-border-default rounded focus:ring-blue-500 dark:focus:ring-blue-400 focus:ring-2"
|
2025-10-16 20:32:11 +08:00
|
|
|
/>
|
|
|
|
|
<span>
|
|
|
|
|
{t("claudeConfig.writeCommonConfig", {
|
|
|
|
|
defaultValue: "写入通用配置",
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onEditClick}
|
|
|
|
|
className="text-xs text-blue-400 dark:text-blue-500 hover:text-blue-500 dark:hover:text-blue-400 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
{t("claudeConfig.editCommonConfig", {
|
|
|
|
|
defaultValue: "编辑通用配置",
|
|
|
|
|
})}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{commonConfigError && !isModalOpen && (
|
|
|
|
|
<p className="text-xs text-red-500 dark:text-red-400 text-right">
|
|
|
|
|
{commonConfigError}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-11-21 09:30:30 +08:00
|
|
|
<JsonEditor
|
2025-10-21 10:49:30 +08:00
|
|
|
value={value}
|
2025-11-21 09:30:30 +08:00
|
|
|
onChange={onChange}
|
2025-10-21 10:49:30 +08:00
|
|
|
placeholder={`{
|
2025-10-16 20:32:11 +08:00
|
|
|
"env": {
|
|
|
|
|
"ANTHROPIC_BASE_URL": "https://your-api-endpoint.com",
|
|
|
|
|
"ANTHROPIC_AUTH_TOKEN": "your-api-key-here"
|
|
|
|
|
}
|
|
|
|
|
}`}
|
2025-11-21 09:30:30 +08:00
|
|
|
darkMode={isDarkMode}
|
2025-10-21 10:49:30 +08:00
|
|
|
rows={14}
|
2025-11-21 09:30:30 +08:00
|
|
|
showValidation={true}
|
|
|
|
|
language="json"
|
2025-10-21 10:49:30 +08:00
|
|
|
/>
|
2025-10-16 20:32:11 +08:00
|
|
|
</div>
|
|
|
|
|
|
2025-11-21 09:30:30 +08:00
|
|
|
<FullScreenPanel
|
|
|
|
|
isOpen={isModalOpen}
|
|
|
|
|
title={t("claudeConfig.editCommonConfigTitle", {
|
|
|
|
|
defaultValue: "编辑通用配置片段",
|
|
|
|
|
})}
|
|
|
|
|
onClose={onModalClose}
|
|
|
|
|
footer={
|
|
|
|
|
<>
|
2025-11-01 18:59:19 +08:00
|
|
|
<Button type="button" variant="outline" onClick={onModalClose}>
|
|
|
|
|
{t("common.cancel")}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="button" onClick={onModalClose} className="gap-2">
|
|
|
|
|
<Save className="w-4 h-4" />
|
|
|
|
|
{t("common.save")}
|
|
|
|
|
</Button>
|
2025-11-21 09:30:30 +08:00
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
{t("claudeConfig.commonConfigHint", {
|
|
|
|
|
defaultValue: "通用配置片段将合并到所有启用它的供应商配置中",
|
|
|
|
|
})}
|
|
|
|
|
</p>
|
|
|
|
|
<JsonEditor
|
|
|
|
|
value={commonConfigSnippet}
|
|
|
|
|
onChange={onCommonConfigSnippetChange}
|
|
|
|
|
placeholder={`{
|
|
|
|
|
"env": {
|
|
|
|
|
"ANTHROPIC_BASE_URL": "https://your-api-endpoint.com"
|
|
|
|
|
}
|
|
|
|
|
}`}
|
|
|
|
|
darkMode={isDarkMode}
|
|
|
|
|
rows={16}
|
|
|
|
|
showValidation={true}
|
|
|
|
|
language="json"
|
|
|
|
|
/>
|
|
|
|
|
{commonConfigError && (
|
|
|
|
|
<p className="text-sm text-red-500 dark:text-red-400">
|
|
|
|
|
{commonConfigError}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</FullScreenPanel>
|
2025-10-16 20:32:11 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|