Skip to content

Commit

Permalink
feat: 新增 历史记录 > 最大保留条数 配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Dec 26, 2024
1 parent a4535d0 commit 8bcf090
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 99 deletions.
9 changes: 4 additions & 5 deletions src/components/UpdateApp/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Interval } from "@/types/shared";
import { relaunch } from "@tauri-apps/plugin-process";
import { type Update, check } from "@tauri-apps/plugin-updater";
import type { Timeout } from "ahooks/lib/useRequest/src/types";
import { Flex, Modal, Typography, message } from "antd";
import clsx from "clsx";
import Markdown from "react-markdown";
Expand All @@ -17,22 +17,21 @@ interface State {
download: number;
}

let timer: Timeout;

const UpdateApp = () => {
const { t } = useTranslation();
const timerRef = useRef<Interval>();
const state = useReactive<State>({ download: 0 });
const [messageApi, contextHolder] = message.useMessage();

// 监听自动更新配置变化
useImmediateKey(globalStore.update, "auto", (value) => {
clearInterval(timer);
clearInterval(timerRef.current);

if (!value) return;

checkUpdate();

timer = setInterval(checkUpdate, 1000 * 60 * 60 * 24);
timerRef.current = setInterval(checkUpdate, 1000 * 60 * 60 * 24);
});

// 监听参与测试版本配置变化
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useImmediate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { noop } from "lodash-es";
import { subscribe } from "valtio";

export const useImmediate = (...args: Parameters<typeof subscribe>) => {
const state = useReactive({
unsubscribe: noop,
});

useMount(async () => {
const [, callback] = args;

callback([]);

state.unsubscribe = subscribe(...args);
});

useUnmount(state.unsubscribe);
};
8 changes: 2 additions & 6 deletions src/layouts/Preference/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ const PreferenceLayout = () => {
});

// 监听全局配置项变化
useSubscribe(globalStore, () => {
handleStoreChanged();
});
useSubscribe(globalStore, () => handleStoreChanged());

// 监听剪贴板配置项变化
useSubscribe(clipboardStore, () => {
handleStoreChanged();
});
useSubscribe(clipboardStore, () => handleStoreChanged());

