'use client'
import type { LucideIcon } from 'lucide-react'
import Link from 'next/link'
import { useCallback, useId, useRef, useState, type MouseEvent, type ReactNode } from 'react'
import {
Drawer,
DrawerCloseButton,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
} from '@/components/ui/Drawer'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { useCoarsePointer } from '@/lib/campaignCoarsePointer'
import { useCampaignLongPress } from '@/lib/campaignLongPress'
import { cn } from '@/lib/utils'
export type CampaignHomeActionButtonProps = {
label: string
icon: LucideIcon
description?: string
href?: string
onClick?: () => void
disabled?: boolean
}
const actionControlClassName =
'group flex min-h-11 w-[4.75rem] shrink-0 snap-start flex-col items-center gap-2 rounded-md text-center outline-none focus-visible:ring-2 focus-visible:ring-ring active:opacity-90 disabled:pointer-events-none disabled:opacity-50'
const circleClassName =
'flex size-14 items-center justify-center rounded-full bg-muted text-foreground transition-[transform,colors] duration-150 ease-out motion-reduce:transition-none pointer-fine:group-hover:scale-[1.05] pointer-coarse:group-data-[pressing=true]:scale-[1.05] motion-reduce:scale-100 group-hover:bg-muted/80 group-focus-visible:bg-muted/80'
const ActionVisual = ({ label, icon: Icon }: { label: string; icon: LucideIcon }) => (
<>
{label}
>
)
export const CampaignHomeActionButton = ({
label,
icon,
description,
href,
onClick,
disabled = false,
}: CampaignHomeActionButtonProps) => {
const isCoarsePointer = useCoarsePointer()
const [drawerOpen, setDrawerOpen] = useState(false)
const descriptionId = useId()
const titleRef = useRef(null)
const openDescription = useCallback(() => {
if (description) setDrawerOpen(true)
}, [description])
const longPressEnabled = Boolean(description) && isCoarsePointer && !disabled
const isInert = !disabled && !href && !onClick
const { pressing, ...longPressHandlers } = useCampaignLongPress({
enabled: longPressEnabled,
onLongPress: openDescription,
onClick:
href || !onClick
? undefined
: (_event: MouseEvent) => {
onClick()
},
})
const className = cn(actionControlClassName, isInert && 'cursor-default')
const aria = {
'aria-label': label,
'aria-describedby': drawerOpen && longPressEnabled ? descriptionId : undefined,
}
const longPressProps = longPressEnabled
? {
...longPressHandlers,
'data-pressing': pressing ? true : undefined,
}
: undefined
let control: ReactNode
if (disabled) {
control = (
)
} else if (href) {
control = (
)
} else {
control = (
)
}
const withTooltip =
description && !isCoarsePointer && !disabled ? (
{control}
{description}
) : (
control
)
return (
<>
{withTooltip}
{description && isCoarsePointer && !disabled ? (
{label}
{description}
Fechar
) : null}
>
)
}