2025-10-17 17:49:16 +08:00
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2025-09-28 20:47:44 +08:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-10-16 10:49:56 +08:00
|
|
|
|
import { toast } from "sonner";
|
2025-10-19 22:12:12 +08:00
|
|
|
|
import { Plus, Settings, Edit3 } from "lucide-react";
|
2025-10-17 17:49:16 +08:00
|
|
|
|
import type { Provider } from "@/types";
|
|
|
|
|
|
import { useProvidersQuery } from "@/lib/query";
|
2025-10-18 16:52:02 +08:00
|
|
|
|
import {
|
|
|
|
|
|
providersApi,
|
|
|
|
|
|
settingsApi,
|
|
|
|
|
|
type AppType,
|
|
|
|
|
|
type ProviderSwitchEvent,
|
|
|
|
|
|
} from "@/lib/api";
|
2025-10-17 17:49:16 +08:00
|
|
|
|
import { useProviderActions } from "@/hooks/useProviderActions";
|
2025-10-16 10:49:56 +08:00
|
|
|
|
import { extractErrorMessage } from "@/utils/errorUtils";
|
|
|
|
|
|
import { AppSwitcher } from "@/components/AppSwitcher";
|
|
|
|
|
|
import { ProviderList } from "@/components/providers/ProviderList";
|
|
|
|
|
|
import { AddProviderDialog } from "@/components/providers/AddProviderDialog";
|
|
|
|
|
|
import { EditProviderDialog } from "@/components/providers/EditProviderDialog";
|
|
|
|
|
|
import { ConfirmDialog } from "@/components/ConfirmDialog";
|
2025-10-16 11:40:02 +08:00
|
|
|
|
import { SettingsDialog } from "@/components/settings/SettingsDialog";
|
2025-10-16 10:49:56 +08:00
|
|
|
|
import { UpdateBadge } from "@/components/UpdateBadge";
|
|
|
|
|
|
import UsageScriptModal from "@/components/UsageScriptModal";
|
|
|
|
|
|
import McpPanel from "@/components/mcp/McpPanel";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
function App() {
|
2025-09-28 20:47:44 +08:00
|
|
|
|
const { t } = useTranslation();
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
const [activeApp, setActiveApp] = useState<AppType>("claude");
|
2025-10-19 22:12:12 +08:00
|
|
|
|
const [isEditMode, setIsEditMode] = useState(false);
|
2025-09-07 10:48:27 +08:00
|
|
|
|
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
|
2025-10-16 10:49:56 +08:00
|
|
|
|
const [isAddOpen, setIsAddOpen] = useState(false);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
const [isMcpOpen, setIsMcpOpen] = useState(false);
|
2025-10-16 10:49:56 +08:00
|
|
|
|
const [editingProvider, setEditingProvider] = useState<Provider | null>(null);
|
|
|
|
|
|
const [usageProvider, setUsageProvider] = useState<Provider | null>(null);
|
|
|
|
|
|
const [confirmDelete, setConfirmDelete] = useState<Provider | null>(null);
|
2025-08-06 20:48:03 +08:00
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
const { data, isLoading, refetch } = useProvidersQuery(activeApp);
|
|
|
|
|
|
const providers = useMemo(() => data?.providers ?? {}, [data]);
|
|
|
|
|
|
const currentProviderId = data?.currentProviderId ?? "";
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 🎯 使用 useProviderActions Hook 统一管理所有 Provider 操作
|
|
|
|
|
|
const {
|
|
|
|
|
|
addProvider,
|
|
|
|
|
|
updateProvider,
|
|
|
|
|
|
switchProvider,
|
|
|
|
|
|
deleteProvider,
|
|
|
|
|
|
saveUsageScript,
|
|
|
|
|
|
} = useProviderActions(activeApp);
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 监听来自托盘菜单的切换事件
|
2025-09-06 16:21:21 +08:00
|
|
|
|
useEffect(() => {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
let unsubscribe: (() => void) | undefined;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
const setupListener = async () => {
|
|
|
|
|
|
try {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
unsubscribe = await providersApi.onSwitched(
|
2025-10-16 10:49:56 +08:00
|
|
|
|
async (event: ProviderSwitchEvent) => {
|
|
|
|
|
|
if (event.appType === activeApp) {
|
|
|
|
|
|
await refetch();
|
|
|
|
|
|
}
|
2025-10-24 11:56:18 +08:00
|
|
|
|
},
|
2025-10-16 10:49:56 +08:00
|
|
|
|
);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
} catch (error) {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
console.error("[App] Failed to subscribe provider switch event", error);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
setupListener();
|
|
|
|
|
|
return () => {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
unsubscribe?.();
|
2025-09-06 16:21:21 +08:00
|
|
|
|
};
|
2025-10-16 10:49:56 +08:00
|
|
|
|
}, [activeApp, refetch]);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 打开网站链接
|
|
|
|
|
|
const handleOpenWebsite = async (url: string) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await settingsApi.openExternal(url);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const detail =
|
|
|
|
|
|
extractErrorMessage(error) ||
|
|
|
|
|
|
t("notifications.openLinkFailed", {
|
|
|
|
|
|
defaultValue: "链接打开失败",
|
|
|
|
|
|
});
|
|
|
|
|
|
toast.error(detail);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-10-01 21:23:55 +08:00
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 编辑供应商
|
|
|
|
|
|
const handleEditProvider = async (provider: Provider) => {
|
|
|
|
|
|
await updateProvider(provider);
|
|
|
|
|
|
setEditingProvider(null);
|
|
|
|
|
|
};
|
2025-09-19 11:06:26 +08:00
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 确认删除供应商
|
|
|
|
|
|
const handleConfirmDelete = async () => {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
if (!confirmDelete) return;
|
2025-10-17 17:49:16 +08:00
|
|
|
|
await deleteProvider(confirmDelete.id);
|
|
|
|
|
|
setConfirmDelete(null);
|
|
|
|
|
|
};
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-10-24 11:56:18 +08:00
|
|
|
|
// 复制供应商
|
|
|
|
|
|
const handleDuplicateProvider = async (provider: Provider) => {
|
2025-10-24 13:02:07 +08:00
|
|
|
|
// 1️⃣ 计算新的 sortIndex:如果原供应商有 sortIndex,则复制它
|
|
|
|
|
|
const newSortIndex =
|
|
|
|
|
|
provider.sortIndex !== undefined ? provider.sortIndex + 1 : undefined;
|
|
|
|
|
|
|
2025-10-24 11:56:18 +08:00
|
|
|
|
const duplicatedProvider: Omit<Provider, "id" | "createdAt"> = {
|
|
|
|
|
|
name: `${provider.name} copy`,
|
|
|
|
|
|
settingsConfig: JSON.parse(JSON.stringify(provider.settingsConfig)), // 深拷贝
|
|
|
|
|
|
websiteUrl: provider.websiteUrl,
|
|
|
|
|
|
category: provider.category,
|
2025-10-24 13:02:07 +08:00
|
|
|
|
sortIndex: newSortIndex, // 复制原 sortIndex + 1
|
2025-10-24 11:56:18 +08:00
|
|
|
|
meta: provider.meta
|
|
|
|
|
|
? JSON.parse(JSON.stringify(provider.meta))
|
|
|
|
|
|
: undefined, // 深拷贝
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-24 13:02:07 +08:00
|
|
|
|
// 2️⃣ 如果原供应商有 sortIndex,需要将后续所有供应商的 sortIndex +1
|
|
|
|
|
|
if (provider.sortIndex !== undefined) {
|
|
|
|
|
|
const updates = Object.values(providers)
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
(p) =>
|
|
|
|
|
|
p.sortIndex !== undefined &&
|
|
|
|
|
|
p.sortIndex >= newSortIndex! &&
|
|
|
|
|
|
p.id !== provider.id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map((p) => ({
|
|
|
|
|
|
id: p.id,
|
|
|
|
|
|
sortIndex: p.sortIndex! + 1,
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// 先更新现有供应商的 sortIndex,为新供应商腾出位置
|
|
|
|
|
|
if (updates.length > 0) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await providersApi.updateSortOrder(updates, activeApp);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("[App] Failed to update sort order", error);
|
|
|
|
|
|
toast.error(
|
|
|
|
|
|
t("provider.sortUpdateFailed", {
|
|
|
|
|
|
defaultValue: "排序更新失败",
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
return; // 如果排序更新失败,不继续添加
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3️⃣ 添加复制的供应商
|
2025-10-24 11:56:18 +08:00
|
|
|
|
await addProvider(duplicatedProvider);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-17 17:49:16 +08:00
|
|
|
|
// 导入配置成功后刷新
|
|
|
|
|
|
const handleImportSuccess = async () => {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
await refetch();
|
2025-10-05 23:33:07 +08:00
|
|
|
|
try {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
await providersApi.updateTrayMenu();
|
2025-10-05 23:33:07 +08:00
|
|
|
|
} catch (error) {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
console.error("[App] Failed to refresh tray menu", error);
|
2025-10-05 23:33:07 +08:00
|
|
|
|
}
|
2025-10-17 17:49:16 +08:00
|
|
|
|
};
|
2025-08-07 21:28:45 +08:00
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
return (
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<div className="flex h-screen flex-col bg-gray-50 dark:bg-gray-950">
|
|
|
|
|
|
<header className="flex-shrink-0 border-b border-gray-200 bg-white px-6 py-4 dark:border-gray-800 dark:bg-gray-900">
|
2025-10-19 23:29:13 +08:00
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
|
|
|
|
<div className="flex items-center gap-1">
|
2025-09-15 10:24:41 +08:00
|
|
|
|
<a
|
|
|
|
|
|
href="https://github.com/farion1231/cc-switch"
|
|
|
|
|
|
target="_blank"
|
2025-10-16 10:49:56 +08:00
|
|
|
|
rel="noreferrer"
|
|
|
|
|
|
className="text-xl font-semibold text-blue-500 transition-colors hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300"
|
2025-09-15 10:24:41 +08:00
|
|
|
|
>
|
2025-09-07 10:48:27 +08:00
|
|
|
|
CC Switch
|
2025-09-15 10:24:41 +08:00
|
|
|
|
</a>
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => setIsSettingsOpen(true)}
|
2025-10-19 22:12:12 +08:00
|
|
|
|
title={t("common.settings")}
|
|
|
|
|
|
className="ml-2"
|
2025-09-08 15:38:06 +08:00
|
|
|
|
>
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<Settings className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
2025-10-19 22:12:12 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => setIsEditMode(!isEditMode)}
|
|
|
|
|
|
title={t(
|
2025-10-24 11:56:18 +08:00
|
|
|
|
isEditMode ? "header.exitEditMode" : "header.enterEditMode",
|
2025-10-19 22:12:12 +08:00
|
|
|
|
)}
|
|
|
|
|
|
className={
|
|
|
|
|
|
isEditMode
|
|
|
|
|
|
? "text-blue-500 hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300"
|
|
|
|
|
|
: ""
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Edit3 className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<UpdateBadge onClick={() => setIsSettingsOpen(true)} />
|
2025-09-07 10:48:27 +08:00
|
|
|
|
</div>
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
2025-10-19 23:29:13 +08:00
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
2025-09-06 16:21:21 +08:00
|
|
|
|
<AppSwitcher activeApp={activeApp} onSwitch={setActiveApp} />
|
2025-10-16 15:32:26 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="mcp"
|
|
|
|
|
|
onClick={() => setIsMcpOpen(true)}
|
|
|
|
|
|
className="min-w-[80px]"
|
|
|
|
|
|
>
|
2025-10-08 21:22:56 +08:00
|
|
|
|
MCP
|
2025-10-16 10:49:56 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
<Button onClick={() => setIsAddOpen(true)}>
|
|
|
|
|
|
<Plus className="h-4 w-4" />
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("header.addProvider")}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
</Button>
|
2025-09-06 16:21:21 +08:00
|
|
|
|
</div>
|
2025-08-04 22:16:26 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</header>
|
|
|
|
|
|
|
2025-10-17 21:51:37 +08:00
|
|
|
|
<main className="flex-1 overflow-y-scroll">
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<div className="mx-auto max-w-4xl px-6 py-6">
|
|
|
|
|
|
<ProviderList
|
|
|
|
|
|
providers={providers}
|
|
|
|
|
|
currentProviderId={currentProviderId}
|
|
|
|
|
|
appType={activeApp}
|
|
|
|
|
|
isLoading={isLoading}
|
2025-10-19 22:12:12 +08:00
|
|
|
|
isEditMode={isEditMode}
|
2025-10-17 17:49:16 +08:00
|
|
|
|
onSwitch={switchProvider}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
onEdit={setEditingProvider}
|
2025-10-17 17:49:16 +08:00
|
|
|
|
onDelete={setConfirmDelete}
|
2025-10-24 11:56:18 +08:00
|
|
|
|
onDuplicate={handleDuplicateProvider}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
onConfigureUsage={setUsageProvider}
|
|
|
|
|
|
onOpenWebsite={handleOpenWebsite}
|
|
|
|
|
|
onCreate={() => setIsAddOpen(true)}
|
|
|
|
|
|
/>
|
2025-09-06 16:21:21 +08:00
|
|
|
|
</div>
|
2025-08-04 22:16:26 +08:00
|
|
|
|
</main>
|
|
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<AddProviderDialog
|
|
|
|
|
|
open={isAddOpen}
|
|
|
|
|
|
onOpenChange={setIsAddOpen}
|
|
|
|
|
|
appType={activeApp}
|
2025-10-17 17:49:16 +08:00
|
|
|
|
onSubmit={addProvider}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<EditProviderDialog
|
|
|
|
|
|
open={Boolean(editingProvider)}
|
|
|
|
|
|
provider={editingProvider}
|
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
|
if (!open) {
|
|
|
|
|
|
setEditingProvider(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
|
|
|
|
|
onSubmit={handleEditProvider}
|
2025-10-16 13:02:38 +08:00
|
|
|
|
appType={activeApp}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
/>
|
2025-08-05 09:51:41 +08:00
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
{usageProvider && (
|
|
|
|
|
|
<UsageScriptModal
|
|
|
|
|
|
provider={usageProvider}
|
2025-08-30 21:54:11 +08:00
|
|
|
|
appType={activeApp}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
isOpen={Boolean(usageProvider)}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
onClose={() => setUsageProvider(null)}
|
|
|
|
|
|
onSave={(script) => {
|
2025-10-17 17:49:16 +08:00
|
|
|
|
void saveUsageScript(usageProvider, script);
|
2025-10-16 10:49:56 +08:00
|
|
|
|
}}
|
2025-08-05 09:51:41 +08:00
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-08-06 16:29:52 +08:00
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
<ConfirmDialog
|
|
|
|
|
|
isOpen={Boolean(confirmDelete)}
|
2025-10-19 11:55:46 +08:00
|
|
|
|
title={t("confirm.deleteProvider")}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
message={
|
|
|
|
|
|
confirmDelete
|
|
|
|
|
|
? t("confirm.deleteProviderMessage", {
|
|
|
|
|
|
name: confirmDelete.name,
|
|
|
|
|
|
})
|
|
|
|
|
|
: ""
|
|
|
|
|
|
}
|
|
|
|
|
|
onConfirm={() => void handleConfirmDelete()}
|
|
|
|
|
|
onCancel={() => setConfirmDelete(null)}
|
|
|
|
|
|
/>
|
2025-09-07 10:48:27 +08:00
|
|
|
|
|
2025-10-16 11:40:02 +08:00
|
|
|
|
<SettingsDialog
|
|
|
|
|
|
open={isSettingsOpen}
|
|
|
|
|
|
onOpenChange={setIsSettingsOpen}
|
|
|
|
|
|
onImportSuccess={handleImportSuccess}
|
|
|
|
|
|
/>
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-16 16:20:45 +08:00
|
|
|
|
<McpPanel
|
|
|
|
|
|
open={isMcpOpen}
|
|
|
|
|
|
onOpenChange={setIsMcpOpen}
|
|
|
|
|
|
appType={activeApp}
|
|
|
|
|
|
/>
|
2025-08-04 22:16:26 +08:00
|
|
|
|
</div>
|
2025-08-06 20:48:03 +08:00
|
|
|
|
);
|
2025-08-04 22:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 20:48:03 +08:00
|
|
|
|
export default App;
|