// 监听快捷键切换窗口显隐
useRegister(toggleWindowVisible, [shortcut.preference]);
Expand Down
12 changes: 5 additions & 7 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@
},
"label": {
"duration": "Retention Duration",
"duration_unit": {
"day": "Day",
"week": "Week",
"month": "Month",
"year": "Year"
},
"duration_unit": "Day",
"max_count": "Max Count",
"max_count_unit": "Items",
"delete_title": "Delete History",
"time_range": "Time Range",
"time_range_opt": {
Expand All @@ -110,7 +107,8 @@
"include_favorite": "Delete Favorites"
},
"hints": {
"duration": "Enter 0 to keep permanently, favorites will be retained when auto-deleting",
"duration": "Enter 0 for permanent retention, only counting and deleting uncollected entries",
"max_count": "Enter 0 for permanent retention, only counting and deleting uncollected entries",
"delete_success": "Successfully Deleted"
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@
},
"label": {
"duration": "保持期間",
"duration_unit": {
"day": "",
"week": "",
"month": "",
"year": ""
},
"duration_unit": "",
"max_count": "最大保持エントリー数",
"max_count_unit": "",
"delete_title": "履歴を削除",
"time_range": "時間範囲",
"time_range_opt": {
Expand All @@ -110,7 +107,8 @@
"include_favorite": "お気に入りも削除"
},
"hints": {
"duration": "0 を入力すると永久保存され、削除時にお気に入りが保持されます",
"duration": "0を入力すると永久に保持して、収蔵されていない項目のみを統計して削除します",
"max_count": "0を入力すると永久に保持して、収蔵されていない項目のみを統計して削除します",
"delete_success": "削除が成功しました"
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@
},
"label": {
"duration": "保留时长",
"duration_unit": {
"day": "",
"week": "",
"month": "",
"year": ""
},
"duration_unit": "",
"max_count": "最大保留条数",
"max_count_unit": "",
"delete_title": "删除历史记录",
"time_range": "时间范围",
"time_range_opt": {
Expand All @@ -110,7 +107,8 @@
"include_favorite": "删除收藏"
},
"hints": {
"duration": "输入 0 则表示永久保留,自动删除时会保留收藏",
"duration": "输入 0 表示永久保留,仅统计和删除未收藏条目",
"max_count": "输入 0 表示永久保留,仅统计和删除未收藏条目",
"delete_success": "删除成功"
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@
},
"label": {
"duration": "保留時長",
"duration_unit": {
"day": "",
"week": "",
"month": "",
"year": ""
},
"duration_unit": "",
"max_count": "最大保留條數",
"max_count_unit": "",
"delete_title": "刪除歷史記錄",
"time_range": "時間範圍",
"time_range_opt": {
Expand All @@ -110,7 +107,8 @@
"include_favorite": "刪除收藏"
},
"hints": {
"duration": "輸入 0 表示永久保留,收藏在自動刪除時將保留",
"duration": "輸入 0 表示永久保留,僅統計和删除未收藏條目",
"max_count": "輸入 0 表示永久保留,僅統計和删除未收藏條目",
"delete_success": "刪除成功"
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/pages/History/components/Duration/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ProListItem from "@/components/ProListItem";
import { InputNumber } from "antd";
import { useSnapshot } from "valtio";

const Duration = () => {
const { history } = useSnapshot(clipboardStore);
const { t } = useTranslation();

return (
<ProListItem
title={t("preference.history.history.label.duration")}
description={t("preference.history.history.hints.duration")}
>
<InputNumber
value={history.duration}
min={0}
addonAfter={t("preference.history.history.label.duration_unit")}
className="w-120"
onChange={(value) => {
clipboardStore.history.duration = value ?? 0;
}}
/>
</ProListItem>
);
};

export default Duration;
27 changes: 27 additions & 0 deletions src/pages/History/components/MaxCount/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ProListItem from "@/components/ProListItem";
import { InputNumber } from "antd";
import { useSnapshot } from "valtio";

const MaxCount = () => {
const { history } = useSnapshot(clipboardStore);
const { t } = useTranslation();

return (
<ProListItem
title={t("preference.history.history.label.max_count")}
description={t("preference.history.history.hints.max_count")}
>
<InputNumber
value={history.maxCount}
min={0}
addonAfter={t("preference.history.history.label.max_count_unit")}
className="w-120"
onChange={(value) => {
clipboardStore.history.maxCount = value ?? 0;
}}
/>
</ProListItem>
);
};

export default MaxCount;
87 changes: 27 additions & 60 deletions src/pages/History/index.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,46 @@
import EcoSelect from "@/components/EcoSelect";
import ProList from "@/components/ProList";
import ProListItem from "@/components/ProListItem";
import type { HistoryTablePayload } from "@/types/database";
import { InputNumber } from "antd";
import { useSnapshot } from "valtio";
import type { Interval } from "@/types/shared";
import Delete from "./components/Delete";
import Duration from "./components/Duration";
import MaxCount from "./components/MaxCount";

const History = () => {
const { history } = useSnapshot(clipboardStore);
const { t } = useTranslation();
const timerRef = useRef<Interval>();

useInterval(
async () => {
const { duration, unit } = clipboardStore.history;
useImmediate(clipboardStore.history, async () => {
const { duration, maxCount } = clipboardStore.history;

if (duration === 0) return;
clearInterval(timerRef.current);

const list = await selectSQL<HistoryTablePayload[]>("history");
if (duration === 0 && maxCount === 0) return;

for (const item of list) {
const { createTime, favorite } = item;
const delay = 1000 * 60 * 30;

if (dayjs().diff(createTime, "days") >= duration * unit) {
if (favorite) continue;
timerRef.current = setInterval(async () => {
const list = await selectSQL<HistoryTablePayload[]>("history", {
favorite: false,
});

deleteSQL("history", item);
}
}
},
1000 * 60 * 30,
);
for (const [index, item] of list.entries()) {
const { createTime } = item;
const diffDays = dayjs().diff(createTime, "days");
const isExpired = duration > 0 && diffDays >= duration;
const isOverMaxCount = maxCount > 0 && index >= maxCount;

const unitOptions = [
{
label: t("preference.history.history.label.duration_unit.day"),
value: 1,
},
{
label: t("preference.history.history.label.duration_unit.week"),
value: 7,
},
{
label: t("preference.history.history.label.duration_unit.month"),
value: 30,
},
{
label: t("preference.history.history.label.duration_unit.year"),
value: 365,
},
];
if (!isExpired && !isOverMaxCount) continue;

deleteSQL("history", item);
}
}, delay);
});

return (
<ProList header={t("preference.history.history.title")} footer={<Delete />}>
<ProListItem
title={t("preference.history.history.label.duration")}
description={t("preference.history.history.hints.duration")}
>
<InputNumber
min={0}
className="w-130"
value={history.duration}
addonAfter={
<EcoSelect
value={history.unit}
options={unitOptions}
onChange={(value) => {
clipboardStore.history.unit = value ?? 0;
}}
/>
}
onChange={(value) => {
clipboardStore.history.duration = value ?? 0;
}}
/>
</ProListItem>
<Duration />

<MaxCount />
</ProList>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/stores/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export const clipboardStore = proxy<ClipboardStore>({
history: {
duration: 0,
unit: 1,
maxCount: 0,
},
});
3 changes: 3 additions & 0 deletions src/types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Interval = ReturnType<typeof setInterval>;

export type Timeout = ReturnType<typeof setTimeout>;
1 change: 1 addition & 0 deletions src/types/store.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ export interface ClipboardStore {
history: {
duration: number;
unit: number;
maxCount: number;
};
}
9 changes: 9 additions & 0 deletions src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { omit } from "lodash-es";
* 初始化配置项
*/
const initStore = async () => {
const { unit, duration } = clipboardStore.history;

if (unit !== 1) {
deepAssign(clipboardStore.history, {
unit: 1,
duration: duration * unit,
});
}

globalStore.appearance.language ??= await getLocale();
globalStore.env.platform = await type();
globalStore.env.appName = await getName();
Expand Down

0 comments on commit 8bcf090

Please sign in to comment.