mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 12:44:18 +08:00
bb48f4f6af
This commit completes Stage 2.5-2.6 of the refactoring plan by: - Consolidating 8 provider form files (1941+ lines) into a single unified ProviderForm component (353 lines), reducing code by ~82% - Implementing modern form management with react-hook-form and zod - Adding preset provider categorization with grouped select UI - Supporting dual-mode operation for both Claude and Codex configs - Removing redundant subcomponents: - ApiKeyInput.tsx (72 lines) - ClaudeConfigEditor.tsx (205 lines) - CodexConfigEditor.tsx (667 lines) - EndpointSpeedTest.tsx (636 lines) - KimiModelSelector.tsx (195 lines) - PresetSelector.tsx (119 lines) Key improvements: - Type-safe form values with ProviderFormValues extension - Automatic template value application for presets - Better internationalization coverage - Cleaner separation of concerns - Enhanced UX with categorized preset groups Updates AddProviderDialog and EditProviderDialog to pass appType prop and handle preset category metadata.
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { useCallback } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import type { Provider } from "@/types";
|
|
import type { AppType } from "@/lib/api";
|
|
import {
|
|
ProviderForm,
|
|
type ProviderFormValues,
|
|
} from "@/components/providers/forms/ProviderForm";
|
|
|
|
interface AddProviderDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
appType: AppType;
|
|
onSubmit: (provider: Omit<Provider, "id">) => Promise<void> | void;
|
|
}
|
|
|
|
export function AddProviderDialog({
|
|
open,
|
|
onOpenChange,
|
|
appType,
|
|
onSubmit,
|
|
}: AddProviderDialogProps) {
|
|
const { t } = useTranslation();
|
|
|
|
const handleSubmit = useCallback(
|
|
async (values: ProviderFormValues) => {
|
|
const parsedConfig = JSON.parse(values.settingsConfig) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
|
|
const providerData: Omit<Provider, "id"> = {
|
|
name: values.name.trim(),
|
|
websiteUrl: values.websiteUrl?.trim() || undefined,
|
|
settingsConfig: parsedConfig,
|
|
...(values.presetCategory ? { category: values.presetCategory } : {}),
|
|
};
|
|
|
|
await onSubmit(providerData);
|
|
onOpenChange(false);
|
|
},
|
|
[onSubmit, onOpenChange],
|
|
);
|
|
|
|
const submitLabel =
|
|
appType === "claude"
|
|
? t("provider.addClaudeProvider", { defaultValue: "添加 Claude 供应商" })
|
|
: t("provider.addCodexProvider", { defaultValue: "添加 Codex 供应商" });
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="max-w-2xl">
|
|
<DialogHeader>
|
|
<DialogTitle>{submitLabel}</DialogTitle>
|
|
<DialogDescription>
|
|
{t("provider.addDescription", {
|
|
defaultValue: "填写信息后即可在列表中快速切换供应商。",
|
|
})}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<ProviderForm
|
|
appType={appType}
|
|
submitLabel={t("common.add", { defaultValue: "添加" })}
|
|
onSubmit={handleSubmit}
|
|
onCancel={() => onOpenChange(false)}
|
|
/>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|