refactor(types): rename AppType to AppId for semantic clarity
Rename `AppType` to `AppId` across the entire frontend codebase to better reflect its purpose as an application identifier rather than a type category. This aligns frontend naming with backend command parameter conventions. Changes: - Rename type `AppType` to `AppId` in src/lib/api/types.ts - Remove `AppType` export from src/lib/api/index.ts - Update all component props from `appType` to `appId` (43 files) - Update all variable names from `appType` to `appId` - Synchronize documentation (CHANGELOG, refactoring plans) - Update test files and MSW mocks BREAKING CHANGE: `AppType` type is no longer exported. Use `AppId` instead. All component props have been renamed from `appType` to `appId`.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
export type { AppType, AppId } from "./types";
|
||||
export type { AppId } from "./types";
|
||||
export { providersApi } from "./providers";
|
||||
export { settingsApi } from "./settings";
|
||||
export { mcpApi } from "./mcp";
|
||||
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
McpServerSpec,
|
||||
McpStatus,
|
||||
} from "@/types";
|
||||
import type { AppType } from "./types";
|
||||
import type { AppId } from "./types";
|
||||
|
||||
export const mcpApi = {
|
||||
async getStatus(): Promise<McpStatus> {
|
||||
@@ -31,7 +31,7 @@ export const mcpApi = {
|
||||
return await invoke("validate_mcp_command", { cmd });
|
||||
},
|
||||
|
||||
async getConfig(app: AppType = "claude"): Promise<McpConfigResponse> {
|
||||
async getConfig(app: AppId = "claude"): Promise<McpConfigResponse> {
|
||||
return await invoke("get_mcp_config", { app });
|
||||
},
|
||||
|
||||
@@ -44,7 +44,7 @@ export const mcpApi = {
|
||||
},
|
||||
|
||||
async upsertServerInConfig(
|
||||
app: AppType,
|
||||
app: AppId,
|
||||
id: string,
|
||||
spec: McpServer,
|
||||
options?: { syncOtherSide?: boolean },
|
||||
@@ -61,7 +61,7 @@ export const mcpApi = {
|
||||
},
|
||||
|
||||
async deleteServerInConfig(
|
||||
app: AppType,
|
||||
app: AppId,
|
||||
id: string,
|
||||
options?: { syncOtherSide?: boolean },
|
||||
): Promise<boolean> {
|
||||
@@ -76,7 +76,7 @@ export const mcpApi = {
|
||||
},
|
||||
|
||||
async setEnabled(
|
||||
app: AppType,
|
||||
app: AppId,
|
||||
id: string,
|
||||
enabled: boolean,
|
||||
): Promise<boolean> {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
||||
import type { Provider } from "@/types";
|
||||
import type { AppType } from "./types";
|
||||
import type { AppId } from "./types";
|
||||
|
||||
export interface ProviderSortUpdate {
|
||||
id: string;
|
||||
@@ -9,37 +9,37 @@ export interface ProviderSortUpdate {
|
||||
}
|
||||
|
||||
export interface ProviderSwitchEvent {
|
||||
appType: AppType;
|
||||
appType: AppId;
|
||||
providerId: string;
|
||||
}
|
||||
|
||||
export const providersApi = {
|
||||
async getAll(appType: AppType): Promise<Record<string, Provider>> {
|
||||
return await invoke("get_providers", { app: appType });
|
||||
async getAll(appId: AppId): Promise<Record<string, Provider>> {
|
||||
return await invoke("get_providers", { app: appId });
|
||||
},
|
||||
|
||||
async getCurrent(appType: AppType): Promise<string> {
|
||||
return await invoke("get_current_provider", { app: appType });
|
||||
async getCurrent(appId: AppId): Promise<string> {
|
||||
return await invoke("get_current_provider", { app: appId });
|
||||
},
|
||||
|
||||
async add(provider: Provider, appType: AppType): Promise<boolean> {
|
||||
return await invoke("add_provider", { provider, app: appType });
|
||||
async add(provider: Provider, appId: AppId): Promise<boolean> {
|
||||
return await invoke("add_provider", { provider, app: appId });
|
||||
},
|
||||
|
||||
async update(provider: Provider, appType: AppType): Promise<boolean> {
|
||||
return await invoke("update_provider", { provider, app: appType });
|
||||
async update(provider: Provider, appId: AppId): Promise<boolean> {
|
||||
return await invoke("update_provider", { provider, app: appId });
|
||||
},
|
||||
|
||||
async delete(id: string, appType: AppType): Promise<boolean> {
|
||||
return await invoke("delete_provider", { id, app: appType });
|
||||
async delete(id: string, appId: AppId): Promise<boolean> {
|
||||
return await invoke("delete_provider", { id, app: appId });
|
||||
},
|
||||
|
||||
async switch(id: string, appType: AppType): Promise<boolean> {
|
||||
return await invoke("switch_provider", { id, app: appType });
|
||||
async switch(id: string, appId: AppId): Promise<boolean> {
|
||||
return await invoke("switch_provider", { id, app: appId });
|
||||
},
|
||||
|
||||
async importDefault(appType: AppType): Promise<boolean> {
|
||||
return await invoke("import_default_config", { app: appType });
|
||||
async importDefault(appId: AppId): Promise<boolean> {
|
||||
return await invoke("import_default_config", { app: appId });
|
||||
},
|
||||
|
||||
async updateTrayMenu(): Promise<boolean> {
|
||||
@@ -48,9 +48,9 @@ export const providersApi = {
|
||||
|
||||
async updateSortOrder(
|
||||
updates: ProviderSortUpdate[],
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
): Promise<boolean> {
|
||||
return await invoke("update_providers_sort_order", { updates, app: appType });
|
||||
return await invoke("update_providers_sort_order", { updates, app: appId });
|
||||
},
|
||||
|
||||
async onSwitched(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { Settings } from "@/types";
|
||||
import type { AppType } from "./types";
|
||||
import type { AppId } from "./types";
|
||||
|
||||
export interface ConfigTransferResult {
|
||||
success: boolean;
|
||||
@@ -30,12 +30,12 @@ export const settingsApi = {
|
||||
return await invoke("is_portable_mode");
|
||||
},
|
||||
|
||||
async getConfigDir(appType: AppType): Promise<string> {
|
||||
return await invoke("get_config_dir", { app: appType });
|
||||
async getConfigDir(appId: AppId): Promise<string> {
|
||||
return await invoke("get_config_dir", { app: appId });
|
||||
},
|
||||
|
||||
async openConfigFolder(appType: AppType): Promise<void> {
|
||||
await invoke("open_config_folder", { app: appType });
|
||||
async openConfigFolder(appId: AppId): Promise<void> {
|
||||
await invoke("open_config_folder", { app: appId });
|
||||
},
|
||||
|
||||
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
export type AppType = "claude" | "codex";
|
||||
// 为避免与后端 Rust `AppType` 枚举语义混淆,可使用更贴近“标识符”的别名
|
||||
export type AppId = AppType;
|
||||
// 前端统一使用 AppId 作为应用标识(与后端命令参数 `app` 一致)
|
||||
export type AppId = "claude" | "codex";
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { UsageResult } from "@/types";
|
||||
import type { AppType } from "./types";
|
||||
import type { AppId } from "./types";
|
||||
|
||||
export const usageApi = {
|
||||
async query(providerId: string, appType: AppType): Promise<UsageResult> {
|
||||
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
||||
return await invoke("query_provider_usage", {
|
||||
provider_id: providerId,
|
||||
app: appType,
|
||||
app: appId,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { CustomEndpoint } from "@/types";
|
||||
import type { AppType } from "./types";
|
||||
import type { AppId } from "./types";
|
||||
|
||||
export interface EndpointLatencyResult {
|
||||
url: string;
|
||||
@@ -10,8 +10,8 @@ export interface EndpointLatencyResult {
|
||||
}
|
||||
|
||||
export const vscodeApi = {
|
||||
async getLiveProviderSettings(appType: AppType) {
|
||||
return await invoke("read_live_provider_settings", { app: appType });
|
||||
async getLiveProviderSettings(appId: AppId) {
|
||||
return await invoke("read_live_provider_settings", { app: appId });
|
||||
},
|
||||
|
||||
async testApiEndpoints(
|
||||
@@ -25,46 +25,46 @@ export const vscodeApi = {
|
||||
},
|
||||
|
||||
async getCustomEndpoints(
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
providerId: string,
|
||||
): Promise<CustomEndpoint[]> {
|
||||
return await invoke("get_custom_endpoints", {
|
||||
app: appType,
|
||||
app: appId,
|
||||
provider_id: providerId,
|
||||
});
|
||||
},
|
||||
|
||||
async addCustomEndpoint(
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
providerId: string,
|
||||
url: string,
|
||||
): Promise<void> {
|
||||
await invoke("add_custom_endpoint", {
|
||||
app: appType,
|
||||
app: appId,
|
||||
provider_id: providerId,
|
||||
url,
|
||||
});
|
||||
},
|
||||
|
||||
async removeCustomEndpoint(
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
providerId: string,
|
||||
url: string,
|
||||
): Promise<void> {
|
||||
await invoke("remove_custom_endpoint", {
|
||||
app: appType,
|
||||
app: appId,
|
||||
provider_id: providerId,
|
||||
url,
|
||||
});
|
||||
},
|
||||
|
||||
async updateEndpointLastUsed(
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
providerId: string,
|
||||
url: string,
|
||||
): Promise<void> {
|
||||
await invoke("update_endpoint_last_used", {
|
||||
app: appType,
|
||||
app: appId,
|
||||
provider_id: providerId,
|
||||
url,
|
||||
});
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import { providersApi, settingsApi, type AppType } from "@/lib/api";
|
||||
import { providersApi, settingsApi, type AppId } from "@/lib/api";
|
||||
import type { Provider, Settings } from "@/types";
|
||||
|
||||
export const useAddProviderMutation = (appType: AppType) => {
|
||||
export const useAddProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -15,11 +15,11 @@ export const useAddProviderMutation = (appType: AppType) => {
|
||||
id: crypto.randomUUID(),
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
await providersApi.add(newProvider, appType);
|
||||
await providersApi.add(newProvider, appId);
|
||||
return newProvider;
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appType] });
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
||||
await providersApi.updateTrayMenu();
|
||||
toast.success(
|
||||
t("notifications.providerAdded", {
|
||||
@@ -38,17 +38,17 @@ export const useAddProviderMutation = (appType: AppType) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateProviderMutation = (appType: AppType) => {
|
||||
export const useUpdateProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (provider: Provider) => {
|
||||
await providersApi.update(provider, appType);
|
||||
await providersApi.update(provider, appId);
|
||||
return provider;
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appType] });
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
||||
toast.success(
|
||||
t("notifications.updateSuccess", {
|
||||
defaultValue: "供应商更新成功",
|
||||
@@ -66,16 +66,16 @@ export const useUpdateProviderMutation = (appType: AppType) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteProviderMutation = (appType: AppType) => {
|
||||
export const useDeleteProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (providerId: string) => {
|
||||
await providersApi.delete(providerId, appType);
|
||||
await providersApi.delete(providerId, appId);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appType] });
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
||||
await providersApi.updateTrayMenu();
|
||||
toast.success(
|
||||
t("notifications.deleteSuccess", {
|
||||
@@ -94,21 +94,21 @@ export const useDeleteProviderMutation = (appType: AppType) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const useSwitchProviderMutation = (appType: AppType) => {
|
||||
export const useSwitchProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (providerId: string) => {
|
||||
return await providersApi.switch(providerId, appType);
|
||||
return await providersApi.switch(providerId, appId);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appType] });
|
||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
||||
await providersApi.updateTrayMenu();
|
||||
toast.success(
|
||||
t("notifications.switchSuccess", {
|
||||
defaultValue: "切换供应商成功",
|
||||
appName: t(`apps.${appType}`, { defaultValue: appType }),
|
||||
appName: t(`apps.${appId}`, { defaultValue: appId }),
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
type UseQueryResult,
|
||||
keepPreviousData,
|
||||
} from "@tanstack/react-query";
|
||||
import { providersApi, settingsApi, usageApi, type AppType } from "@/lib/api";
|
||||
import { providersApi, settingsApi, usageApi, type AppId } from "@/lib/api";
|
||||
import type { Provider, Settings, UsageResult } from "@/types";
|
||||
|
||||
const sortProviders = (
|
||||
@@ -35,33 +35,33 @@ export interface ProvidersQueryData {
|
||||
}
|
||||
|
||||
export const useProvidersQuery = (
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
): UseQueryResult<ProvidersQueryData> => {
|
||||
return useQuery({
|
||||
queryKey: ["providers", appType],
|
||||
queryKey: ["providers", appId],
|
||||
placeholderData: keepPreviousData,
|
||||
queryFn: async () => {
|
||||
let providers: Record<string, Provider> = {};
|
||||
let currentProviderId = "";
|
||||
|
||||
try {
|
||||
providers = await providersApi.getAll(appType);
|
||||
providers = await providersApi.getAll(appId);
|
||||
} catch (error) {
|
||||
console.error("获取供应商列表失败:", error);
|
||||
}
|
||||
|
||||
try {
|
||||
currentProviderId = await providersApi.getCurrent(appType);
|
||||
currentProviderId = await providersApi.getCurrent(appId);
|
||||
} catch (error) {
|
||||
console.error("获取当前供应商失败:", error);
|
||||
}
|
||||
|
||||
if (Object.keys(providers).length === 0) {
|
||||
try {
|
||||
const success = await providersApi.importDefault(appType);
|
||||
const success = await providersApi.importDefault(appId);
|
||||
if (success) {
|
||||
providers = await providersApi.getAll(appType);
|
||||
currentProviderId = await providersApi.getCurrent(appType);
|
||||
providers = await providersApi.getAll(appId);
|
||||
currentProviderId = await providersApi.getCurrent(appId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("导入默认配置失败:", error);
|
||||
@@ -85,12 +85,12 @@ export const useSettingsQuery = (): UseQueryResult<Settings> => {
|
||||
|
||||
export const useUsageQuery = (
|
||||
providerId: string,
|
||||
appType: AppType,
|
||||
appId: AppId,
|
||||
enabled: boolean = true,
|
||||
): UseQueryResult<UsageResult> => {
|
||||
return useQuery({
|
||||
queryKey: ["usage", providerId, appType],
|
||||
queryFn: async () => usageApi.query(providerId, appType),
|
||||
queryKey: ["usage", providerId, appId],
|
||||
queryFn: async () => usageApi.query(providerId, appId),
|
||||
enabled: enabled && !!providerId,
|
||||
refetchOnWindowFocus: false,
|
||||
staleTime: 5 * 60 * 1000, // 5分钟
|
||||
|
||||
Reference in New Issue
Block a user