refactor(types): introduce Settings and apply in API

- style(prettier): format src files
- style(rustfmt): format Rust sources
- refactor(tauri-api): type-safe getSettings/saveSettings
- refactor(d.ts): declare window.api with Settings

[skip ci]
This commit is contained in:
Jason
2025-09-07 11:36:09 +08:00
parent 77bdeb02fb
commit 02bfc97ee6
11 changed files with 210 additions and 106 deletions

View File

@@ -330,9 +330,7 @@ function App() {
)}
{isSettingsOpen && (
<SettingsModal
onClose={() => setIsSettingsOpen(false)}
/>
<SettingsModal onClose={() => setIsSettingsOpen(false)} />
)}
</div>
);

View File

@@ -100,7 +100,10 @@ const ProviderList: React.FC<ProviderListProps> = ({
{provider.websiteUrl}
</button>
) : (
<span className="text-[var(--color-text-secondary)]" title={apiUrl}>
<span
className="text-[var(--color-text-secondary)]"
title={apiUrl}
>
{apiUrl}
</span>
)}

View File

@@ -1,10 +1,7 @@
import { useState, useEffect } from "react";
import { X, Info, RefreshCw, FolderOpen } from "lucide-react";
import "../lib/tauri-api";
interface Settings {
showInDock: boolean;
}
import type { Settings } from "../types";
interface SettingsModalProps {
onClose: () => void;
@@ -36,9 +33,9 @@ export default function SettingsModal({ onClose }: SettingsModalProps) {
const loadConfigPath = async () => {
try {
const status = await window.api.getConfigStatus("claude");
if (status?.path) {
setConfigPath(status.path.replace("/claude_code_config.json", ""));
const path = await window.api.getAppConfigPath();
if (path) {
setConfigPath(path);
}
} catch (error) {
console.error("获取配置路径失败:", error);
@@ -67,7 +64,7 @@ export default function SettingsModal({ onClose }: SettingsModalProps) {
const handleOpenConfigFolder = async () => {
try {
await window.api.openConfigFolder("claude");
await window.api.openAppConfigFolder();
} catch (error) {
console.error("打开配置文件夹失败:", error);
}

View File

@@ -1,6 +1,6 @@
import { invoke } from "@tauri-apps/api/core";
import { listen, UnlistenFn } from "@tauri-apps/api/event";
import { Provider } from "../types";
import { Provider, Settings } from "../types";
// 应用类型
export type AppType = "claude" | "codex";
@@ -196,7 +196,7 @@ export const tauriAPI = {
},
// 获取设置
getSettings: async (): Promise<any> => {
getSettings: async (): Promise<Settings> => {
try {
return await invoke("get_settings");
} catch (error) {
@@ -206,7 +206,7 @@ export const tauriAPI = {
},
// 保存设置
saveSettings: async (settings: any): Promise<boolean> => {
saveSettings: async (settings: Settings): Promise<boolean> => {
try {
return await invoke("save_settings", { settings });
} catch (error) {
@@ -223,6 +223,25 @@ export const tauriAPI = {
console.error("检查更新失败:", error);
}
},
// 获取应用配置文件路径
getAppConfigPath: async (): Promise<string> => {
try {
return await invoke("get_app_config_path");
} catch (error) {
console.error("获取应用配置路径失败:", error);
return "";
}
},
// 打开应用配置文件夹
openAppConfigFolder: async (): Promise<void> => {
try {
await invoke("open_app_config_folder");
} catch (error) {
console.error("打开应用配置文件夹失败:", error);
}
},
};
// 创建全局 API 对象,兼容现有代码

View File

@@ -9,3 +9,8 @@ export interface AppConfig {
providers: Record<string, Provider>;
current: string;
}
// 应用设置类型(用于 SettingsModal 与 Tauri API
export interface Settings {
showInDock: boolean;
}

8
src/vite-env.d.ts vendored
View File

@@ -1,6 +1,6 @@
/// <reference types="vite/client" />
import { Provider } from "./types";
import { Provider, Settings } from "./types";
import { AppType } from "./lib/tauri-api";
import type { UnlistenFn } from "@tauri-apps/api/event";
@@ -35,9 +35,11 @@ declare global {
onProviderSwitched: (
callback: (data: { appType: string; providerId: string }) => void,
) => Promise<UnlistenFn>;
getSettings: () => Promise<any>;
saveSettings: (settings: any) => Promise<boolean>;
getSettings: () => Promise<Settings>;
saveSettings: (settings: Settings) => Promise<boolean>;
checkForUpdates: () => Promise<void>;
getAppConfigPath: () => Promise<string>;
openAppConfigFolder: () => Promise<void>;
};
platform: {
isMac: boolean;