feat: tipc showShareMenu and saveToEagle

This commit is contained in:
DIYgod 2024-05-18 18:56:43 +08:00
parent 4f20e29414
commit d4ea0962d1
No known key found for this signature in database
3 changed files with 57 additions and 66 deletions

View File

@ -1,5 +1,5 @@
import "dotenv/config"
import { app, BrowserWindow, ipcMain, ShareMenu } from "electron"
import { app, BrowserWindow, ipcMain } from "electron"
import path from "path"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { createWindow } from "./window"
@ -34,48 +34,6 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})
// IPC test
ipcMain.on("ping", () => console.log("pong"))
ipcMain.on("share-url", (_, url: string) => {
new ShareMenu({
urls: [url],
}).popup()
})
ipcMain.on(
"save-to-eagle",
async (
event,
data: {
url: string
images: string[]
},
) => {
try {
const res = await fetch("http://localhost:41595/api/item/addFromURLs", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
items: data.images?.map((image) => ({
url: image,
website: data.url,
headers: {
referer: data.url,
},
})),
}),
})
const result = await res.json()
event.reply("save-to-eagle-reply", result)
} catch (error) {
event.reply("save-to-eagle-reply", null)
}
},
)
createWindow()
app.on("activate", function () {

View File

@ -1,5 +1,5 @@
import { tipc } from "@egoist/tipc/main"
import { Menu, MessageBoxOptions, dialog } from "electron"
import { Menu, MessageBoxOptions, ShareMenu, dialog } from "electron"
const t = tipc.create()
@ -53,6 +53,45 @@ export const router = {
})
return result.response === 0
}),
showShareMenu: t.procedure
.input<string>()
.action(async ({ input, context }) => {
const menu = new ShareMenu({
urls: [input],
})
menu.popup({
callback: () => {
context.sender.send("menu-closed")
},
})
}),
saveToEagle: t.procedure
.input<{ url: string; images: string[] }>()
.action(async ({ input }) => {
try {
const res = await fetch("http://localhost:41595/api/item/addFromURLs", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
items: input.images?.map((image) => ({
url: image,
website: input.url,
headers: {
referer: input.url,
},
})),
}),
})
return await res.json()
} catch (error) {
return null
}
}),
}
export type Router = typeof router

View File

@ -2,6 +2,7 @@ import { useToast } from "@renderer/components/ui/use-toast"
import { useCallback } from "react"
import { ofetch } from "ofetch"
import { useQuery } from "@tanstack/react-query"
import { client } from "@renderer/lib/client"
export const useCheckEagle = () =>
useQuery({
@ -57,7 +58,7 @@ export const useEntryActions = ({
const { toast } = useToast()
const execAction = useCallback(
(action: string) => {
async (action: string) => {
switch (action) {
case "copyLink":
navigator.clipboard.writeText(url)
@ -70,29 +71,22 @@ export const useEntryActions = ({
window.open(url, "_blank")
break
case "share":
window.electron.ipcRenderer.send("share-url", url)
client.showShareMenu(url)
break
case "save-to-eagle":
window.electron.ipcRenderer.once(
"save-to-eagle-reply",
(_, response) => {
if (response?.status === "success") {
toast({
duration: 3000,
description: "Saved to Eagle.",
})
} else {
toast({
duration: 3000,
description: "Failed to save to Eagle.",
})
}
},
)
window.electron.ipcRenderer.send("save-to-eagle", {
url,
images,
})
if (!images?.length) return
const response = await client.saveToEagle({ url, images })
if (response?.status === "success") {
toast({
duration: 3000,
description: "Saved to Eagle.",
})
} else {
toast({
duration: 3000,
description: "Failed to save to Eagle.",
})
}
break
}
},