Files
cc-switch/src/components/providers/ProviderCard.tsx

164 lines
5.1 KiB
TypeScript
Raw Normal View History

2025-10-16 10:49:56 +08:00
import { useMemo } from "react";
import { MoveVertical } from "lucide-react";
2025-10-16 10:49:56 +08:00
import { useTranslation } from "react-i18next";
import type {
DraggableAttributes,
DraggableSyntheticListeners,
} from "@dnd-kit/core";
import type { Provider } from "@/types";
import type { AppType } from "@/lib/api";
import { cn } from "@/lib/utils";
import { ProviderActions } from "@/components/providers/ProviderActions";
import UsageFooter from "@/components/UsageFooter";
interface DragHandleProps {
attributes: DraggableAttributes;
listeners: DraggableSyntheticListeners;
isDragging: boolean;
}
interface ProviderCardProps {
provider: Provider;
isCurrent: boolean;
appType: AppType;
isEditMode?: boolean;
2025-10-16 10:49:56 +08:00
onSwitch: (provider: Provider) => void;
onEdit: (provider: Provider) => void;
onDelete: (provider: Provider) => void;
onConfigureUsage: (provider: Provider) => void;
onOpenWebsite: (url: string) => void;
dragHandleProps?: DragHandleProps;
}
const extractApiUrl = (provider: Provider, fallbackText: string) => {
if (provider.websiteUrl) {
return provider.websiteUrl;
}
const config = provider.settingsConfig;
if (config && typeof config === "object") {
const envBase = (config as Record<string, any>)?.env?.ANTHROPIC_BASE_URL;
if (typeof envBase === "string" && envBase.trim()) {
return envBase;
}
const baseUrl = (config as Record<string, any>)?.config;
if (typeof baseUrl === "string" && baseUrl.includes("base_url")) {
const match = baseUrl.match(/base_url\s*=\s*['"]([^'"]+)['"]/);
if (match?.[1]) {
return match[1];
}
}
}
return fallbackText;
};
export function ProviderCard({
provider,
isCurrent,
appType,
isEditMode = false,
2025-10-16 10:49:56 +08:00
onSwitch,
onEdit,
onDelete,
onConfigureUsage,
onOpenWebsite,
dragHandleProps,
}: ProviderCardProps) {
const { t } = useTranslation();
const fallbackUrlText = t("provider.notConfigured", {
defaultValue: "未配置接口地址",
});
const displayUrl = useMemo(() => {
return extractApiUrl(provider, fallbackUrlText);
}, [provider, fallbackUrlText]);
const usageEnabled = provider.meta?.usage_script?.enabled ?? false;
const handleOpenWebsite = () => {
if (!displayUrl || displayUrl === fallbackUrlText) {
return;
}
onOpenWebsite(displayUrl);
};
return (
<div
className={cn(
"rounded-lg bg-card p-4 shadow-sm transition-all duration-200",
2025-10-16 10:49:56 +08:00
isCurrent
? "border-active border-border-active bg-primary/5"
: "border border-border-default hover:border-border-hover",
feat: complete stage 4 cleanup and code formatting This commit completes stage 4 of the refactoring plan, focusing on cleanup and optimization of the modernized codebase. ## Key Changes ### Code Cleanup - Remove legacy `src/lib/styles.ts` (no longer needed) - Remove old modal components (`ImportProgressModal.tsx`, `ProviderList.tsx`) - Streamline `src/lib/tauri-api.ts` from 712 lines to 17 lines (-97.6%) - Remove global `window.api` pollution - Keep only event listeners (`tauriEvents.onProviderSwitched`) - All API calls now use modular `@/lib/api/*` layer ### Type System - Clean up `src/vite-env.d.ts` (remove 156 lines of outdated types) - Remove obsolete global type declarations - All TypeScript checks pass with zero errors ### Code Formatting - Format all source files with Prettier (82 files) - Fix formatting issues in 15 files: - App.tsx and core components - MCP management components - Settings module components - Provider management components - UI components ### Documentation Updates - Update `REFACTORING_CHECKLIST.md` with stage 4 progress - Mark completed tasks in `REFACTORING_MASTER_PLAN.md` ## Impact **Code Reduction:** - Total: -1,753 lines, +384 lines (net -1,369 lines) - tauri-api.ts: 712 → 17 lines (-97.6%) - Removed styles.ts: -82 lines - Removed vite-env.d.ts declarations: -156 lines **Quality Improvements:** - ✅ Zero TypeScript errors - ✅ Zero TODO/FIXME comments - ✅ 100% Prettier compliant - ✅ Zero `window.api` references - ✅ Fully modular API layer ## Testing - [x] TypeScript compilation passes - [x] Code formatting validated - [x] No linting errors Stage 4 completion: 100% Ready for stage 5 (testing and bug fixes)
2025-10-16 12:13:51 +08:00
dragHandleProps?.isDragging &&
"cursor-grabbing border-active border-border-dragging shadow-lg",
2025-10-16 10:49:56 +08:00
)}
>
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div className="flex flex-1 items-start">
2025-10-16 10:49:56 +08:00
<button
type="button"
className={cn(
"mt-1 flex h-8 flex-shrink-0 items-center justify-center overflow-hidden rounded-md border text-muted-foreground transition-all duration-200 cursor-grab active:cursor-grabbing",
isEditMode
? "w-8 mr-3 border-muted hover:border-border-hover hover:text-foreground hover:bg-muted/50 opacity-100"
: "w-0 mr-0 border-transparent opacity-0 pointer-events-none",
dragHandleProps?.isDragging && "border-border-active text-primary bg-primary/10 cursor-grabbing",
2025-10-16 10:49:56 +08:00
)}
aria-label={t("provider.dragHandle")}
2025-10-16 10:49:56 +08:00
{...(dragHandleProps?.attributes ?? {})}
{...(dragHandleProps?.listeners ?? {})}
>
<MoveVertical className="h-4 w-4" />
2025-10-16 10:49:56 +08:00
</button>
<div className="space-y-1">
<div className="flex flex-wrap items-center gap-2 min-h-[20px]">
2025-10-16 10:49:56 +08:00
<h3 className="text-base font-semibold leading-none">
{provider.name}
</h3>
<span
className={cn(
"rounded-full bg-green-500/10 px-2 py-0.5 text-xs font-medium text-green-500 dark:text-green-400 transition-opacity duration-200",
isCurrent ? "opacity-100" : "opacity-0 pointer-events-none"
)}
>
{t("provider.currentlyUsing")}
</span>
2025-10-16 10:49:56 +08:00
</div>
{displayUrl && (
<button
type="button"
onClick={handleOpenWebsite}
className="inline-flex items-center text-sm text-blue-500 transition-colors hover:underline dark:text-blue-400"
2025-10-16 10:49:56 +08:00
title={displayUrl}
>
<span className="truncate">{displayUrl}</span>
</button>
)}
</div>
</div>
<ProviderActions
isCurrent={isCurrent}
onSwitch={() => onSwitch(provider)}
onEdit={() => onEdit(provider)}
onConfigureUsage={() => onConfigureUsage(provider)}
onDelete={() => onDelete(provider)}
/>
</div>
<UsageFooter
providerId={provider.id}
appType={appType}
usageEnabled={usageEnabled}
/>
</div>
);
}