2025-10-17 18:38:49 +08:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
import { CodexAuthSection, CodexConfigSection } from "./CodexConfigSections";
|
|
|
|
|
import { CodexQuickWizardModal } from "./CodexQuickWizardModal";
|
|
|
|
|
import { CodexCommonConfigModal } from "./CodexCommonConfigModal";
|
2025-10-16 17:40:25 +08:00
|
|
|
|
|
|
|
|
interface CodexConfigEditorProps {
|
|
|
|
|
authValue: string;
|
|
|
|
|
|
|
|
|
|
configValue: string;
|
|
|
|
|
|
|
|
|
|
onAuthChange: (value: string) => void;
|
|
|
|
|
|
|
|
|
|
onConfigChange: (value: string) => void;
|
|
|
|
|
|
|
|
|
|
onAuthBlur?: () => void;
|
|
|
|
|
|
|
|
|
|
useCommonConfig: boolean;
|
|
|
|
|
|
|
|
|
|
onCommonConfigToggle: (checked: boolean) => void;
|
|
|
|
|
|
|
|
|
|
commonConfigSnippet: string;
|
|
|
|
|
|
|
|
|
|
onCommonConfigSnippetChange: (value: string) => void;
|
|
|
|
|
|
|
|
|
|
commonConfigError: string;
|
|
|
|
|
|
|
|
|
|
authError: string;
|
|
|
|
|
|
2025-10-16 23:56:30 +08:00
|
|
|
configError: string; // config.toml 错误提示
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-16 23:56:30 +08:00
|
|
|
onWebsiteUrlChange?: (url: string) => void; // 更新网址回调
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-16 23:56:30 +08:00
|
|
|
isTemplateModalOpen?: boolean; // 模态框状态
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-16 23:56:30 +08:00
|
|
|
setIsTemplateModalOpen?: (open: boolean) => void; // 设置模态框状态
|
|
|
|
|
|
|
|
|
|
onNameChange?: (name: string) => void; // 更新供应商名称回调
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CodexConfigEditor: React.FC<CodexConfigEditorProps> = ({
|
|
|
|
|
authValue,
|
|
|
|
|
configValue,
|
|
|
|
|
onAuthChange,
|
|
|
|
|
onConfigChange,
|
|
|
|
|
onAuthBlur,
|
|
|
|
|
useCommonConfig,
|
|
|
|
|
onCommonConfigToggle,
|
|
|
|
|
commonConfigSnippet,
|
|
|
|
|
onCommonConfigSnippetChange,
|
|
|
|
|
commonConfigError,
|
|
|
|
|
authError,
|
2025-10-16 23:56:30 +08:00
|
|
|
configError,
|
2025-10-16 17:40:25 +08:00
|
|
|
onWebsiteUrlChange,
|
|
|
|
|
onNameChange,
|
|
|
|
|
isTemplateModalOpen: externalTemplateModalOpen,
|
|
|
|
|
setIsTemplateModalOpen: externalSetTemplateModalOpen,
|
|
|
|
|
}) => {
|
|
|
|
|
const [isCommonConfigModalOpen, setIsCommonConfigModalOpen] = useState(false);
|
|
|
|
|
|
2025-10-17 18:38:49 +08:00
|
|
|
// Use internal state or external state
|
2025-10-18 16:52:02 +08:00
|
|
|
const [internalTemplateModalOpen, setInternalTemplateModalOpen] =
|
|
|
|
|
useState(false);
|
|
|
|
|
const isTemplateModalOpen =
|
|
|
|
|
externalTemplateModalOpen ?? internalTemplateModalOpen;
|
|
|
|
|
const setIsTemplateModalOpen =
|
|
|
|
|
externalSetTemplateModalOpen ?? setInternalTemplateModalOpen;
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-17 18:38:49 +08:00
|
|
|
// Auto-open common config modal if there's an error
|
2025-10-16 17:40:25 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (commonConfigError && !isCommonConfigModalOpen) {
|
|
|
|
|
setIsCommonConfigModalOpen(true);
|
|
|
|
|
}
|
|
|
|
|
}, [commonConfigError, isCommonConfigModalOpen]);
|
|
|
|
|
|
2025-10-17 18:38:49 +08:00
|
|
|
const handleQuickWizardApply = (
|
|
|
|
|
auth: string,
|
|
|
|
|
config: string,
|
2025-10-18 16:52:02 +08:00
|
|
|
extras: { websiteUrl?: string; displayName?: string },
|
2025-10-17 18:38:49 +08:00
|
|
|
) => {
|
|
|
|
|
onAuthChange(auth);
|
2025-10-16 17:40:25 +08:00
|
|
|
onConfigChange(config);
|
|
|
|
|
|
2025-10-17 18:38:49 +08:00
|
|
|
if (onWebsiteUrlChange && extras.websiteUrl) {
|
|
|
|
|
onWebsiteUrlChange(extras.websiteUrl);
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 18:38:49 +08:00
|
|
|
if (onNameChange && extras.displayName) {
|
|
|
|
|
onNameChange(extras.displayName);
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-6">
|
2025-10-17 18:38:49 +08:00
|
|
|
{/* Auth JSON Section */}
|
|
|
|
|
<CodexAuthSection
|
|
|
|
|
value={authValue}
|
|
|
|
|
onChange={onAuthChange}
|
|
|
|
|
onBlur={onAuthBlur}
|
|
|
|
|
error={authError}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Config TOML Section */}
|
|
|
|
|
<CodexConfigSection
|
|
|
|
|
value={configValue}
|
|
|
|
|
onChange={onConfigChange}
|
|
|
|
|
useCommonConfig={useCommonConfig}
|
|
|
|
|
onCommonConfigToggle={onCommonConfigToggle}
|
|
|
|
|
onEditCommonConfig={() => setIsCommonConfigModalOpen(true)}
|
|
|
|
|
commonConfigError={commonConfigError}
|
|
|
|
|
configError={configError}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Quick Wizard Modal */}
|
|
|
|
|
<CodexQuickWizardModal
|
|
|
|
|
isOpen={isTemplateModalOpen}
|
|
|
|
|
onClose={() => setIsTemplateModalOpen(false)}
|
|
|
|
|
onApply={handleQuickWizardApply}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Common Config Modal */}
|
|
|
|
|
<CodexCommonConfigModal
|
|
|
|
|
isOpen={isCommonConfigModalOpen}
|
|
|
|
|
onClose={() => setIsCommonConfigModalOpen(false)}
|
|
|
|
|
value={commonConfigSnippet}
|
|
|
|
|
onChange={onCommonConfigSnippetChange}
|
|
|
|
|
error={commonConfigError}
|
|
|
|
|
/>
|
2025-10-16 17:40:25 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CodexConfigEditor;
|