'use client' import { createContext, useCallback, useContext, useMemo, useRef, useState, type ReactNode, type RefObject, } from 'react' import { Drawer, DrawerCloseButton, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, } from '@/components/ui/Drawer' import { cn } from '@/lib/utils' export type CampaignListSheetChrome = { title: string description?: string hasCustomFooter: boolean sheetBodyClassName?: string onOpenChange: (open: boolean) => void } type CampaignListSheetContextValue = { bodyPortalRef: RefObject footerPortalRef: RefObject /** Bumps when the drawer body/footer mount targets attach — portals read this. */ portalRevision: number openSheet: (chrome: CampaignListSheetChrome) => void dismissSheet: (onOpenChange: (open: boolean) => void) => void isActiveSheet: (onOpenChange: (open: boolean) => void) => boolean } const CampaignListSheetContext = createContext(null) export const useCampaignListSheet = () => useContext(CampaignListSheetContext) /** * One bottom Drawer for an entire list surface (B42 mobile cards). Cell bodies * portal into it so we never mount hundreds of Drawer roots and never sync form * trees through provider state (which tripped React #185 in prod). */ export const CampaignListSheetProvider = ({ children }: { children: ReactNode }) => { const titleRef = useRef(null) const bodyPortalRef = useRef(null) const footerPortalRef = useRef(null) const [open, setOpen] = useState(false) const [chrome, setChrome] = useState(null) const [portalRevision, setPortalRevision] = useState(0) const activeOnOpenChangeRef = useRef<((open: boolean) => void) | null>(null) const attachBodyPortal = useCallback((node: HTMLDivElement | null) => { bodyPortalRef.current = node if (node) setPortalRevision((value) => value + 1) }, []) const attachFooterPortal = useCallback((node: HTMLDivElement | null) => { footerPortalRef.current = node if (node) setPortalRevision((value) => value + 1) }, []) const dismissSheet = useCallback((onOpenChange: (open: boolean) => void) => { if (activeOnOpenChangeRef.current !== onOpenChange) return setOpen(false) activeOnOpenChangeRef.current = null setChrome(null) onOpenChange(false) }, []) const openSheet = useCallback((next: CampaignListSheetChrome) => { const previousOnOpenChange = activeOnOpenChangeRef.current if (previousOnOpenChange && previousOnOpenChange !== next.onOpenChange) { previousOnOpenChange(false) } activeOnOpenChangeRef.current = next.onOpenChange setChrome((current) => { if ( current && current.onOpenChange === next.onOpenChange && current.title === next.title && current.description === next.description && current.sheetBodyClassName === next.sheetBodyClassName && current.hasCustomFooter === next.hasCustomFooter ) { return current } return next }) setOpen(true) }, []) const isActiveSheet = useCallback( (onOpenChange: (open: boolean) => void) => open && activeOnOpenChangeRef.current === onOpenChange, [open], ) const handleOpenChange = useCallback( (nextOpen: boolean) => { if (nextOpen) return const onOpenChange = activeOnOpenChangeRef.current if (!onOpenChange) { setOpen(false) setChrome(null) return } dismissSheet(onOpenChange) }, [dismissSheet], ) const contextValue = useMemo( () => ({ bodyPortalRef, footerPortalRef, portalRevision, openSheet, dismissSheet, isActiveSheet, }), [portalRevision, openSheet, dismissSheet, isActiveSheet], ) return ( {children} {chrome && open ? ( {chrome.title} {chrome.description ? ( {chrome.description} ) : null}
{!chrome.hasCustomFooter ? Fechar : null} ) : null} ) }