'use client' import { useEffect, useLayoutEffect, useRef, type MutableRefObject, type ReactNode } from 'react' import { Group, Panel, Separator, usePanelRef, type PanelImperativeHandle, } from 'react-resizable-panels' import type { CampaignRole } from '@/lib/campaignRoles' import { CHAT_MIN_PX, getSavedChatWidthPx, resolveChatPanelWidthPx, saveChatWidthPx, } from '@/lib/sollinhaChatPanelWidth' import { cn } from '@/lib/utils' import { CampaignAIDrawer } from '@/components/campaign/shell/ai/CampaignAIDrawer' import { CampaignAIFab } from '@/components/campaign/shell/ai/CampaignAIFab' import { CampaignAISidebar } from '@/components/campaign/shell/ai/CampaignAISidebar' import { CampaignAISidebarProvider, useAISidebar, } from '@/components/campaign/shell/ai/CampaignAISidebarContext' const CHAT_DEFAULT_PCT = '25' const MAIN_MIN_PCT = '35' export const CampaignAISidebarShell = ({ role, children, }: { role: CampaignRole children: ReactNode }) => { const panelRef = usePanelRef() // C159 — every role reaches the assistant: the communication assessor's // chat is scoped by the toolset (speech acervo only), not by hiding the // FAB, the drawer or the desktop header button. (C154 hid all three.) return ( {children} ) } /** * The chat Panel (and its Separator) stay mounted at every viewport — changing * `react-resizable-panels`' panel count on breakpoint crossing forces a full * page navigation in this app (B167). Its footprint is driven purely by CSS: a * class on the Panel's OUTER element zeroes the flex space while `react-resizable-panels` * would otherwise keep writing `display:flex` inline there. RRP routes its own * className/style props to the nested content wrapper specifically so they * cannot break the flex layout, so the class is toggled through `elementRef` * (attached to the root div) and wins via `display:none !important` (see * styles.css), surviving RRP's own async, viewport-driven re-layouts. The panel * is deliberately NOT `collapsible` — RRP's auto-collapse of a `display:none` * (0-width) panel left it collapsed on the desktop side of a crossing, which no * expand could reliably undo. Hiding and showing are entirely CSS-driven, so the * panel always keeps a real RRP size (default 25% / the width the user chose and * B166 remembers) whenever it is visible. The mobile Drawer portals over the * content as a Group sibling. */ const AISidebarSurfaces = ({ panelRef, children, }: { panelRef: MutableRefObject children: ReactNode }) => { const { open, isMobile, measured } = useAISidebar()! const groupRef = useRef(null) const chatPanelElementRef = useRef(null) // B166: once the user resizes the panel, a re-render across the // mobile/desktop boundary (or a re-run of the sizing effect) must never // override the size chosen live in this session. const hasUserSizedRef = useRef(false) // Before the first viewport measurement the render must not hide the panel // (it would flash the main content at 100% then settle to 25% on desktop); // the hydration frame is treated as desktop-with-chat-open, matching the // panel's RRP default. B203: a fresh session starts closed — `open` only // leaves false via session restore or user action. const chatVisible = measured ? !isMobile && open : true // Apply the remembered — or the capped-default — width once the desktop // panel's layout has settled; re-applies if the device later crosses back // into desktop (a mobile-first load that grows to desktop still gets it). // The imperative `resize(px)` converts px→% from the group's measured // offsets, so it must not run during the first commit (transient values). // (B166.) useLayoutEffect(() => { if (isMobile || !measured) return const frame = requestAnimationFrame(() => { const group = groupRef.current const panel = panelRef.current if (!group || !panel || hasUserSizedRef.current || panel.isCollapsed()) return const groupWidth = group.getBoundingClientRect().width if (groupWidth <= 0) return panel.resize(resolveChatPanelWidthPx(groupWidth, getSavedChatWidthPx())) }) return () => cancelAnimationFrame(frame) }, [isMobile, measured, panelRef]) // The class carries `display:none !important`, which beats the `display:flex` // RRP writes inline on the Panel element on every (incl. async resize) render // — so the zeroed footprint survives RRP's own re-layouts. useLayoutEffect(() => { chatPanelElementRef.current?.classList.toggle('b167-ai-chat-hidden', !chatVisible) }, [chatVisible]) // A keyboard user mid-conversation in the mobile drawer lands on the desktop // panel when the window grows; move focus into the chat so the task survives // the surface swap instead of dropping to . const prevIsMobileRef = useRef(isMobile) useEffect(() => { const prev = prevIsMobileRef.current prevIsMobileRef.current = isMobile if (prev && !isMobile && open) { chatPanelElementRef.current?.querySelector('textarea')?.focus() } }, [isMobile, open]) const onLayoutChanged = (_layout: unknown, meta: { isUserInteraction: boolean }) => { if (!meta.isUserInteraction) return const panel = panelRef.current if (!panel || panel.isCollapsed()) return const { inPixels } = panel.getSize() if (inPixels < CHAT_MIN_PX) return hasUserSizedRef.current = true saveChatWidthPx(inPixels) } return ( <> {/* Main content */}
{children}
{/* Resize handle — only meaningful while the chat panel is visible */} {/* Double-click resets the panel to its raw `defaultSize` (uncapped 25%); the product default is the capped min(25%, 360px), so the stale reset affordance would reopen wider than allowed — disable it. (B166.) */}
{/* AI chat sidebar — footprint hidden while the chat is closed or on mobile */} {chatVisible ? : null} {/* Mobile chat surface — mounted outside the Group so the main content owns the full width */} {isMobile ? : null} ) }