refactor: unify directory structure and extract shared components
- Migrate all form components from ProviderForm/ to providers/forms/ - Create shared components to eliminate code duplication: * ApiKeySection: unified API key input with "Get API Key" link * EndpointField: unified endpoint URL input with manage button - Refactor ClaudeFormFields (-31% lines) and CodexFormFields (-33% lines) - Update all import paths to use new locations - Reduce code duplication from ~12% to ~7% This change improves maintainability and makes the codebase more DRY.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import JsonEditor from "../JsonEditor";
|
import JsonEditor from "@/components/JsonEditor";
|
||||||
import { X, Save } from "lucide-react";
|
import { X, Save } from "lucide-react";
|
||||||
import { isLinux } from "../../lib/platform";
|
import { isLinux } from "@/lib/platform";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
interface ClaudeConfigEditorProps {
|
interface ClaudeConfigEditorProps {
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { FormLabel } from "@/components/ui/form";
|
import { FormLabel } from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import ApiKeyInput from "@/components/ProviderForm/ApiKeyInput";
|
import EndpointSpeedTest from "./EndpointSpeedTest";
|
||||||
import EndpointSpeedTest from "@/components/ProviderForm/EndpointSpeedTest";
|
import KimiModelSelector from "./KimiModelSelector";
|
||||||
import KimiModelSelector from "@/components/ProviderForm/KimiModelSelector";
|
import { ApiKeySection, EndpointField } from "./shared";
|
||||||
import { Zap } from "lucide-react";
|
|
||||||
import type { ProviderCategory } from "@/types";
|
import type { ProviderCategory } from "@/types";
|
||||||
import type { TemplateValueConfig } from "@/config/providerPresets";
|
import type { TemplateValueConfig } from "@/config/providerPresets";
|
||||||
|
|
||||||
@@ -90,38 +89,13 @@ export function ClaudeFormFields({
|
|||||||
<>
|
<>
|
||||||
{/* API Key 输入框 */}
|
{/* API Key 输入框 */}
|
||||||
{shouldShowApiKey && (
|
{shouldShowApiKey && (
|
||||||
<div className="space-y-1">
|
<ApiKeySection
|
||||||
<ApiKeyInput
|
|
||||||
value={apiKey}
|
value={apiKey}
|
||||||
onChange={onApiKeyChange}
|
onChange={onApiKeyChange}
|
||||||
required={category !== "official"}
|
category={category}
|
||||||
placeholder={
|
shouldShowLink={shouldShowApiKeyLink}
|
||||||
category === "official"
|
websiteUrl={websiteUrl}
|
||||||
? t("providerForm.officialNoApiKey", {
|
|
||||||
defaultValue: "官方供应商无需 API Key",
|
|
||||||
})
|
|
||||||
: t("providerForm.apiKeyAutoFill", {
|
|
||||||
defaultValue: "输入 API Key,将自动填充到配置",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
disabled={category === "official"}
|
|
||||||
/>
|
/>
|
||||||
{/* API Key 获取链接 */}
|
|
||||||
{shouldShowApiKeyLink && websiteUrl && (
|
|
||||||
<div className="-mt-1 pl-1">
|
|
||||||
<a
|
|
||||||
href={websiteUrl}
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
className="text-xs text-blue-400 dark:text-blue-500 hover:text-blue-500 dark:hover:text-blue-400 transition-colors"
|
|
||||||
>
|
|
||||||
{t("providerForm.getApiKey", {
|
|
||||||
defaultValue: "获取 API Key",
|
|
||||||
})}
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* 模板变量输入 */}
|
{/* 模板变量输入 */}
|
||||||
@@ -161,40 +135,19 @@ export function ClaudeFormFields({
|
|||||||
|
|
||||||
{/* Base URL 输入框 */}
|
{/* Base URL 输入框 */}
|
||||||
{shouldShowSpeedTest && (
|
{shouldShowSpeedTest && (
|
||||||
<div className="space-y-2">
|
<EndpointField
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<FormLabel htmlFor="baseUrl">
|
|
||||||
{t("providerForm.apiEndpoint", { defaultValue: "API 端点" })}
|
|
||||||
</FormLabel>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => onEndpointModalToggle(true)}
|
|
||||||
className="flex items-center gap-1 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
|
|
||||||
>
|
|
||||||
<Zap className="h-3.5 w-3.5" />
|
|
||||||
{t("providerForm.manageAndTest", {
|
|
||||||
defaultValue: "管理和测速",
|
|
||||||
})}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<Input
|
|
||||||
id="baseUrl"
|
id="baseUrl"
|
||||||
type="url"
|
label={t("providerForm.apiEndpoint", { defaultValue: "API 端点" })}
|
||||||
value={baseUrl}
|
value={baseUrl}
|
||||||
onChange={(e) => onBaseUrlChange(e.target.value)}
|
onChange={onBaseUrlChange}
|
||||||
placeholder={t("providerForm.apiEndpointPlaceholder", {
|
placeholder={t("providerForm.apiEndpointPlaceholder", {
|
||||||
defaultValue: "https://api.example.com",
|
defaultValue: "https://api.example.com",
|
||||||
})}
|
})}
|
||||||
autoComplete="off"
|
hint={t("providerForm.apiHint", {
|
||||||
/>
|
|
||||||
<div className="p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg">
|
|
||||||
<p className="text-xs text-amber-600 dark:text-amber-400">
|
|
||||||
{t("providerForm.apiHint", {
|
|
||||||
defaultValue: "API 端点地址用于连接服务器",
|
defaultValue: "API 端点地址用于连接服务器",
|
||||||
})}
|
})}
|
||||||
</p>
|
onManageClick={() => onEndpointModalToggle(true)}
|
||||||
</div>
|
/>
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* 端点测速弹窗 */}
|
{/* 端点测速弹窗 */}
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ import React, { useState, useEffect, useRef } from "react";
|
|||||||
|
|
||||||
import { X, Save } from "lucide-react";
|
import { X, Save } from "lucide-react";
|
||||||
|
|
||||||
import { isLinux } from "../../lib/platform";
|
import { isLinux } from "@/lib/platform";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
generateThirdPartyAuth,
|
generateThirdPartyAuth,
|
||||||
generateThirdPartyConfig,
|
generateThirdPartyConfig,
|
||||||
} from "../../config/codexProviderPresets";
|
} from "@/config/codexProviderPresets";
|
||||||
|
|
||||||
interface CodexConfigEditorProps {
|
interface CodexConfigEditorProps {
|
||||||
authValue: string;
|
authValue: string;
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { FormLabel } from "@/components/ui/form";
|
import EndpointSpeedTest from "./EndpointSpeedTest";
|
||||||
import { Input } from "@/components/ui/input";
|
import { ApiKeySection, EndpointField } from "./shared";
|
||||||
import ApiKeyInput from "@/components/ProviderForm/ApiKeyInput";
|
|
||||||
import EndpointSpeedTest from "@/components/ProviderForm/EndpointSpeedTest";
|
|
||||||
import { Zap } from "lucide-react";
|
|
||||||
import type { ProviderCategory } from "@/types";
|
import type { ProviderCategory } from "@/types";
|
||||||
|
|
||||||
interface EndpointCandidate {
|
interface EndpointCandidate {
|
||||||
@@ -49,77 +46,39 @@ export function CodexFormFields({
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Codex API Key 输入框 */}
|
{/* Codex API Key 输入框 */}
|
||||||
<div className="space-y-1">
|
<ApiKeySection
|
||||||
<ApiKeyInput
|
|
||||||
id="codexApiKey"
|
id="codexApiKey"
|
||||||
label="API Key"
|
label="API Key"
|
||||||
value={codexApiKey}
|
value={codexApiKey}
|
||||||
onChange={onApiKeyChange}
|
onChange={onApiKeyChange}
|
||||||
required={category !== "official"}
|
category={category}
|
||||||
placeholder={
|
shouldShowLink={shouldShowApiKeyLink}
|
||||||
category === "official"
|
websiteUrl={websiteUrl}
|
||||||
? t("providerForm.codexOfficialNoApiKey", {
|
placeholder={{
|
||||||
|
official: t("providerForm.codexOfficialNoApiKey", {
|
||||||
defaultValue: "官方供应商无需 API Key",
|
defaultValue: "官方供应商无需 API Key",
|
||||||
})
|
}),
|
||||||
: t("providerForm.codexApiKeyAutoFill", {
|
thirdParty: t("providerForm.codexApiKeyAutoFill", {
|
||||||
defaultValue: "输入 API Key,将自动填充到配置",
|
defaultValue: "输入 API Key,将自动填充到配置",
|
||||||
})
|
}),
|
||||||
}
|
}}
|
||||||
disabled={category === "official"}
|
|
||||||
/>
|
/>
|
||||||
{/* Codex API Key 获取链接 */}
|
|
||||||
{shouldShowApiKeyLink && websiteUrl && (
|
|
||||||
<div className="-mt-1 pl-1">
|
|
||||||
<a
|
|
||||||
href={websiteUrl}
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
className="text-xs text-blue-400 dark:text-blue-500 hover:text-blue-500 dark:hover:text-blue-400 transition-colors"
|
|
||||||
>
|
|
||||||
{t("providerForm.getApiKey", {
|
|
||||||
defaultValue: "获取 API Key",
|
|
||||||
})}
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Codex Base URL 输入框 */}
|
{/* Codex Base URL 输入框 */}
|
||||||
{shouldShowSpeedTest && (
|
{shouldShowSpeedTest && (
|
||||||
<div className="space-y-2">
|
<EndpointField
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<FormLabel htmlFor="codexBaseUrl">
|
|
||||||
{t("codexConfig.apiUrlLabel", { defaultValue: "API 端点" })}
|
|
||||||
</FormLabel>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => onEndpointModalToggle(true)}
|
|
||||||
className="flex items-center gap-1 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
|
|
||||||
>
|
|
||||||
<Zap className="h-3.5 w-3.5" />
|
|
||||||
{t("providerForm.manageAndTest", {
|
|
||||||
defaultValue: "管理和测速",
|
|
||||||
})}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<Input
|
|
||||||
id="codexBaseUrl"
|
id="codexBaseUrl"
|
||||||
type="url"
|
label={t("codexConfig.apiUrlLabel", { defaultValue: "API 端点" })}
|
||||||
value={codexBaseUrl}
|
value={codexBaseUrl}
|
||||||
onChange={(e) => onBaseUrlChange(e.target.value)}
|
onChange={onBaseUrlChange}
|
||||||
placeholder={t("providerForm.codexApiEndpointPlaceholder", {
|
placeholder={t("providerForm.codexApiEndpointPlaceholder", {
|
||||||
defaultValue: "https://api.example.com/v1",
|
defaultValue: "https://api.example.com/v1",
|
||||||
})}
|
})}
|
||||||
autoComplete="off"
|
hint={t("providerForm.codexApiHint", {
|
||||||
/>
|
|
||||||
<div className="p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg">
|
|
||||||
<p className="text-xs text-amber-600 dark:text-amber-400">
|
|
||||||
{t("providerForm.codexApiHint", {
|
|
||||||
defaultValue: "Codex API 端点地址",
|
defaultValue: "Codex API 端点地址",
|
||||||
})}
|
})}
|
||||||
</p>
|
onManageClick={() => onEndpointModalToggle(true)}
|
||||||
</div>
|
/>
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* 端点测速弹窗 - Codex */}
|
{/* 端点测速弹窗 - Codex */}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
type CodexProviderPreset,
|
type CodexProviderPreset,
|
||||||
} from "@/config/codexProviderPresets";
|
} from "@/config/codexProviderPresets";
|
||||||
import { applyTemplateValues } from "@/utils/providerConfigUtils";
|
import { applyTemplateValues } from "@/utils/providerConfigUtils";
|
||||||
import CodexConfigEditor from "@/components/ProviderForm/CodexConfigEditor";
|
import CodexConfigEditor from "./CodexConfigEditor";
|
||||||
import { CommonConfigEditor } from "./CommonConfigEditor";
|
import { CommonConfigEditor } from "./CommonConfigEditor";
|
||||||
import { ProviderPresetSelector } from "./ProviderPresetSelector";
|
import { ProviderPresetSelector } from "./ProviderPresetSelector";
|
||||||
import { BasicFormFields } from "./BasicFormFields";
|
import { BasicFormFields } from "./BasicFormFields";
|
||||||
|
|||||||
76
src/components/providers/forms/shared/ApiKeySection.tsx
Normal file
76
src/components/providers/forms/shared/ApiKeySection.tsx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import ApiKeyInput from "../ApiKeyInput";
|
||||||
|
import type { ProviderCategory } from "@/types";
|
||||||
|
|
||||||
|
interface ApiKeySectionProps {
|
||||||
|
id?: string;
|
||||||
|
label?: string;
|
||||||
|
value: string;
|
||||||
|
onChange: (value: string) => void;
|
||||||
|
category?: ProviderCategory;
|
||||||
|
shouldShowLink: boolean;
|
||||||
|
websiteUrl: string;
|
||||||
|
placeholder?: {
|
||||||
|
official: string;
|
||||||
|
thirdParty: string;
|
||||||
|
};
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ApiKeySection({
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
category,
|
||||||
|
shouldShowLink,
|
||||||
|
websiteUrl,
|
||||||
|
placeholder,
|
||||||
|
disabled,
|
||||||
|
}: ApiKeySectionProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const defaultPlaceholder = {
|
||||||
|
official: t("providerForm.officialNoApiKey", {
|
||||||
|
defaultValue: "官方供应商无需 API Key",
|
||||||
|
}),
|
||||||
|
thirdParty: t("providerForm.apiKeyAutoFill", {
|
||||||
|
defaultValue: "输入 API Key,将自动填充到配置",
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const finalPlaceholder = placeholder || defaultPlaceholder;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-1">
|
||||||
|
<ApiKeyInput
|
||||||
|
id={id}
|
||||||
|
label={label}
|
||||||
|
value={value}
|
||||||
|
onChange={onChange}
|
||||||
|
required={category !== "official"}
|
||||||
|
placeholder={
|
||||||
|
category === "official"
|
||||||
|
? finalPlaceholder.official
|
||||||
|
: finalPlaceholder.thirdParty
|
||||||
|
}
|
||||||
|
disabled={disabled ?? category === "official"}
|
||||||
|
/>
|
||||||
|
{/* API Key 获取链接 */}
|
||||||
|
{shouldShowLink && websiteUrl && (
|
||||||
|
<div className="-mt-1 pl-1">
|
||||||
|
<a
|
||||||
|
href={websiteUrl}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="text-xs text-blue-400 dark:text-blue-500 hover:text-blue-500 dark:hover:text-blue-400 transition-colors"
|
||||||
|
>
|
||||||
|
{t("providerForm.getApiKey", {
|
||||||
|
defaultValue: "获取 API Key",
|
||||||
|
})}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
63
src/components/providers/forms/shared/EndpointField.tsx
Normal file
63
src/components/providers/forms/shared/EndpointField.tsx
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { FormLabel } from "@/components/ui/form";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Zap } from "lucide-react";
|
||||||
|
|
||||||
|
interface EndpointFieldProps {
|
||||||
|
id: string;
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
onChange: (value: string) => void;
|
||||||
|
placeholder: string;
|
||||||
|
hint: string;
|
||||||
|
showManageButton?: boolean;
|
||||||
|
onManageClick?: () => void;
|
||||||
|
manageButtonLabel?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function EndpointField({
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
placeholder,
|
||||||
|
hint,
|
||||||
|
showManageButton = true,
|
||||||
|
onManageClick,
|
||||||
|
manageButtonLabel,
|
||||||
|
}: EndpointFieldProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const defaultManageLabel = t("providerForm.manageAndTest", {
|
||||||
|
defaultValue: "管理和测速",
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<FormLabel htmlFor={id}>{label}</FormLabel>
|
||||||
|
{showManageButton && onManageClick && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onManageClick}
|
||||||
|
className="flex items-center gap-1 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
|
||||||
|
>
|
||||||
|
<Zap className="h-3.5 w-3.5" />
|
||||||
|
{manageButtonLabel || defaultManageLabel}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<Input
|
||||||
|
id={id}
|
||||||
|
type="url"
|
||||||
|
value={value}
|
||||||
|
onChange={(e) => onChange(e.target.value)}
|
||||||
|
placeholder={placeholder}
|
||||||
|
autoComplete="off"
|
||||||
|
/>
|
||||||
|
<div className="p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg">
|
||||||
|
<p className="text-xs text-amber-600 dark:text-amber-400">{hint}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
2
src/components/providers/forms/shared/index.ts
Normal file
2
src/components/providers/forms/shared/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export { ApiKeySection } from "./ApiKeySection";
|
||||||
|
export { EndpointField } from "./EndpointField";
|
||||||
Reference in New Issue
Block a user