Skip to content

Commit

Permalink
feat: entry openInBrowser and share
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 16, 2024
1 parent 95ad3a7 commit 5b0e946
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
8 changes: 7 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config"
import { app, BrowserWindow, ipcMain } from "electron"
import { app, BrowserWindow, ipcMain, ShareMenu } from "electron"
import path from "path"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { createWindow } from "./window"
Expand Down Expand Up @@ -33,6 +33,12 @@ app.whenReady().then(() => {
// IPC test
ipcMain.on("ping", () => console.log("pong"))

ipcMain.on("share-url", (_, url: string) => {
new ShareMenu({
urls: [url],
}).popup()
})

createWindow()

app.on("activate", function () {
Expand Down
68 changes: 50 additions & 18 deletions src/renderer/src/components/entry-content/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,66 @@ import {
} from "@renderer/components/ui/tooltip"
import { useCallback } from "react"
import { useToast } from "@renderer/components/ui/use-toast"
import { cn } from "@renderer/lib/utils"

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 }) {
if (!url) return null

const { toast } = useToast()

const copyLink = useCallback(() => {
navigator.clipboard.writeText(url)
toast({
duration: 1000,
description: "Link copied to clipboard.",
})
}, [url])
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],
)

return (
<div className="px-5 h-14 flex items-center text-lg justify-end text-zinc-500 gap-5">
<TooltipProvider delayDuration={300}>
<Tooltip>
<TooltipTrigger className="flex items-center my-2">
<i
className="i-mingcute-link-line cursor-pointer no-drag-region"
onClick={() => copyLink()}
/>
</TooltipTrigger>
<TooltipContent side="bottom">Copy Link</TooltipContent>
</Tooltip>
<i className="i-mingcute-world-2-line cursor-pointer no-drag-region" />
<i className="i-mingcute-share-2-line cursor-pointer no-drag-region" />
{items.map((item) => (
<Tooltip>
<TooltipTrigger className="flex items-center my-2">
<i
className={cn(item.className, "cursor-pointer no-drag-region")}
onClick={() => execAction(item.action)}
/>
</TooltipTrigger>
<TooltipContent side="bottom">{item.name}</TooltipContent>
</Tooltip>
))}
</TooltipProvider>
</div>
)
Expand Down

0 comments on commit 5b0e946

Please sign in to comment.