spa5k / nextjs_approuter_electron

This is a template for building Electron apps with Next.js App router, SSR and Server Components
https://www.saybackend.com/blog/03-electron-nextjs-ssr
60 stars 8 forks source link

Cannot Use other Electron Native Code #9

Closed Revaycolizer closed 1 week ago

Revaycolizer commented 1 week ago

I exposed openDirectory on Main and preload.ts but my frontend is complain that ts: Property 'openDirectory' does not exist on type '{ ipcRenderer: { send: (channel: string, ...args: any[]) => void; }; }'.

Preload.ts

import { contextBridge, ipcRenderer } from "electron";

contextBridge.exposeInMainWorld("electron", {
  ipcRenderer: {
    send: (channel: string, data: any) => ipcRenderer.send(channel, data),
    on: (channel: string, listener: (event: any, ...args: any[]) => void) =>
      ipcRenderer.on(channel, listener),
  },
  openDirectory: () => ipcRenderer.invoke("dialog:openDirectory"),
});

Main.ts


  ipcMain.on("ping", () => console.log("pong"));
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

ipcMain.handle("dialog:openDirectory", async () => {
  const result = await dialog.showOpenDialog({
    properties: ["openDirectory"],
  });
  if (result.canceled) {
    return { canceled: true };
  }
  return { canceled: false, filePath: result.filePaths[0] };
});
spa5k commented 1 week ago

Thats typescript issue, you can fix it by adding a .d.ts file

Revaycolizer commented 1 week ago

Thats typescript issue, you can fix it by adding a .d.ts file

Thanks it worked I figured out there were two electron.d.ts that's why it was not working