From 506730400a862a74f22bc309a609dea0028d3a69 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 16 May 2024 22:00:36 +0800 Subject: [PATCH] feat: save images to eagle --- src/main/index.ts | 33 +++++++++++++ src/renderer/public/eagle.svg | 48 +++++++++++++++++++ .../components/entry-column/article-item.tsx | 2 +- .../components/entry-column/context-menu.tsx | 17 +++++-- .../src/components/entry-content/index.tsx | 2 +- .../src/components/entry-content/share.tsx | 37 ++++++++++---- src/renderer/src/hooks/useEntryActions.tsx | 30 +++++++++++- 7 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 src/renderer/public/eagle.svg diff --git a/src/main/index.ts b/src/main/index.ts index 8807f7234..dcf9b884f 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -39,6 +39,39 @@ app.whenReady().then(() => { }).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/renderer/public/eagle.svg b/src/renderer/public/eagle.svg new file mode 100644 index 000000000..0c7f08db8 --- /dev/null +++ b/src/renderer/public/eagle.svg @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/renderer/src/components/entry-column/article-item.tsx b/src/renderer/src/components/entry-column/article-item.tsx index 8a44c85fe..0f5d65156 100644 --- a/src/renderer/src/components/entry-column/article-item.tsx +++ b/src/renderer/src/components/entry-column/article-item.tsx @@ -6,7 +6,7 @@ import { EntryContextMenu } from "./context-menu" export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) { return ( - +
diff --git a/src/renderer/src/components/entry-column/context-menu.tsx b/src/renderer/src/components/entry-column/context-menu.tsx index 73b332eff..fdda5e5ad 100644 --- a/src/renderer/src/components/entry-column/context-menu.tsx +++ b/src/renderer/src/components/entry-column/context-menu.tsx @@ -5,6 +5,7 @@ import { ContextMenuTrigger, } from "@renderer/components/ui/context-menu" import { useEntryActions } from "@renderer/hooks/useEntryActions" +import { EntriesResponse } from "@renderer/lib/types" export const items = [ [ @@ -18,6 +19,11 @@ export const items = [ className: "i-mingcute-world-2-line", action: "openInBrowser", }, + { + name: "Save Images to Eagle", + icon: "/eagle.svg", + action: "save-to-eagle", + }, { name: "Share", className: "i-mingcute-share-2-line", @@ -27,17 +33,20 @@ export const items = [ ] export function EntryContextMenu({ + entry, view, - url, children, }: { + entry: EntriesResponse[number] view: number - url?: string children: React.ReactNode }) { - if (!url) return children + if (!entry?.url) return children - const { execAction } = useEntryActions(url) + const { execAction } = useEntryActions({ + url: entry.url, + images: entry.images, + }) return ( diff --git a/src/renderer/src/components/entry-content/index.tsx b/src/renderer/src/components/entry-content/index.tsx index 77d44cf84..8efaacdd4 100644 --- a/src/renderer/src/components/entry-content/index.tsx +++ b/src/renderer/src/components/entry-content/index.tsx @@ -17,7 +17,7 @@ export function EntryContent({ entry }: { entry: ActivedEntry }) { return ( <> - + {items[view].map((item) => ( - + - execAction(item.action)} - /> + {item.icon ? ( + execAction(item.action)} + /> + ) : ( + execAction(item.action)} + /> + )} {item.name} diff --git a/src/renderer/src/hooks/useEntryActions.tsx b/src/renderer/src/hooks/useEntryActions.tsx index a8d017670..2c761cb0b 100644 --- a/src/renderer/src/hooks/useEntryActions.tsx +++ b/src/renderer/src/hooks/useEntryActions.tsx @@ -1,7 +1,13 @@ import { useToast } from "@renderer/components/ui/use-toast" import { useCallback } from "react" -export const useEntryActions = (url: string) => { +export const useEntryActions = ({ + url, + images, +}: { + url: string + images?: string[] +}) => { const { toast } = useToast() const execAction = useCallback( @@ -20,6 +26,28 @@ export const useEntryActions = (url: string) => { case "share": window.electron.ipcRenderer.send("share-url", 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, + }) + break } }, [toast],