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

161 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-10-16 10:49:56 +08:00
import { CSS } from "@dnd-kit/utilities";
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
import { DndContext, closestCenter } from "@dnd-kit/core";
2025-10-16 10:49:56 +08:00
import {
SortableContext,
useSortable,
verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import type { CSSProperties } from "react";
import type { Provider } from "@/types";
import type { AppId } from "@/lib/api";
2025-10-16 10:49:56 +08:00
import { useDragSort } from "@/hooks/useDragSort";
import { ProviderCard } from "@/components/providers/ProviderCard";
import { ProviderEmptyState } from "@/components/providers/ProviderEmptyState";
interface ProviderListProps {
providers: Record<string, Provider>;
currentProviderId: string;
appId: AppId;
isEditMode?: boolean;
2025-10-16 10:49:56 +08:00
onSwitch: (provider: Provider) => void;
onEdit: (provider: Provider) => void;
onDelete: (provider: Provider) => void;
onDuplicate: (provider: Provider) => void;
2025-10-16 10:49:56 +08:00
onConfigureUsage?: (provider: Provider) => void;
onOpenWebsite: (url: string) => void;
onCreate?: () => void;
isLoading?: boolean;
}
export function ProviderList({
providers,
currentProviderId,
appId,
isEditMode = false,
2025-10-16 10:49:56 +08:00
onSwitch,
onEdit,
onDelete,
onDuplicate,
2025-10-16 10:49:56 +08:00
onConfigureUsage,
onOpenWebsite,
onCreate,
isLoading = false,
}: ProviderListProps) {
const { sortedProviders, sensors, handleDragEnd } = useDragSort(
providers,
appId,
2025-10-16 10:49:56 +08:00
);
if (isLoading) {
return (
<div className="space-y-3">
{[0, 1, 2].map((index) => (
<div
key={index}
className="h-28 w-full rounded-lg border border-dashed border-muted-foreground/40 bg-muted/40"
/>
))}
</div>
);
}
if (sortedProviders.length === 0) {
return <ProviderEmptyState onCreate={onCreate} />;
}
return (
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragEnd={handleDragEnd}
>
<SortableContext
items={sortedProviders.map((provider) => provider.id)}
strategy={verticalListSortingStrategy}
>
<div className="space-y-3">
{sortedProviders.map((provider) => (
<SortableProviderCard
key={provider.id}
provider={provider}
isCurrent={provider.id === currentProviderId}
appId={appId}
isEditMode={isEditMode}
2025-10-16 10:49:56 +08:00
onSwitch={onSwitch}
onEdit={onEdit}
onDelete={onDelete}
onDuplicate={onDuplicate}
2025-10-16 10:49:56 +08:00
onConfigureUsage={onConfigureUsage}
onOpenWebsite={onOpenWebsite}
/>
))}
</div>
</SortableContext>
</DndContext>
);
}
interface SortableProviderCardProps {
provider: Provider;
isCurrent: boolean;
appId: AppId;
isEditMode: boolean;
2025-10-16 10:49:56 +08:00
onSwitch: (provider: Provider) => void;
onEdit: (provider: Provider) => void;
onDelete: (provider: Provider) => void;
onDuplicate: (provider: Provider) => void;
2025-10-16 10:49:56 +08:00
onConfigureUsage?: (provider: Provider) => void;
onOpenWebsite: (url: string) => void;
}
function SortableProviderCard({
provider,
isCurrent,
appId,
isEditMode,
2025-10-16 10:49:56 +08:00
onSwitch,
onEdit,
onDelete,
onDuplicate,
2025-10-16 10:49:56 +08:00
onConfigureUsage,
onOpenWebsite,
}: SortableProviderCardProps) {
const {
setNodeRef,
attributes,
listeners,
transform,
transition,
isDragging,
} = useSortable({ id: provider.id });
const style: CSSProperties = {
transform: CSS.Transform.toString(transform),
transition,
};
return (
<div ref={setNodeRef} style={style}>
<ProviderCard
provider={provider}
isCurrent={isCurrent}
appId={appId}
isEditMode={isEditMode}
2025-10-16 10:49:56 +08:00
onSwitch={onSwitch}
onEdit={onEdit}
onDelete={onDelete}
onDuplicate={onDuplicate}
2025-10-16 10:49:56 +08:00
onConfigureUsage={
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
onConfigureUsage ? (item) => onConfigureUsage(item) : () => undefined
2025-10-16 10:49:56 +08:00
}
onOpenWebsite={onOpenWebsite}
dragHandleProps={{
attributes,
listeners,
isDragging,
}}
/>
</div>
);
}