refactor: rename global API from electronAPI to api and update references
This commit is contained in:
18
src/App.tsx
18
src/App.tsx
@@ -69,8 +69,8 @@ function App() {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadProviders = async () => {
|
const loadProviders = async () => {
|
||||||
const loadedProviders = await window.electronAPI.getProviders();
|
const loadedProviders = await window.api.getProviders();
|
||||||
const currentId = await window.electronAPI.getCurrentProvider();
|
const currentId = await window.api.getCurrentProvider();
|
||||||
setProviders(loadedProviders);
|
setProviders(loadedProviders);
|
||||||
setCurrentProviderId(currentId);
|
setCurrentProviderId(currentId);
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ function App() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const loadConfigStatus = async () => {
|
const loadConfigStatus = async () => {
|
||||||
const status = await window.electronAPI.getClaudeConfigStatus();
|
const status = await window.api.getClaudeConfigStatus();
|
||||||
setConfigStatus({ exists: Boolean(status?.exists), path: String(status?.path || "") });
|
setConfigStatus({ exists: Boolean(status?.exists), path: String(status?.path || "") });
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -95,14 +95,14 @@ function App() {
|
|||||||
...provider,
|
...provider,
|
||||||
id: generateId(),
|
id: generateId(),
|
||||||
};
|
};
|
||||||
await window.electronAPI.addProvider(newProvider);
|
await window.api.addProvider(newProvider);
|
||||||
await loadProviders();
|
await loadProviders();
|
||||||
setIsAddModalOpen(false);
|
setIsAddModalOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditProvider = async (provider: Provider) => {
|
const handleEditProvider = async (provider: Provider) => {
|
||||||
try {
|
try {
|
||||||
await window.electronAPI.updateProvider(provider);
|
await window.api.updateProvider(provider);
|
||||||
await loadProviders();
|
await loadProviders();
|
||||||
setEditingProviderId(null);
|
setEditingProviderId(null);
|
||||||
// 显示编辑成功提示
|
// 显示编辑成功提示
|
||||||
@@ -121,7 +121,7 @@ function App() {
|
|||||||
title: "删除供应商",
|
title: "删除供应商",
|
||||||
message: `确定要删除供应商 "${provider?.name}" 吗?此操作无法撤销。`,
|
message: `确定要删除供应商 "${provider?.name}" 吗?此操作无法撤销。`,
|
||||||
onConfirm: async () => {
|
onConfirm: async () => {
|
||||||
await window.electronAPI.deleteProvider(id);
|
await window.api.deleteProvider(id);
|
||||||
await loadProviders();
|
await loadProviders();
|
||||||
setConfirmDialog(null);
|
setConfirmDialog(null);
|
||||||
showNotification("供应商删除成功", "success");
|
showNotification("供应商删除成功", "success");
|
||||||
@@ -130,7 +130,7 @@ function App() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSwitchProvider = async (id: string) => {
|
const handleSwitchProvider = async (id: string) => {
|
||||||
const success = await window.electronAPI.switchProvider(id);
|
const success = await window.api.switchProvider(id);
|
||||||
if (success) {
|
if (success) {
|
||||||
setCurrentProviderId(id);
|
setCurrentProviderId(id);
|
||||||
// 显示重启提示
|
// 显示重启提示
|
||||||
@@ -147,7 +147,7 @@ function App() {
|
|||||||
// 自动导入现有配置为"default"供应商
|
// 自动导入现有配置为"default"供应商
|
||||||
const handleAutoImportDefault = async () => {
|
const handleAutoImportDefault = async () => {
|
||||||
try {
|
try {
|
||||||
const result = await window.electronAPI.importCurrentConfigAsDefault()
|
const result = await window.api.importCurrentConfigAsDefault()
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
await loadProviders()
|
await loadProviders()
|
||||||
@@ -161,7 +161,7 @@ function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleOpenConfigFolder = async () => {
|
const handleOpenConfigFolder = async () => {
|
||||||
await window.electronAPI.openConfigFolder();
|
await window.api.openConfigFolder();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
|||||||
|
|
||||||
const handleUrlClick = async (url: string) => {
|
const handleUrlClick = async (url: string) => {
|
||||||
try {
|
try {
|
||||||
await window.electronAPI.openExternal(url)
|
await window.api.openExternal(url)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('打开链接失败:', error)
|
console.error('打开链接失败:', error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ interface ImportResult {
|
|||||||
message?: string;
|
message?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tauri API 封装,保持与 Electron API 相同的接口
|
// Tauri API 封装,提供统一的全局 API 接口
|
||||||
export const tauriAPI = {
|
export const tauriAPI = {
|
||||||
// 获取所有供应商
|
// 获取所有供应商
|
||||||
getProviders: async (): Promise<Record<string, Provider>> => {
|
getProviders: async (): Promise<Record<string, Provider>> => {
|
||||||
@@ -144,9 +144,9 @@ export const tauriAPI = {
|
|||||||
|
|
||||||
// 创建全局 API 对象,兼容现有代码
|
// 创建全局 API 对象,兼容现有代码
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
// 始终绑定到 window.electronAPI,以避免环境判断失误导致未绑定而报错
|
// 绑定到 window.api,避免 Electron 命名造成误解
|
||||||
// API 内部已做 try/catch,非 Tauri 环境下也会安全返回默认值
|
// API 内部已做 try/catch,非 Tauri 环境下也会安全返回默认值
|
||||||
(window as any).electronAPI = tauriAPI;
|
(window as any).api = tauriAPI;
|
||||||
|
|
||||||
// 提供平台信息
|
// 提供平台信息
|
||||||
(window as any).platform = {
|
(window as any).platform = {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React from 'react'
|
|||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
import App from './App'
|
import App from './App'
|
||||||
import './index.css'
|
import './index.css'
|
||||||
// 导入 Tauri API(自动绑定到 window.electronAPI)
|
// 导入 Tauri API(自动绑定到 window.api)
|
||||||
import './lib/tauri-api'
|
import './lib/tauri-api'
|
||||||
|
|
||||||
// 根据平台添加 body class,便于平台特定样式
|
// 根据平台添加 body class,便于平台特定样式
|
||||||
|
|||||||
2
src/vite-env.d.ts
vendored
2
src/vite-env.d.ts
vendored
@@ -15,7 +15,7 @@ interface ConfigStatus {
|
|||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
electronAPI: {
|
api: {
|
||||||
getProviders: () => Promise<Record<string, Provider>>;
|
getProviders: () => Promise<Record<string, Provider>>;
|
||||||
getCurrentProvider: () => Promise<string>;
|
getCurrentProvider: () => Promise<string>;
|
||||||
addProvider: (provider: Provider) => Promise<boolean>;
|
addProvider: (provider: Provider) => Promise<boolean>;
|
||||||
|
|||||||
Reference in New Issue
Block a user