From 0bcf0df849ba0e7618fa0bb49cd776b6e6b98cde Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 16 May 2024 20:49:07 +0800 Subject: [PATCH] feat: useEntryActions --- .../components/entry-column/context-menu.tsx | 40 +++++++++++++++ .../src/components/entry-content/index.tsx | 2 +- .../src/components/entry-content/share.tsx | 49 ++----------------- .../feed-column/category-context-menu.tsx | 1 - src/renderer/src/hooks/useEntryActions.tsx | 29 +++++++++++ 5 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 src/renderer/src/components/entry-column/context-menu.tsx create mode 100644 src/renderer/src/hooks/useEntryActions.tsx diff --git a/src/renderer/src/components/entry-column/context-menu.tsx b/src/renderer/src/components/entry-column/context-menu.tsx new file mode 100644 index 000000000..c649b3a61 --- /dev/null +++ b/src/renderer/src/components/entry-column/context-menu.tsx @@ -0,0 +1,40 @@ +import { useState } from "react" +import { + ContextMenu, + ContextMenuContent, + ContextMenuItem, + ContextMenuTrigger, +} from "@renderer/components/ui/context-menu" +import { useEntryActions } from "@renderer/hooks/useEntryActions" + +export const items = [ + [ + { + name: "Copy Link", + className: "i-mingcute-link-line", + action: "copyLink", + }, + { + name: "Open in Browser", + className: "i-mingcute-world-2-line", + action: "openInBrowser", + }, + { + name: "Share", + className: "i-mingcute-share-2-line", + action: "share", + }, + ], +] + +export function EntryContextMenu({ + view, + url, +}: { + view?: number + url: string +}) { + const { execAction } = useEntryActions(url) + + return <>{url} +} diff --git a/src/renderer/src/components/entry-content/index.tsx b/src/renderer/src/components/entry-content/index.tsx index 21316d5af..77d44cf84 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 ( <> - + { - switch (action) { - case "copyLink": - navigator.clipboard.writeText(url) - toast({ - duration: 1000, - description: "Link copied to clipboard.", - }) - break - case "openInBrowser": - window.open(url, "_blank") - break - case "share": - window.electron.ipcRenderer.send("share-url", url) - break - } - }, - [url], - ) + const { execAction } = useEntryActions(url) return (
- {items.map((item) => ( + {items[view].map((item) => ( { + const { toast } = useToast() + + const execAction = useCallback( + (action: string) => { + switch (action) { + case "copyLink": + navigator.clipboard.writeText(url) + toast({ + duration: 1000, + description: "Link copied to clipboard.", + }) + break + case "openInBrowser": + window.open(url, "_blank") + break + case "share": + window.electron.ipcRenderer.send("share-url", url) + break + } + }, + [toast], + ) + + return { execAction } +}