Skip to content

Commit

Permalink
feat: save images to eagle
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 16, 2024
1 parent acb1b0c commit 5067304
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 15 deletions.
33 changes: 33 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
48 changes: 48 additions & 0 deletions src/renderer/public/eagle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/renderer/src/components/entry-column/article-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EntryContextMenu } from "./context-menu"

export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) {
return (
<EntryContextMenu url={entry.url} view={0}>
<EntryContextMenu entry={entry} view={0}>
<div className="flex my-5 px-2 py-3">
<FeedIcon feed={entry.feeds} />
<div className="line-clamp-5 text-sm flex-1 -mt-0.5 leading-tight">
Expand Down
17 changes: 13 additions & 4 deletions src/renderer/src/components/entry-column/context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[
Expand All @@ -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",
Expand All @@ -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 (
<ContextMenu>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/entry-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function EntryContent({ entry }: { entry: ActivedEntry }) {

return (
<>
<EntryShare url={entry?.url} view={0} />
<EntryShare entry={entry} view={0} />
<m.div
className="px-5 py-5 overflow-y-auto h-full"
initial={{ opacity: 0.01, y: 100 }}
Expand Down
37 changes: 29 additions & 8 deletions src/renderer/src/components/entry-content/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,43 @@ import {
import { cn } from "@renderer/lib/utils"
import { useEntryActions } from "@renderer/hooks/useEntryActions"
import { items } from "@renderer/components/entry-column/context-menu"
import { ActivedEntry } from "@renderer/lib/types"

export function EntryShare({ view, url }: { view: number; url?: string }) {
if (!url) return null
export function EntryShare({
view,
entry,
}: {
view: number
entry: ActivedEntry
}) {
if (!entry?.url) return null

const { execAction } = useEntryActions(url)
const { execAction } = useEntryActions({
url: entry.url,
images: entry.images,
})

return (
<div className="px-5 h-14 flex items-center text-lg justify-end text-zinc-500 gap-5">
<TooltipProvider delayDuration={300}>
{items[view].map((item) => (
<Tooltip>
<Tooltip key={item.name}>
<TooltipTrigger className="flex items-center my-2">
<i
className={cn(item.className, "cursor-pointer no-drag-region")}
onClick={() => execAction(item.action)}
/>
{item.icon ? (
<img
className="w-4 h-4 grayscale cursor-pointer no-drag-region"
src={item.icon}
onClick={() => execAction(item.action)}
/>
) : (
<i
className={cn(
item.className,
"cursor-pointer no-drag-region",
)}
onClick={() => execAction(item.action)}
/>
)}
</TooltipTrigger>
<TooltipContent side="bottom">{item.name}</TooltipContent>
</Tooltip>
Expand Down
30 changes: 29 additions & 1 deletion src/renderer/src/hooks/useEntryActions.tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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],
Expand Down

0 comments on commit 5067304

Please sign in to comment.