From d4ea0962d19188a255cd0ec586d2773ec3a2ad82 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 18 May 2024 18:56:43 +0800 Subject: [PATCH] feat: tipc showShareMenu and saveToEagle --- src/main/index.ts | 44 +--------------------- src/main/tipc.ts | 41 +++++++++++++++++++- src/renderer/src/hooks/useEntryActions.tsx | 38 ++++++++----------- 3 files changed, 57 insertions(+), 66 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index ea000b10f..7b01c13a6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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 () { diff --git a/src/main/tipc.ts b/src/main/tipc.ts index d0d83b61e..1632c7583 100644 --- a/src/main/tipc.ts +++ b/src/main/tipc.ts @@ -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() + .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 diff --git a/src/renderer/src/hooks/useEntryActions.tsx b/src/renderer/src/hooks/useEntryActions.tsx index 985c18da6..ba680b572 100644 --- a/src/renderer/src/hooks/useEntryActions.tsx +++ b/src/renderer/src/hooks/useEntryActions.tsx @@ -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 } },