Skip to content

Commit

Permalink
feat: useEntryActions
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 16, 2024
1 parent 5b0e946 commit 0bcf0df
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 46 deletions.
40 changes: 40 additions & 0 deletions src/renderer/src/components/entry-column/context-menu.tsx
Original file line number Diff line number Diff line change
@@ -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}</>
}
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} />
<EntryShare url={entry?.url} view={0} />
<m.div
className="px-5 py-5 overflow-y-auto h-full"
initial={{ opacity: 0.01, y: 100 }}
Expand Down
49 changes: 5 additions & 44 deletions src/renderer/src/components/entry-content/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,19 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@renderer/components/ui/tooltip"
import { useCallback } from "react"
import { useToast } from "@renderer/components/ui/use-toast"
import { cn } from "@renderer/lib/utils"
import { useEntryActions } from "@renderer/hooks/useEntryActions"
import { items } from "@renderer/components/entry-column/context-menu"

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 EntryShare({ url }: { url?: string }) {
export function EntryShare({ view, url }: { view: number; url?: string }) {
if (!url) return null

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
}
},
[url],
)
const { execAction } = useEntryActions(url)

return (
<div className="px-5 h-14 flex items-center text-lg justify-end text-zinc-500 gap-5">
<TooltipProvider delayDuration={300}>
{items.map((item) => (
{items[view].map((item) => (
<Tooltip>
<TooltipTrigger className="flex items-center my-2">
<i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
ContextMenuSeparator,
} from "@renderer/components/ui/context-menu"
import { Dialog, DialogTrigger } from "@renderer/components/ui/dialog"
import { CategoryRenameDialog } from "./category-rename-dialog"
Expand Down
29 changes: 29 additions & 0 deletions src/renderer/src/hooks/useEntryActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useToast } from "@renderer/components/ui/use-toast"
import { useCallback } from "react"

export const useEntryActions = (url: string) => {
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 }
}

0 comments on commit 0bcf0df

Please sign in to comment.