Skip to content

Commit

Permalink
fix: optional window.electron
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 24, 2024
1 parent 48db1f1 commit f3fafff
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/preload/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ElectronAPI } from "@electron-toolkit/preload"

declare global {
interface Window {
electron: ElectronAPI
electron?: ElectronAPI
api: unknown
}
}
2 changes: 1 addition & 1 deletion src/renderer/src/components/feed-column/category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function FeedCategory({
label: "Delete Category",
click: async () => {
if (
await client.showConfirmDialog({
await client?.showConfirmDialog({
title: `Delete Category ${data.name}?`,
message: `This operation will delete your category, but the feeds it contains will be retained and grouped by website.`,
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/hooks/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const useEntryActions = ({
!entry.images?.length,
onClick: async () => {
if (!entry.url || !entry.images?.length) return
const response = await client.saveToEagle({
const response = await client?.saveToEagle({
url: entry.url,
images: entry.images,
})
Expand All @@ -174,7 +174,7 @@ export const useEntryActions = ({
action: "share",
onClick: () => {
if (!entry.url) return
client.showShareMenu(entry.url)
client?.showShareMenu(entry.url)
},
},
],
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createClient } from "@egoist/tipc/renderer"
import type { Router } from "@main/tipc"

export const client = createClient<Router>({
ipcInvoke: window.electron.ipcRenderer.invoke,
})
export const client = window.electron
? createClient<Router>({
ipcInvoke: window.electron.ipcRenderer.invoke,
})
: null
10 changes: 5 additions & 5 deletions src/renderer/src/lib/native-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const showNativeMenu = async (
type: "text" as const,
label: "Inspect Element",
click: () => {
client.inspectElement({
client?.inspectElement({
x: e.pageX,
y: e.pageY,
})
Expand All @@ -33,21 +33,21 @@ export const showNativeMenu = async (
el.setAttribute("data-context-menu-open", "true")
}

const unlisten = window.electron.ipcRenderer.on("menu-click", (_, index) => {
const unlisten = window.electron?.ipcRenderer.on("menu-click", (_, index) => {
const item = items[index]
if (item && item.type === "text") {
item.click()
}
})

window.electron.ipcRenderer.once("menu-closed", () => {
unlisten()
window.electron?.ipcRenderer.once("menu-closed", () => {
unlisten?.()
if (el instanceof HTMLElement) {
el.removeAttribute("data-context-menu-open")
}
})

await client.showContextMenu({
await client?.showContextMenu({
items: items.map((item) => {
if (item.type === "text") {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/pages/debug.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Versions from "../components/Versions"

export function Component() {
const ipcHandle = (): void => window.electron.ipcRenderer.send("ping")
const ipcHandle = (): void => window.electron?.ipcRenderer.send("ping")

return (
<>
Expand Down

0 comments on commit f3fafff

Please sign in to comment.