From 78cacf1f8a24ce40995f5171c070831f5f635f92 Mon Sep 17 00:00:00 2001 From: prostarz Date: Sun, 17 Nov 2024 03:40:53 +0000 Subject: [PATCH] fix: linux errors --- backend/main.ts | 38 +++++++++++------------------- backend/utils/window.ts | 36 +++++++++++++++++++++++------ src/App.tsx | 50 +++++++++++++++++----------------------- src/hooks/useSettings.ts | 6 ++--- src/lib/history.ts | 1 - vite.config.ts | 3 +-- 6 files changed, 68 insertions(+), 66 deletions(-) diff --git a/backend/main.ts b/backend/main.ts index 115c6fb..07fc90d 100644 --- a/backend/main.ts +++ b/backend/main.ts @@ -1,11 +1,8 @@ import { app, BrowserWindow, net, protocol } from "electron"; import path from "node:path"; -import url, { fileURLToPath } from "node:url"; +import url from "node:url"; import window from "./utils/window"; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); -export let win: BrowserWindow | null = window.window; - // DEEP LINKING const deepLinkName = "falkor"; if (process.defaultApp) { @@ -25,16 +22,19 @@ if (!gotTheLock) { } else { app.on("second-instance", (_event, commandLine, _workingDirectory) => { // Someone tried to run a second instance, we should focus our window. - if (win) { - if (win.isMinimized()) win.restore(); - win.focus(); + if (window?.window) { + if (window?.window?.isMinimized()) window?.window?.restore(); + window?.window?.focus(); } - win?.webContents.send("app:deep-link", commandLine?.pop()?.slice(0)); + window?.window?.webContents?.send( + "app:deep-link", + commandLine?.pop()?.slice(0) + ); }); app.whenReady().then(async () => { - win = window.createWindow(); + window?.createWindow(); protocol.handle("local", (request) => { const filePath = request.url.slice("local:".length); return net.fetch(url.pathToFileURL(decodeURI(filePath)).toString()); @@ -42,38 +42,28 @@ if (!gotTheLock) { await import("./handlers/events"); - while (!win) { + while (!window?.window) { await new Promise((resolve) => setTimeout(resolve, 600)); } - win.webContents.once("did-finish-load", () => { + window?.window?.webContents?.once("did-finish-load", () => { setTimeout(() => { - win?.webContents.send("app:backend-loaded"); + window?.window?.webContents?.send("app:backend-loaded"); }, 1000); }); }); } -process.env.APP_ROOT = path.join(__dirname, ".."); - -export const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"]; -export const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron"); -export const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist"); - -process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL - ? path.join(process.env.APP_ROOT, "public") - : RENDERER_DIST; - // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on("window-all-closed", () => { if (process.platform === "darwin") return; app.quit(); - win = null; + window.destroy; }); app.on("activate", () => { if (BrowserWindow.getAllWindows().length <= 0) return; - win = window.createWindow(); + window.createWindow(); }); diff --git a/backend/utils/window.ts b/backend/utils/window.ts index 93aa64c..d65bb50 100644 --- a/backend/utils/window.ts +++ b/backend/utils/window.ts @@ -1,7 +1,19 @@ import { app, BrowserWindow, Menu, nativeImage, screen, Tray } from "electron"; import path from "node:path"; -import { __dirname, RENDERER_DIST, VITE_DEV_SERVER_URL } from "../main"; -import { settings } from "./settings/settings"; +import { fileURLToPath } from "node:url"; +import { settings } from "../utils/settings/settings"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +process.env.APP_ROOT = path.join(__dirname, ".."); + +const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"]; +export const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron"); +const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist"); + +process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL + ? path.join(process.env.APP_ROOT, "public") + : RENDERER_DIST; class Window { window: BrowserWindow | null = null; @@ -23,7 +35,7 @@ class Window { preload: path.join(__dirname, "preload.mjs"), devTools: !app.isPackaged, }, - autoHideMenuBar: true, + autoHideMenuBar: false, minWidth: 1000, minHeight: 600, frame, @@ -32,23 +44,26 @@ class Window { resizable: true, }); + win.webContents.openDevTools(); + const loadURL = - VITE_DEV_SERVER_URL || path.join(RENDERER_DIST, "index.html"); + VITE_DEV_SERVER_URL || `file://${path.join(RENDERER_DIST, "index.html")}`; win.loadURL(loadURL); if (app.isPackaged) { win.setMenu(null); } - if (!this.tray) this.createTray(); + if (!this?.tray) this.createTray(); this.window = win; return win; } createTray() { - const trayIconPath = path.join(process.env.VITE_PUBLIC, "icon.png"); - const tray = new Tray(nativeImage.createFromPath(trayIconPath)); + const tray = new Tray( + nativeImage.createFromPath(path.join(process.env.VITE_PUBLIC, "icon.png")) + ); tray.setToolTip("Falkor"); tray.setContextMenu(this.createContextMenu()); @@ -77,6 +92,13 @@ class Window { return contextMenu; } + + destroy() { + this.window?.destroy(); + this.window = null; + this.tray?.destroy(); + this.tray = null; + } } const window = new Window(); diff --git a/src/App.tsx b/src/App.tsx index 8625009..23fdfdc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,45 +1,39 @@ import { Toaster } from "@/components/ui/sonner"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { createRouter, RouterProvider } from "@tanstack/react-router"; -import { useMemo } from "react"; import ErrorComponent from "./components/errorComponent"; import { ThemeProvider } from "./components/theme-provider"; import { useThemes } from "./hooks/useThemes"; import { memoryHistory } from "./lib/history"; import { routeTree } from "./routeTree.gen"; -// Create a new query client instance -const createQueryClient = () => { - return new QueryClient({ - defaultOptions: { - queries: { - refetchInterval: false, - refetchOnMount: false, - refetchOnWindowFocus: false, - refetchIntervalInBackground: false, - }, +const queryClient = new QueryClient({ + defaultOptions: { + queries: { + refetchInterval: false, + refetchOnMount: false, + refetchOnWindowFocus: false, + refetchIntervalInBackground: false, }, - }); -}; + }, +}); // Create the router instance -const createAppRouter = (queryClient: QueryClient) => { - return createRouter({ - routeTree, - history: memoryHistory, // Use global memory history - context: { - queryClient, - }, - defaultPreload: "intent", - defaultPreloadStaleTime: 0, - defaultErrorComponent: (props) => , - }); -}; +const appRouter = createRouter({ + routeTree, + history: memoryHistory, + context: { + queryClient, + }, + defaultPreload: "intent", + defaultPreloadStaleTime: 0, + defaultErrorComponent: (props) => , +}); // Register the router instance for type safety declare module "@tanstack/react-router" { interface Register { - router: ReturnType; + router: typeof appRouter; } } @@ -48,15 +42,13 @@ function App() { useThemes(); // Memoize queryClient and router to avoid recreating them on each render - const queryClient = useMemo(createQueryClient, []); - const router = useMemo(() => createAppRouter(queryClient), [queryClient]); return ( {/* {!hasLoaded && } */} - + ); diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.ts index 756dc14..0a7fbf2 100644 --- a/src/hooks/useSettings.ts +++ b/src/hooks/useSettings.ts @@ -5,10 +5,10 @@ export const useSettings = () => { const store = useSettingsStore(); useEffect(() => { - if (store.hasDoneFirstFetch) return; + if (store?.hasDoneFirstFetch) return; - store.fetchSettings(); - store.setHasDoneFirstFetch(); + store?.fetchSettings(); + store?.setHasDoneFirstFetch(); }, [store]); return { diff --git a/src/lib/history.ts b/src/lib/history.ts index ad5a874..d2eac4f 100644 --- a/src/lib/history.ts +++ b/src/lib/history.ts @@ -1,7 +1,6 @@ // history.ts import { createMemoryHistory } from "@tanstack/react-router"; -// Export memory history globally so `goBack` can access it export const memoryHistory = createMemoryHistory({ initialEntries: ["/"], }); diff --git a/vite.config.ts b/vite.config.ts index ae7981b..afd0bf3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,5 @@ import { TanStackRouterVite } from "@tanstack/router-plugin/vite"; import react from "@vitejs/plugin-react"; -import { rmSync } from "node:fs"; import path from "node:path"; import { defineConfig } from "vite"; import electron from "vite-plugin-electron/simple"; @@ -8,7 +7,7 @@ import pkg from "./package.json"; // https://vitejs.dev/config/ export default defineConfig(({ command }) => { - rmSync("dist-electron", { recursive: true, force: true }); + // rmSync("dist-electron", { recursive: true, force: true }); const isServe = command === "serve"; const isBuild = command === "build";