Skip to content

Commit

Permalink
fix: linux errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prostarz committed Nov 17, 2024
1 parent f57c044 commit 78cacf1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 66 deletions.
38 changes: 14 additions & 24 deletions backend/main.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -25,55 +22,48 @@ 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());
});

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();
});
36 changes: 29 additions & 7 deletions backend/utils/window.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +35,7 @@ class Window {
preload: path.join(__dirname, "preload.mjs"),
devTools: !app.isPackaged,
},
autoHideMenuBar: true,
autoHideMenuBar: false,
minWidth: 1000,
minHeight: 600,
frame,
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
50 changes: 21 additions & 29 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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) => <ErrorComponent {...props} />,
});
};
const appRouter = createRouter({
routeTree,
history: memoryHistory,
context: {
queryClient,
},
defaultPreload: "intent",
defaultPreloadStaleTime: 0,
defaultErrorComponent: (props) => <ErrorComponent {...props} />,
});

// Register the router instance for type safety
declare module "@tanstack/react-router" {
interface Register {
router: ReturnType<typeof createAppRouter>;
router: typeof appRouter;
}
}

Expand All @@ -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 (
<ThemeProvider defaultTheme="dark" storageKey="ui-theme">
{/* {!hasLoaded && <SplashScreen />} */}
<QueryClientProvider client={queryClient}>
<Toaster />
<RouterProvider router={router} />
<RouterProvider router={appRouter} />
</QueryClientProvider>
</ThemeProvider>
);
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/lib/history.ts
Original file line number Diff line number Diff line change
@@ -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: ["/"],
});
Expand Down
3 changes: 1 addition & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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";
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";
Expand Down

0 comments on commit 78cacf1

Please sign in to comment.