'use client' import { usePathname } from 'next/navigation' import { useCallback, useEffect, useRef } from 'react' import { CampaignGlobalSearchBody } from '@/components/campaign/dashboard/CampaignGlobalSearchMount' import { CampaignHomeActionStrip } from '@/components/campaign/dashboard/CampaignHomeActionStrip' import { useHomeSearch } from '@/components/campaign/dashboard/HomeSearchContext' import { HomeSearchExcludeProvider } from '@/components/campaign/dashboard/HomeSearchExcludeContext' import { useCampaignQuickActionContext } from '@/components/campaign/shell/CampaignQuickActionContext' import { useCampaignQuickActionsSnap } from '@/components/campaign/shell/CampaignQuickActionsSnapContext' import { Drawer, DrawerContent, DrawerTitle } from '@/components/ui/Drawer' import { QUICK_ACTIONS_SNAP_COLLAPSED, QUICK_ACTIONS_SNAP_DOCK, QUICK_ACTIONS_SNAP_FULL, QUICK_ACTIONS_SNAP_POINTS, quickActionsSnapAfterDismiss, quickActionsSnapIsDock, quickActionsSnapIsFull, type QuickActionsSnapPoint, } from '@/lib/campaignQuickActionSnap' import type { CampaignQuickAction } from '@/lib/campaignQuickActionTypes' import { resolveHomeSearchExcludeContext } from '@/lib/homeSearchExcludeCurrentEntity' import { cn } from '@/lib/utils' const SNAP_POINTS = [...QUICK_ACTIONS_SNAP_POINTS] export const CampaignQuickActionsDrawer = ({ actions, }: { actions: readonly CampaignQuickAction[] }) => { const pathname = usePathname() const { context } = useCampaignQuickActionContext() const excludeContext = resolveHomeSearchExcludeContext(pathname, context) const { snapPoint, setSnapPoint, isDock, isFull } = useCampaignQuickActionsSnap() const { uiFocused, clear } = useHomeSearch() const showActions = actions.length > 0 && isDock && !isFull const showSearchResults = isDock || isFull const skipNextPathCollapseRef = useRef(true) const wasUiFocusedRef = useRef(false) const dismissToCollapsed = useCallback(() => { // Clear first so the uiFocused→FULL effect cannot reopen on the same tick. clear() setSnapPoint(quickActionsSnapAfterDismiss()) }, [clear, setSnapPoint]) const handleSnapPointChange = useCallback( (next: QuickActionsSnapPoint | string | number | null) => { if (next === QUICK_ACTIONS_SNAP_COLLAPSED) { dismissToCollapsed() return } if (next === QUICK_ACTIONS_SNAP_DOCK || next === QUICK_ACTIONS_SNAP_FULL) { setSnapPoint(next) return } dismissToCollapsed() }, [dismissToCollapsed, setSnapPoint], ) const toggleSnap = useCallback(() => { if (uiFocused || quickActionsSnapIsFull(snapPoint)) { dismissToCollapsed() return } if (quickActionsSnapIsDock(snapPoint)) { setSnapPoint(QUICK_ACTIONS_SNAP_COLLAPSED) return } setSnapPoint(QUICK_ACTIONS_SNAP_DOCK) }, [dismissToCollapsed, setSnapPoint, snapPoint, uiFocused]) const handleOpenChange = useCallback( (open: boolean) => { if (!open) { dismissToCollapsed() } }, [dismissToCollapsed], ) useEffect(() => { if (skipNextPathCollapseRef.current) { skipNextPathCollapseRef.current = false return } dismissToCollapsed() }, [dismissToCollapsed, pathname]) useEffect(() => { if (!uiFocused) return wasUiFocusedRef.current = true if (!quickActionsSnapIsFull(snapPoint)) { setSnapPoint(QUICK_ACTIONS_SNAP_FULL) } }, [snapPoint, setSnapPoint, uiFocused]) // Empty blur (uiFocused true→false) → collapsed. Skip the never-focused load path // so the initial dock from B100 survives. useEffect(() => { if (uiFocused) return if (!wasUiFocusedRef.current) return wasUiFocusedRef.current = false setSnapPoint(quickActionsSnapAfterDismiss()) }, [setSnapPoint, uiFocused]) return ( Ações rápidas
{showActions ? (
({ id: action.id, label: action.label, icon: action.icon, description: action.description, href: action.href, }))} className="w-full" />
) : null}
) }