mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-02 02:05:57 +08:00
refactor: remove OMO common config two-layer merge system
Each OMO provider now stores its complete configuration directly in settings_config.otherFields instead of relying on a shared OmoGlobalConfig merged at write time. This simplifies the data flow from a 4-tuple (agents, categories, otherFields, useCommonConfig) to a 3-tuple and eliminates an entire DB table, two Tauri commands, and ~1700 lines of merge/sync code across frontend and backend. Backend: - Delete database/dao/omo.rs (OmoGlobalConfig struct + get/save methods) - Remove get/set_config_snippet from settings DAO - Remove get/set_common_config_snippet Tauri commands - Replace merge_config() with build_config() in services/omo.rs - Simplify OmoVariant (remove config_key, known_keys) - Simplify import_from_local and build_local_file_data - Rewrite all OMO service tests Frontend: - Delete OmoCommonConfigEditor.tsx and OmoGlobalConfigFields.tsx - Delete src/lib/api/config.ts - Remove OmoGlobalConfig type and merge preview functions - Remove useGlobalConfig/useSaveGlobalConfig query hooks - Simplify useOmoDraftState (remove all common config state) - Replace OmoCommonConfigEditor with read-only JsonEditor preview - Clean i18n keys (zh/en/ja)
This commit is contained in:
@@ -1,22 +1,11 @@
|
||||
import { useState, useCallback, useEffect, useRef, useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import type { OmoGlobalConfig } from "@/types/omo";
|
||||
import { useState, useCallback, useMemo } from "react";
|
||||
import {
|
||||
mergeOmoConfigPreview,
|
||||
mergeOmoSlimConfigPreview,
|
||||
buildOmoSlimProfilePreview,
|
||||
} from "@/types/omo";
|
||||
import { type OmoGlobalConfigFieldsRef } from "../OmoGlobalConfigFields";
|
||||
import * as configApi from "@/lib/api/config";
|
||||
import {
|
||||
EMPTY_OMO_GLOBAL_CONFIG,
|
||||
buildOmoProfilePreview,
|
||||
} from "../helpers/opencodeFormUtils";
|
||||
} from "@/types/omo";
|
||||
|
||||
interface UseOmoDraftStateParams {
|
||||
initialOmoSettings: Record<string, unknown> | undefined;
|
||||
queriedOmoGlobalConfig: OmoGlobalConfig | undefined;
|
||||
isEditMode: boolean;
|
||||
appId: string;
|
||||
category?: string;
|
||||
@@ -33,33 +22,15 @@ export interface OmoDraftState {
|
||||
>;
|
||||
omoOtherFieldsStr: string;
|
||||
setOmoOtherFieldsStr: React.Dispatch<React.SetStateAction<string>>;
|
||||
useOmoCommonConfig: boolean;
|
||||
setUseOmoCommonConfig: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
isOmoConfigModalOpen: boolean;
|
||||
setIsOmoConfigModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
isOmoSaving: boolean;
|
||||
omoGlobalConfigRef: React.RefObject<OmoGlobalConfigFieldsRef | null>;
|
||||
omoFieldsKey: number;
|
||||
effectiveOmoGlobalConfig: OmoGlobalConfig;
|
||||
mergedOmoJsonPreview: string;
|
||||
handleOmoGlobalConfigSave: () => Promise<void>;
|
||||
handleOmoEditClick: () => void;
|
||||
resetOmoDraftState: (useCommonConfig?: boolean) => void;
|
||||
setOmoGlobalState: React.Dispatch<
|
||||
React.SetStateAction<OmoGlobalConfig | null>
|
||||
>;
|
||||
resetOmoDraftState: () => void;
|
||||
}
|
||||
|
||||
export function useOmoDraftState({
|
||||
initialOmoSettings,
|
||||
queriedOmoGlobalConfig,
|
||||
isEditMode,
|
||||
appId,
|
||||
category,
|
||||
}: UseOmoDraftStateParams): OmoDraftState {
|
||||
const { t } = useTranslation();
|
||||
const isSlim = category === "omo-slim";
|
||||
const commonConfigKey = isSlim ? "omo_slim" : "omo";
|
||||
|
||||
const [omoAgents, setOmoAgents] = useState<
|
||||
Record<string, Record<string, unknown>>
|
||||
@@ -82,118 +53,25 @@ export function useOmoDraftState({
|
||||
return otherFields ? JSON.stringify(otherFields, null, 2) : "";
|
||||
});
|
||||
|
||||
const [omoGlobalState, setOmoGlobalState] = useState<OmoGlobalConfig | null>(
|
||||
null,
|
||||
);
|
||||
|
||||
const [isOmoConfigModalOpen, setIsOmoConfigModalOpen] = useState(false);
|
||||
const [useOmoCommonConfig, setUseOmoCommonConfig] = useState(() => {
|
||||
const raw = initialOmoSettings?.useCommonConfig;
|
||||
return typeof raw === "boolean" ? raw : true;
|
||||
});
|
||||
const [isOmoSaving, setIsOmoSaving] = useState(false);
|
||||
const omoGlobalConfigRef = useRef<OmoGlobalConfigFieldsRef>(null);
|
||||
const [omoFieldsKey, setOmoFieldsKey] = useState(0);
|
||||
const effectiveOmoGlobalConfig =
|
||||
omoGlobalState ?? queriedOmoGlobalConfig ?? EMPTY_OMO_GLOBAL_CONFIG;
|
||||
|
||||
const mergedOmoJsonPreview = useMemo(() => {
|
||||
if (useOmoCommonConfig) {
|
||||
if (isSlim) {
|
||||
const merged = mergeOmoSlimConfigPreview(
|
||||
effectiveOmoGlobalConfig,
|
||||
omoAgents,
|
||||
omoOtherFieldsStr,
|
||||
);
|
||||
return JSON.stringify(merged, null, 2);
|
||||
}
|
||||
const merged = mergeOmoConfigPreview(
|
||||
effectiveOmoGlobalConfig,
|
||||
omoAgents,
|
||||
omoCategories,
|
||||
omoOtherFieldsStr,
|
||||
);
|
||||
return JSON.stringify(merged, null, 2);
|
||||
} else {
|
||||
if (isSlim) {
|
||||
return JSON.stringify(
|
||||
buildOmoSlimProfilePreview(omoAgents, omoOtherFieldsStr),
|
||||
null,
|
||||
2,
|
||||
);
|
||||
}
|
||||
if (isSlim) {
|
||||
return JSON.stringify(
|
||||
buildOmoProfilePreview(omoAgents, omoCategories, omoOtherFieldsStr),
|
||||
buildOmoSlimProfilePreview(omoAgents, omoOtherFieldsStr),
|
||||
null,
|
||||
2,
|
||||
);
|
||||
}
|
||||
}, [
|
||||
useOmoCommonConfig,
|
||||
effectiveOmoGlobalConfig,
|
||||
omoAgents,
|
||||
omoCategories,
|
||||
omoOtherFieldsStr,
|
||||
isSlim,
|
||||
]);
|
||||
return JSON.stringify(
|
||||
buildOmoProfilePreview(omoAgents, omoCategories, omoOtherFieldsStr),
|
||||
null,
|
||||
2,
|
||||
);
|
||||
}, [omoAgents, omoCategories, omoOtherFieldsStr, isSlim]);
|
||||
|
||||
// Auto-detect whether common config has content for new OMO/OMO Slim profiles
|
||||
useEffect(() => {
|
||||
if (
|
||||
appId !== "opencode" ||
|
||||
(category !== "omo" && category !== "omo-slim") ||
|
||||
isEditMode
|
||||
)
|
||||
return;
|
||||
let active = true;
|
||||
(async () => {
|
||||
let next = false;
|
||||
try {
|
||||
const raw = await configApi.getCommonConfigSnippet(commonConfigKey);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
||||
next = Object.keys(parsed).some(
|
||||
(k) => k !== "id" && k !== "updatedAt",
|
||||
);
|
||||
}
|
||||
} catch {}
|
||||
if (active) setUseOmoCommonConfig(next);
|
||||
})();
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [appId, category, isEditMode, commonConfigKey]);
|
||||
|
||||
const handleOmoGlobalConfigSave = useCallback(async () => {
|
||||
if (!omoGlobalConfigRef.current) return;
|
||||
setIsOmoSaving(true);
|
||||
try {
|
||||
const config = omoGlobalConfigRef.current.buildCurrentConfigStrict();
|
||||
await configApi.setCommonConfigSnippet(
|
||||
commonConfigKey,
|
||||
JSON.stringify(config),
|
||||
);
|
||||
setIsOmoConfigModalOpen(false);
|
||||
toast.success(
|
||||
t("omo.globalConfigSaved", { defaultValue: "Global config saved" }),
|
||||
);
|
||||
} catch (err) {
|
||||
toast.error(String(err));
|
||||
} finally {
|
||||
setIsOmoSaving(false);
|
||||
}
|
||||
}, [t, commonConfigKey]);
|
||||
|
||||
const handleOmoEditClick = useCallback(() => {
|
||||
setOmoFieldsKey((k) => k + 1);
|
||||
setIsOmoConfigModalOpen(true);
|
||||
}, []);
|
||||
|
||||
const resetOmoDraftState = useCallback((useCommonConfig = true) => {
|
||||
const resetOmoDraftState = useCallback(() => {
|
||||
setOmoAgents({});
|
||||
setOmoCategories({});
|
||||
setOmoOtherFieldsStr("");
|
||||
setUseOmoCommonConfig(useCommonConfig);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
@@ -203,18 +81,7 @@ export function useOmoDraftState({
|
||||
setOmoCategories,
|
||||
omoOtherFieldsStr,
|
||||
setOmoOtherFieldsStr,
|
||||
useOmoCommonConfig,
|
||||
setUseOmoCommonConfig,
|
||||
isOmoConfigModalOpen,
|
||||
setIsOmoConfigModalOpen,
|
||||
isOmoSaving,
|
||||
omoGlobalConfigRef,
|
||||
omoFieldsKey,
|
||||
effectiveOmoGlobalConfig,
|
||||
mergedOmoJsonPreview,
|
||||
handleOmoGlobalConfigSave,
|
||||
handleOmoEditClick,
|
||||
resetOmoDraftState,
|
||||
setOmoGlobalState,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user