Skip to content

Commit

Permalink
refactor: 优化获取路径的相关代码 (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored and kohaiy committed Sep 20, 2024
1 parent 51ace40 commit 3a885da
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 63 deletions.
45 changes: 23 additions & 22 deletions src/database/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,15 @@ import Database from "tauri-plugin-sql-api";

let db: Database | null;

/**
* 处理参数
* @param payload 数据
*/
const handlePayload = (payload: TablePayload) => {
const omitPayload = omitBy(payload, isNil);

const keys = map(Object.keys(omitPayload), (key) => `[${key}]`);
const refs = map(keys, () => "?");
const values = map(Object.values(omitPayload), (item) => {
return isBoolean(item) ? Number(item) : item;
});

return {
keys,
refs,
values,
};
};

/**
* 初始化数据库
*/
export const initDatabase = async () => {
const appName = await getName();
const ext = isDev() ? "dev.db" : "db";
const path = joinPath(getSaveDataDir(), `${appName}.${ext}`);

db = await Database.load(`sqlite:${getSaveDataDir()}${appName}.${ext}`);
db = await Database.load(`sqlite:${path}`);

await executeSQL(
`
Expand Down Expand Up @@ -154,7 +135,7 @@ export const deleteSQL = async (tableName: TableName, id?: number) => {

if (type !== "image") return;

removeFile(getSaveImageDir() + value);
removeFile(getSaveImagePath(value));
};

if (id) {
Expand All @@ -178,3 +159,23 @@ export const closeDatabase = async () => {

db = null;
};

/**
* 处理参数
* @param payload 数据
*/
const handlePayload = (payload: TablePayload) => {
const omitPayload = omitBy(payload, isNil);

const keys = map(Object.keys(omitPayload), (key) => `[${key}]`);
const refs = map(keys, () => "?");
const values = map(Object.values(omitPayload), (item) => {
return isBoolean(item) ? Number(item) : item;
});

return {
keys,
refs,
values,
};
};
23 changes: 16 additions & 7 deletions src/pages/Backup/components/SavePath/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ 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 { dataDir, join } from "@tauri-apps/api/path";
import { dataDir } from "@tauri-apps/api/path";
import { Button, Flex, Space, Tooltip, message } from "antd";
import { isString } from "antd/es/button";
import { isEqual } from "lodash-es";
import type { FC } from "react";
import type { State } from "../..";

const SavePath: FC<{ state: State }> = (props) => {
const { state } = props;
const { t } = useTranslation();
const [defaultDir, setDefaultDir] = useState("");

useMount(async () => {
setDefaultDir(await dataDir());
});

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

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

state.spinning = true;

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

if (!dirName) return;

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

state.spinning = false;

Expand All @@ -43,6 +47,10 @@ const SavePath: FC<{ state: State }> = (props) => {
}
};

const isEqualPath = (nextDir = defaultDir) => {
return isEqual(joinPath(nextDir, getSaveDataDirName()), getSaveDataDir());
};

return (
<ProList header={t("preference.data_backup.storage_path.title")}>
<ProListItem
Expand All @@ -55,7 +63,7 @@ const SavePath: FC<{ state: State }> = (props) => {
previewPath(getSaveDataDir());
}}
>
{getSaveDataDir(false)}
{getSaveDataDir()}
</span>
</Flex>
}
Expand All @@ -74,6 +82,7 @@ const SavePath: FC<{ state: State }> = (props) => {
title={t("preference.data_backup.storage_path.hints.default_path")}
>
<Button
disabled={isEqualPath()}
icon={<ReloadOutlined className="text-14!" />}
onClick={() => handleChange(true)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Clipboard/Panel/components/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const List = () => {
const data = state.data.list[index];
let { type, value } = data;

value = type !== "image" ? value : getSaveImageDir() + value;
value = type !== "image" ? value : getSaveImagePath(value);

return (
<Item
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/tray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { invoke } from "@tauri-apps/api";

/**
* 设置图片的可见性
* 设置托盘的可见性
*/
export const setTrayVisible = (visible: boolean) => {
invoke(TRAY_PLUGIN.SET_TRAY_VISIBLE, {
Expand Down
59 changes: 59 additions & 0 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { sep } from "@tauri-apps/api/path";
import { last } from "lodash-es";

/**
* 连接相应系统的路径
* @param paths 路径数组
*/
export const joinPath = (...paths: string[]) => {
const joinPaths = paths.map((path) => {
if (path.endsWith(sep)) {
return path.slice(0, -1);
}

return path;
});

return joinPaths.join(sep);
};

/**
* 获取存储数据的目录
* @param endWithSep 结尾是否包含分隔符
*/
export const getSaveDataDir = () => {
let { saveDataDir = "" } = globalStore.env;

saveDataDir = joinPath(saveDataDir, "");

return saveDataDir.slice(0, -1);
};

/**
* 获取存储图片的目录
*/
export const getSaveImageDir = () => {
return joinPath(getSaveDataDir(), "images", "");
};

/**
* 获取图片内容的存储路径
* @param file 文件名
*/
export const getSaveImagePath = (fileName: string) => {
return joinPath(getSaveImageDir(), fileName);
};

/**
* 备份数据时全局数据的存储路径
*/
export const getBackupStorePath = () => {
return joinPath(getSaveDataDir(), "backup-store");
};

/**
* 存储数据的目录名
*/
export const getSaveDataDirName = () => {
return last(getSaveDataDir().split(sep)) as string;
};
32 changes: 0 additions & 32 deletions src/utils/shared.ts

This file was deleted.

0 comments on commit 3a885da

Please sign in to comment.