refactor(codex): extract template to config with i18n support
**Changes:** - Create src/config/codexTemplates.ts with getCodexCustomTemplate factory - Support both Chinese and English templates based on i18n.language - Remove 70 lines of duplicated template strings from ProviderForm.tsx - Update both useEffect and handlePresetChange to use template factory - Clean up unused "Custom (Blank Template)" preset entry **Benefits:** - ✅ Eliminates code duplication (35-line template repeated twice) - ✅ Adds internationalization support for English users - ✅ Follows project architecture (templates in config/ directory) - ✅ Improves maintainability (single source of truth) - ✅ Net reduction: 34 lines (81 additions, 115 deletions) **Technical Details:** - Template selection logic: (i18n.language || "zh").startsWith("zh") ? "zh" : "en" - Templates are identical except for comments language - Both auth and config are returned as a single CodexTemplate object Addresses DRY principle violation and architectural concerns identified in code review.
This commit is contained in:
@@ -21,6 +21,7 @@ import {
|
||||
} from "@/config/geminiProviderPresets";
|
||||
import { applyTemplateValues } from "@/utils/providerConfigUtils";
|
||||
import { mergeProviderMeta } from "@/utils/providerMetaUtils";
|
||||
import { getCodexCustomTemplate } from "@/config/codexTemplates";
|
||||
import CodexConfigEditor from "./CodexConfigEditor";
|
||||
import { CommonConfigEditor } from "./CommonConfigEditor";
|
||||
import GeminiConfigEditor from "./GeminiConfigEditor";
|
||||
@@ -89,7 +90,7 @@ export function ProviderForm({
|
||||
initialData,
|
||||
showButtons = true,
|
||||
}: ProviderFormProps) {
|
||||
const { t } = useTranslation();
|
||||
const { t, i18n } = useTranslation();
|
||||
const isEditMode = Boolean(initialData);
|
||||
|
||||
const [selectedPresetId, setSelectedPresetId] = useState<string | null>(
|
||||
@@ -220,6 +221,15 @@ export function ProviderForm({
|
||||
[originalHandleCodexConfigChange, debouncedValidate],
|
||||
);
|
||||
|
||||
// Codex 新建模式:初始化时自动填充模板(支持国际化)
|
||||
useEffect(() => {
|
||||
if (appId === "codex" && !initialData && selectedPresetId === "custom") {
|
||||
const locale = (i18n.language || "zh").startsWith("zh") ? "zh" : "en";
|
||||
const template = getCodexCustomTemplate(locale);
|
||||
resetCodexConfig(template.auth, template.config);
|
||||
}
|
||||
}, [appId, initialData, selectedPresetId, resetCodexConfig, i18n.language]);
|
||||
|
||||
useEffect(() => {
|
||||
form.reset(defaultValues);
|
||||
}, [defaultValues, form]);
|
||||
@@ -525,9 +535,11 @@ export function ProviderForm({
|
||||
setActivePreset(null);
|
||||
form.reset(defaultValues);
|
||||
|
||||
// Codex 自定义模式:重置为空配置
|
||||
// Codex 自定义模式:加载模板(支持国际化)
|
||||
if (appId === "codex") {
|
||||
resetCodexConfig({}, "");
|
||||
const locale = (i18n.language || "zh").startsWith("zh") ? "zh" : "en";
|
||||
const template = getCodexCustomTemplate(locale);
|
||||
resetCodexConfig(template.auth, template.config);
|
||||
}
|
||||
// Gemini 自定义模式:重置为空配置
|
||||
if (appId === "gemini") {
|
||||
|
||||
Reference in New Issue
Block a user