fix(codex): improve config snippet handling with consistent trimming

- Trim config snippets during initialization from localStorage and defaults
- Trim snippets before comparison in toggle and change handlers
- Store trimmed values to localStorage for consistency
- Prevents configuration matching issues caused by accidental whitespace
This commit is contained in:
Jason
2025-09-20 23:00:53 +08:00
parent b8d2daccde
commit 8c826b3073

View File

@@ -135,19 +135,19 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
const [codexCommonConfigSnippet, setCodexCommonConfigSnippetState] = const [codexCommonConfigSnippet, setCodexCommonConfigSnippetState] =
useState<string>(() => { useState<string>(() => {
if (typeof window === "undefined") { if (typeof window === "undefined") {
return DEFAULT_CODEX_COMMON_CONFIG_SNIPPET; return DEFAULT_CODEX_COMMON_CONFIG_SNIPPET.trim();
} }
try { try {
const stored = window.localStorage.getItem( const stored = window.localStorage.getItem(
CODEX_COMMON_CONFIG_STORAGE_KEY, CODEX_COMMON_CONFIG_STORAGE_KEY,
); );
if (stored && stored.trim()) { if (stored && stored.trim()) {
return stored; return stored.trim();
} }
} catch { } catch {
// ignore localStorage 读取失败 // ignore localStorage 读取失败
} }
return DEFAULT_CODEX_COMMON_CONFIG_SNIPPET; return DEFAULT_CODEX_COMMON_CONFIG_SNIPPET.trim();
}); });
const [codexCommonConfigError, setCodexCommonConfigError] = useState(""); const [codexCommonConfigError, setCodexCommonConfigError] = useState("");
const isUpdatingFromCodexCommonConfig = useRef(false); const isUpdatingFromCodexCommonConfig = useRef(false);
@@ -689,10 +689,10 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
// Codex: 处理通用配置开关 // Codex: 处理通用配置开关
const handleCodexCommonConfigToggle = (checked: boolean) => { const handleCodexCommonConfigToggle = (checked: boolean) => {
const { updatedConfig, error: snippetError } = const snippet = codexCommonConfigSnippet.trim();
updateTomlCommonConfigSnippet( const { updatedConfig, error: snippetError } = updateTomlCommonConfigSnippet(
codexConfig, codexConfig,
codexCommonConfigSnippet, snippet,
checked, checked,
); );
@@ -715,10 +715,11 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
// Codex: 处理通用配置片段变化 // Codex: 处理通用配置片段变化
const handleCodexCommonConfigSnippetChange = (value: string) => { const handleCodexCommonConfigSnippetChange = (value: string) => {
const previousSnippet = codexCommonConfigSnippet; const previousSnippet = codexCommonConfigSnippet.trim();
const sanitizedValue = value.trim();
setCodexCommonConfigSnippet(value); setCodexCommonConfigSnippet(value);
if (!value.trim()) { if (!sanitizedValue) {
setCodexCommonConfigError(""); setCodexCommonConfigError("");
if (useCodexCommonConfig) { if (useCodexCommonConfig) {
const { updatedConfig } = updateTomlCommonConfigSnippet( const { updatedConfig } = updateTomlCommonConfigSnippet(
@@ -741,7 +742,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
); );
const addResult = updateTomlCommonConfigSnippet( const addResult = updateTomlCommonConfigSnippet(
removeResult.updatedConfig, removeResult.updatedConfig,
value, sanitizedValue,
true, true,
); );
@@ -762,7 +763,10 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
// 保存 Codex 通用配置到 localStorage // 保存 Codex 通用配置到 localStorage
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
try { try {
window.localStorage.setItem(CODEX_COMMON_CONFIG_STORAGE_KEY, value); window.localStorage.setItem(
CODEX_COMMON_CONFIG_STORAGE_KEY,
sanitizedValue,
);
} catch { } catch {
// ignore localStorage 写入失败 // ignore localStorage 写入失败
} }