From b54c767d75774990d63a7259b04d564efebc009d Mon Sep 17 00:00:00 2001 From: haritabh-z01 Date: Wed, 10 Sep 2025 17:22:38 +0530 Subject: [PATCH] chore: add reset app button in advanced settings --- apps/desktop/src/db/index.ts | 2 +- .../main/pages/settings/advanced/index.tsx | 106 ++++++++++++++++-- apps/desktop/src/trpc/routers/settings.ts | 38 +++++++ 3 files changed, 135 insertions(+), 11 deletions(-) diff --git a/apps/desktop/src/db/index.ts b/apps/desktop/src/db/index.ts index f821b11..ff66bbd 100644 --- a/apps/desktop/src/db/index.ts +++ b/apps/desktop/src/db/index.ts @@ -6,7 +6,7 @@ import * as fs from "fs"; import * as schema from "./schema"; // Get the user data directory for storing the database -const dbPath = app.isPackaged +export const dbPath = app.isPackaged ? path.join(app.getPath("userData"), "amical.db") : path.join(process.cwd(), "amical.db"); diff --git a/apps/desktop/src/renderer/main/pages/settings/advanced/index.tsx b/apps/desktop/src/renderer/main/pages/settings/advanced/index.tsx index 6642f2a..fc44077 100644 --- a/apps/desktop/src/renderer/main/pages/settings/advanced/index.tsx +++ b/apps/desktop/src/renderer/main/pages/settings/advanced/index.tsx @@ -6,17 +6,31 @@ import { CardDescription, CardContent, } from "@/components/ui/card"; -import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog"; import { api } from "@/trpc/react"; import { toast } from "sonner"; export default function AdvancedSettingsPage() { const [preloadWhisperModel, setPreloadWhisperModel] = useState(true); + const [isResetting, setIsResetting] = useState(false); // tRPC queries and mutations const settingsQuery = api.settings.getSettings.useQuery(); + const dataPathQuery = api.settings.getDataPath.useQuery(); const utils = api.useUtils(); const updateTranscriptionSettingsMutation = @@ -31,6 +45,21 @@ export default function AdvancedSettingsPage() { }, }); + const resetAppMutation = api.settings.resetApp.useMutation({ + onMutate: () => { + setIsResetting(true); + toast.info("Resetting app..."); + }, + onSuccess: () => { + toast.success("App reset successfully. Restarting..."); + }, + onError: (error) => { + setIsResetting(false); + console.error("Failed to reset app:", error); + toast.error("Failed to reset app. Please try again."); + }, + }); + // Load settings when query data is available useEffect(() => { if (settingsQuery.data?.transcription) { @@ -97,15 +126,72 @@ export default function AdvancedSettingsPage() {
-
- - + +
+ + + + + + Danger Zone + + Actions here are irreversible and will delete all your data + + + +
+
+
+ +

+ Delete all data and start fresh +

+
+ + + + + + + + Are you absolutely sure? + + + This action cannot be undone. This will permanently + delete: +
    +
  • All your transcriptions
  • +
  • All your notes
  • +
  • Your vocabulary
  • +
  • All settings and preferences
  • +
  • Downloaded models
  • +
+
+ The app will restart with a fresh installation. +
+
+ + Cancel + resetAppMutation.mutate()} + > + Yes, delete everything + + +
+
diff --git a/apps/desktop/src/trpc/routers/settings.ts b/apps/desktop/src/trpc/routers/settings.ts index 552355b..50a12e1 100644 --- a/apps/desktop/src/trpc/routers/settings.ts +++ b/apps/desktop/src/trpc/routers/settings.ts @@ -2,6 +2,8 @@ import { observable } from "@trpc/server/observable"; import { z } from "zod"; import { app } from "electron"; import { createRouter, procedure } from "../trpc"; +import { dbPath } from "../../db"; +import * as fs from "fs/promises"; // FormatterConfig schema const FormatterConfigSchema = z.object({ @@ -504,5 +506,41 @@ export const settingsRouter = createRouter({ throw error; } }), + + // Get data path + getDataPath: procedure.query(() => { + return app.getPath("userData"); + }), + + // Reset app - deletes all data and restarts + resetApp: procedure.mutation(async ({ ctx }) => { + try { + const logger = ctx.serviceManager.getLogger(); + if (logger) { + logger.main.info("Resetting app - deleting all data"); + } + + // Delete the database file + await fs.unlink(dbPath); + + // Handle restart differently in development vs production + if (process.env.NODE_ENV === "development" || !app.isPackaged) { + //! restarting will not work properly in dev mode + app.quit(); + } else { + // Production mode: relaunch the app + app.relaunch(); + app.quit(); + } + + return { success: true }; + } catch (error) { + const logger = ctx.serviceManager.getLogger(); + if (logger) { + logger.main.error("Error resetting app:", error); + } + throw new Error("Failed to reset app"); + } + }), }); // This comment prevents prettier from removing the trailing newline