mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-26 23:56:02 +08:00
8a05e7bd3d
* feat(gemini): add Gemini provider integration - Add gemini_config.rs module for .env file parsing - Extend AppType enum to support Gemini - Implement GeminiConfigEditor and GeminiFormFields components - Add GeminiIcon with standardized 1024x1024 viewBox - Add Gemini provider presets configuration - Update i18n translations for Gemini support - Extend ProviderService and McpService for Gemini * fix(gemini): resolve TypeScript errors, add i18n support, and fix MCP logic **Critical Fixes:** - Fix TS2741 errors in tests/msw/state.ts by adding missing Gemini type definitions - Fix ProviderCard.extractApiUrl to support GOOGLE_GEMINI_BASE_URL display - Add missing apps.gemini i18n keys (zh/en) for proper app name display - Fix MCP service Gemini cross-app duplication logic to prevent self-copy **Technical Details:** - tests/msw/state.ts: Add gemini default providers, current ID, and MCP config - ProviderCard.tsx: Check both ANTHROPIC_BASE_URL and GOOGLE_GEMINI_BASE_URL - services/mcp.rs: Skip Gemini in sync_other_side logic with unreachable!() guards - Run pnpm format to auto-fix code style issues **Verification:** - ✅ pnpm typecheck passes - ✅ pnpm format completed * feat(gemini): enhance authentication and config parsing - Add strict and lenient .env parsing modes - Implement PackyCode partner authentication detection - Support Google OAuth official authentication - Auto-configure security.auth.selectedType for PackyCode - Add comprehensive test coverage for all auth types - Update i18n for OAuth hints and Gemini config --------- Co-authored-by: Jason <farion1231@gmail.com>
78 lines
1.9 KiB
TypeScript
78 lines
1.9 KiB
TypeScript
import { useState, useCallback } from "react";
|
|
import type { ProviderCategory } from "@/types";
|
|
import {
|
|
getApiKeyFromConfig,
|
|
setApiKeyInConfig,
|
|
hasApiKeyField,
|
|
} from "@/utils/providerConfigUtils";
|
|
|
|
interface UseApiKeyStateProps {
|
|
initialConfig?: string;
|
|
onConfigChange: (config: string) => void;
|
|
selectedPresetId: string | null;
|
|
category?: ProviderCategory;
|
|
appType?: string;
|
|
}
|
|
|
|
/**
|
|
* 管理 API Key 输入状态
|
|
* 自动同步 API Key 和 JSON 配置
|
|
*/
|
|
export function useApiKeyState({
|
|
initialConfig,
|
|
onConfigChange,
|
|
selectedPresetId,
|
|
category,
|
|
appType,
|
|
}: UseApiKeyStateProps) {
|
|
const [apiKey, setApiKey] = useState(() => {
|
|
if (initialConfig) {
|
|
return getApiKeyFromConfig(initialConfig, appType);
|
|
}
|
|
return "";
|
|
});
|
|
|
|
const handleApiKeyChange = useCallback(
|
|
(key: string) => {
|
|
setApiKey(key);
|
|
|
|
const configString = setApiKeyInConfig(
|
|
initialConfig || "{}",
|
|
key.trim(),
|
|
{
|
|
// 最佳实践:仅在“新增模式”且“非官方类别”时补齐缺失字段
|
|
// - 新增模式:selectedPresetId !== null
|
|
// - 非官方类别:category !== undefined && category !== "official"
|
|
// - 官方类别:不创建字段(UI 也会禁用输入框)
|
|
// - 未传入 category:不创建字段(避免意外行为)
|
|
createIfMissing:
|
|
selectedPresetId !== null &&
|
|
category !== undefined &&
|
|
category !== "official",
|
|
appType,
|
|
},
|
|
);
|
|
|
|
onConfigChange(configString);
|
|
},
|
|
[initialConfig, selectedPresetId, category, appType, onConfigChange],
|
|
);
|
|
|
|
const showApiKey = useCallback(
|
|
(config: string, isEditMode: boolean) => {
|
|
return (
|
|
selectedPresetId !== null ||
|
|
(isEditMode && hasApiKeyField(config, appType))
|
|
);
|
|
},
|
|
[selectedPresetId, appType],
|
|
);
|
|
|
|
return {
|
|
apiKey,
|
|
setApiKey,
|
|
handleApiKeyChange,
|
|
showApiKey,
|
|
};
|
|
}
|