import * as React from "react" import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { ClipboardCopyIcon, DotsHorizontalIcon, DownloadIcon, Link2Icon, SizeIcon, } from "@radix-ui/react-icons" interface ImageActionsProps { shouldMerge?: boolean isLink?: boolean onView?: () => void onDownload?: () => void onCopy?: () => void onCopyLink?: () => void } interface ActionButtonProps extends React.ButtonHTMLAttributes { icon: React.ReactNode tooltip: string } export const ActionWrapper = React.memo( React.forwardRef>( ({ children, className, ...props }, ref) => (
{children}
), ), ) ActionWrapper.displayName = "ActionWrapper" export const ActionButton = React.memo( React.forwardRef( ({ icon, tooltip, className, ...props }, ref) => ( {tooltip} ), ), ) ActionButton.displayName = "ActionButton" type ActionKey = "onView" | "onDownload" | "onCopy" | "onCopyLink" const ActionItems: Array<{ key: ActionKey icon: React.ReactNode tooltip: string isLink?: boolean }> = [ { key: "onView", icon: , tooltip: "View image", }, { key: "onDownload", icon: , tooltip: "Download image", }, { key: "onCopy", icon: , tooltip: "Copy image to clipboard", }, { key: "onCopyLink", icon: , tooltip: "Copy image link", isLink: true, }, ] export const ImageActions: React.FC = React.memo( ({ shouldMerge = false, isLink = false, ...actions }) => { const [isOpen, setIsOpen] = React.useState(false) const handleAction = React.useCallback( (e: React.MouseEvent, action: (() => void) | undefined) => { e.preventDefault() e.stopPropagation() action?.() }, [], ) const filteredActions = React.useMemo( () => ActionItems.filter((item) => isLink || !item.isLink), [isLink], ) return ( {shouldMerge ? ( } tooltip="Open menu" onClick={(e) => e.preventDefault()} /> {filteredActions.map(({ key, icon, tooltip }) => ( handleAction(e, actions[key])} >
{icon} {tooltip}
))}
) : ( filteredActions.map(({ key, icon, tooltip }) => ( handleAction(e, actions[key])} /> )) )}
) }, ) ImageActions.displayName = "ImageActions"