Skip to content

Commit

Permalink
feat: 支持恢复默认的数据存储路径 (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Sep 13, 2024
1 parent 844eb86 commit a44b8b3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"valtio-persist": "^1.0.2"
},
"devDependencies": {
"@ant-design/icons": "^5.4.0",
"@biomejs/biome": "1.8.3",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { error } from "tauri-plugin-log-api";
const App = () => {
const { appearance } = useSnapshot(globalStore);

useMount(async () => {
useMount(() => {
// 处理系统主题变化
handleSystemThemeChanged();

Expand All @@ -22,9 +22,6 @@ const App = () => {
// 生成 antd 的颜色变量
generateColorVars();

// 初始化数据库
initDatabase();

// 监听语言的变化
watchKey(globalStore.appearance, "language", i18n.changeLanguage);

Expand Down
10 changes: 5 additions & 5 deletions src/database/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { removeFile } from "@tauri-apps/api/fs";
import { isBoolean, isNil, map, omitBy } from "lodash-es";
import Database from "tauri-plugin-sql-api";

let db: Database;
let db: Database | null;

/**
* 处理参数
Expand All @@ -35,8 +35,6 @@ const handlePayload = (payload: TablePayload) => {
* 初始化数据库
*/
export const initDatabase = async () => {
await closeDatabase();

const appName = await getName();
const ext = isDev() ? "dev.db" : "db";

Expand Down Expand Up @@ -70,10 +68,10 @@ export const executeSQL = async (query: string, values?: unknown[]) => {
}

if (query.startsWith("SELECT")) {
return await db.select(query, values);
return await db!.select(query, values);
}

await db.execute(query, values);
await db!.execute(query, values);
};

/**
Expand Down Expand Up @@ -177,4 +175,6 @@ export const deleteSQL = async (tableName: TableName, id?: number) => {
*/
export const closeDatabase = async () => {
await db?.close();

db = null;
};
37 changes: 27 additions & 10 deletions src/pages/Backup/components/SavePath/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import ProList from "@/components/ProList";
import ProListItem from "@/components/ProListItem";
import { NodeIndexOutlined, ReloadOutlined } from "@ant-design/icons";
import { open } from "@tauri-apps/api/dialog";
import { emit } from "@tauri-apps/api/event";
import { join, sep } from "@tauri-apps/api/path";
import { Button, Flex, message } from "antd";
import { dataDir, join } from "@tauri-apps/api/path";
import { Button, Flex, Space, Tooltip, message } from "antd";
import { isString } from "antd/es/button";
import type { FC } from "react";
import type { State } from "../..";

const SavePath: FC<{ state: State }> = (props) => {
const { state } = props;

const handleClick = async () => {
const handleChange = async (isDefault = false) => {
try {
const select = await open({ directory: true });
const nextDir = isDefault
? await dataDir()
: await open({ directory: true });

if (!isString(select)) return;
if (!isString(nextDir) || getSaveDataDir().startsWith(nextDir)) return;

state.spinning = true;

const dirName = await moveData(getSaveDataDir(), select);
const dirName = await moveData(getSaveDataDir(), nextDir);

if (!dirName) return;

globalStore.env.saveDataDir = await join(select, dirName);
globalStore.env.saveDataDir = await join(nextDir, dirName);

state.spinning = false;

Expand All @@ -42,19 +45,33 @@ const SavePath: FC<{ state: State }> = (props) => {
<ProListItem
title={
<Flex vertical align="flex-start" gap={2}>
自定义存储路径
数据存储路径
<span
className="color-3 hover:color-primary cursor-pointer break-all text-12 transition"
onMouseDown={() => {
previewPath(getSaveDataDir());
}}
>
{getSaveDataDir().replace(new RegExp(`${sep}$`, "g"), "")}
{getSaveDataDir(false)}
</span>
</Flex>
}
>
<Button onClick={handleClick}>更改</Button>
<Space.Compact>
<Tooltip title="自定义">
<Button
icon={<NodeIndexOutlined />}
onClick={() => handleChange()}
/>
</Tooltip>

<Tooltip title="恢复默认">
<Button
icon={<ReloadOutlined className="text-14!" />}
onClick={() => handleChange(true)}
/>
</Tooltip>
</Space.Compact>
</ProListItem>
</ProList>
);
Expand Down
6 changes: 1 addition & 5 deletions src/pages/Clipboard/Panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ const ClipboardPanel = () => {
getClipboardList();
});

listen(LISTEN_KEY.CHANGE_DATA_FILE, async () => {
await initDatabase();

getClipboardList();
});
listen(LISTEN_KEY.CHANGE_DATA_FILE, getClipboardList);

listen<boolean>(LISTEN_KEY.TOGGLE_LISTENING, ({ payload }) => {
if (payload) {
Expand Down
12 changes: 8 additions & 4 deletions src/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { sep } from "@tauri-apps/api/path";
/**
* 获取存储数据的目录
*/
export const getSaveDataDir = () => {
const { saveDataDir } = globalStore.env;
export const getSaveDataDir = (endWithSep = true) => {
let { saveDataDir = "" } = globalStore.env;

if (saveDataDir?.endsWith(sep)) {
if (!saveDataDir.endsWith(sep)) {
saveDataDir += sep;
}

if (endWithSep) {
return saveDataDir;
}

return saveDataDir + sep;
return saveDataDir.replace(new RegExp(`${sep}$`), "");
};

/**
Expand Down

0 comments on commit a44b8b3

Please sign in to comment.