From 5627116204eaeb0ff7a00b3791ada29c8cc5980e Mon Sep 17 00:00:00 2001 From: an-lee Date: Sat, 7 Sep 2024 10:49:37 +0800 Subject: [PATCH] fix reset button (#1045) --- enjoy/src/i18n/en.json | 5 +++-- enjoy/src/i18n/zh-CN.json | 1 + enjoy/src/main/db/models/user-setting.ts | 4 ++++ enjoy/src/main/window.ts | 14 ++++++++------ .../components/preferences/reset-all-button.tsx | 10 ++++++++-- .../components/preferences/reset-settings.tsx | 6 +++--- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/enjoy/src/i18n/en.json b/enjoy/src/i18n/en.json index 6c7eff66..19e4ab75 100644 --- a/enjoy/src/i18n/en.json +++ b/enjoy/src/i18n/en.json @@ -288,6 +288,7 @@ "resetAllConfirmation": "It will remove all of your personal data, are you sure?", "resetSettings": "Reset Settings", "resetSettingsConfirmation": "It will reset all of your settings, are you sure? The library will not be affected.", + "settingsReseted": "Settings reseted", "proxySettings": "Proxy Settings", "proxyConfigUpdated": "Proxy config updated", "apiSettings": "API Settings", @@ -757,8 +758,8 @@ "import": "Import", "default": "Default", "setDefault": "Set Default", - "dictSettings": "Dict Settings", - "dictSettingsShort": "Dict Settings", + "dictSettings": "Dictionaries Settings", + "dictSettingsShort": "Dictionaries", "importDict": "Import Dictionary", "dictRemoved": "Dictionary removed successfully", "interrupted": "Interrupted", diff --git a/enjoy/src/i18n/zh-CN.json b/enjoy/src/i18n/zh-CN.json index c120cfca..e803e698 100644 --- a/enjoy/src/i18n/zh-CN.json +++ b/enjoy/src/i18n/zh-CN.json @@ -288,6 +288,7 @@ "resetAllConfirmation": "这将删除您的所有个人数据, 您确定要重置吗?", "resetSettings": "重置设置选项", "resetSettingsConfirmation": "您确定要重置个人设置选项吗?资料库不会受影响。", + "settingsReseted": "设置选项已重置", "proxySettings": "代理设置", "proxyConfigUpdated": "代理配置已更新", "apiSettings": "API 设置", diff --git a/enjoy/src/main/db/models/user-setting.ts b/enjoy/src/main/db/models/user-setting.ts index fb451f0f..795485cc 100644 --- a/enjoy/src/main/db/models/user-setting.ts +++ b/enjoy/src/main/db/models/user-setting.ts @@ -68,6 +68,10 @@ export class UserSetting extends Model { } } + static async clear(): Promise { + await UserSetting.destroy({ where: {} }); + } + static async migrateFromSettings(): Promise { // hotkeys const hotkeys = await UserSetting.get(UserSettingKeyEnum.HOTKEYS); diff --git a/enjoy/src/main/window.ts b/enjoy/src/main/window.ts index 5647e257..4c86247a 100644 --- a/enjoy/src/main/window.ts +++ b/enjoy/src/main/window.ts @@ -24,6 +24,7 @@ import echogarden from "./echogarden"; import camdict from "./camdict"; import dict from "./dict"; import decompresser from "./decompresser"; +import { UserSetting } from "@main/db/models"; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -292,8 +293,12 @@ main.init = async () => { }; }); - ipcMain.handle("app-reset", () => { - fs.removeSync(settings.userDataPath()); + ipcMain.handle("app-reset", async () => { + const userDataPath = settings.userDataPath(); + + await db.disconnect(); + + fs.removeSync(userDataPath); fs.removeSync(settings.file()); app.relaunch(); @@ -301,10 +306,7 @@ main.init = async () => { }); ipcMain.handle("app-reset-settings", () => { - fs.removeSync(settings.file()); - - app.relaunch(); - app.exit(); + UserSetting.clear(); }); ipcMain.handle("app-relaunch", () => { diff --git a/enjoy/src/renderer/components/preferences/reset-all-button.tsx b/enjoy/src/renderer/components/preferences/reset-all-button.tsx index ef2c3315..0187adc2 100644 --- a/enjoy/src/renderer/components/preferences/reset-all-button.tsx +++ b/enjoy/src/renderer/components/preferences/reset-all-button.tsx @@ -8,6 +8,7 @@ import { AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, + toast, } from "@renderer/components/ui"; import { useContext } from "react"; import { AppSettingsProviderContext } from "../../context"; @@ -48,8 +49,13 @@ export const ResetAllButton = (props: { children: React.ReactNode }) => { export const ResetSettingsButton = (props: { children: React.ReactNode }) => { const { EnjoyApp } = useContext(AppSettingsProviderContext); - const reset = () => { - EnjoyApp.app.resetSettings(); + const reset = async () => { + try { + await EnjoyApp.app.resetSettings(); + toast.success(t("settingsReseted")); + } catch (error) { + toast.error(error.message); + } }; return ( diff --git a/enjoy/src/renderer/components/preferences/reset-settings.tsx b/enjoy/src/renderer/components/preferences/reset-settings.tsx index 621b08c6..08866866 100644 --- a/enjoy/src/renderer/components/preferences/reset-settings.tsx +++ b/enjoy/src/renderer/components/preferences/reset-settings.tsx @@ -1,6 +1,6 @@ import { t } from "i18next"; import { Button } from "@renderer/components/ui"; -import { ResetAllButton } from "@renderer/components"; +import { ResetSettingsButton } from "@renderer/components"; import { InfoIcon } from "lucide-react"; export const ResetSettings = () => { @@ -15,11 +15,11 @@ export const ResetSettings = () => {
- + - +