{"version":3,"file":"SectionNavigator.mjs","names":[],"sources":["../../../../../src/plugins/ChannelDetail/SectionNavigator/SectionNavigator.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, {\n  type ComponentProps,\n  type ComponentType,\n  createContext,\n  type HTMLAttributes,\n  useCallback,\n  useContext,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\n\nexport type SectionNavigatorLayout = string;\n\nexport const SECTION_NAVIGATOR_LAYOUT = {\n  inline: 'inline',\n  tabs: 'tabs',\n} as const;\n\nexport type SectionNavigatorRoute = {\n  id: string;\n};\n\nexport type SectionNavigatorSectionContentProps = {\n  layout: SectionNavigatorLayout;\n};\n\nexport type SectionNavigatorNavButtonProps = ComponentProps<'button'> & {\n  sectionId: string;\n  selected: boolean;\n  select: () => void;\n};\n\nexport type SectionNavigatorSection = {\n  id: string;\n  NavButton: ComponentType<SectionNavigatorNavButtonProps>;\n  SectionContent: ComponentType<SectionNavigatorSectionContentProps>;\n};\n\nexport type SectionNavigatorLayoutObserverFactory = ({\n  element,\n  setLayout,\n  tabsLayoutMinWidth,\n}: {\n  element: HTMLElement;\n  setLayout: (layout: SectionNavigatorLayout) => void;\n  tabsLayoutMinWidth: number;\n}) => (() => void) | void;\n\nexport type SectionNavigatorContextValue = {\n  layout: SectionNavigatorLayout;\n  history: SectionNavigatorRoute[];\n  historyPop: () => void;\n  historyPush: (route: SectionNavigatorRoute) => void;\n  /** Whether the navigation drawer overlay is currently open (inline layout only). */\n  isNavigationOpen: boolean;\n  /** Opens the navigation drawer overlay (inline layout). */\n  openNavigation: () => void;\n  /** Closes the navigation drawer overlay (inline layout). */\n  closeNavigation: () => void;\n};\n\nexport type SectionNavigatorProps = HTMLAttributes<HTMLDivElement> & {\n  sections: SectionNavigatorSection[];\n  createLayoutObserver?: SectionNavigatorLayoutObserverFactory;\n  defaultLayout?: SectionNavigatorLayout;\n  initialHistory?: SectionNavigatorRoute[];\n  layout?: SectionNavigatorLayout;\n  /** Called whenever the resolved layout changes (and once on mount). */\n  onLayoutChange?: (layout: SectionNavigatorLayout) => void;\n  tabsLayoutMinWidth?: number;\n};\n\nconst DEFAULT_TABS_LAYOUT_MIN_WIDTH = 640;\n\nconst defaultCreateLayoutObserver: SectionNavigatorLayoutObserverFactory = ({\n  element,\n  setLayout,\n  tabsLayoutMinWidth,\n}) => {\n  if (typeof ResizeObserver === 'undefined') return;\n\n  const observedElement = element.parentElement ?? element;\n  const updateLayout = (width: number) => {\n    if (width <= 0) return;\n\n    setLayout(\n      width < tabsLayoutMinWidth\n        ? SECTION_NAVIGATOR_LAYOUT.inline\n        : SECTION_NAVIGATOR_LAYOUT.tabs,\n    );\n  };\n  const observer = new ResizeObserver(([entry]) => {\n    updateLayout(entry.contentRect.width);\n  });\n\n  updateLayout(observedElement.getBoundingClientRect().width);\n  observer.observe(observedElement);\n\n  return () => observer.disconnect();\n};\n\nconst defaultSectionNavigatorContextValue: SectionNavigatorContextValue = {\n  closeNavigation: () => undefined,\n  history: [],\n  historyPop: () => undefined,\n  historyPush: () => undefined,\n  isNavigationOpen: false,\n  layout: SECTION_NAVIGATOR_LAYOUT.tabs,\n  openNavigation: () => undefined,\n};\n\nconst SectionNavigatorContext = createContext<SectionNavigatorContextValue>(\n  defaultSectionNavigatorContextValue,\n);\n\nexport const useSectionNavigatorContext = () => useContext(SectionNavigatorContext);\n\nconst getCurrentRoute = (history: SectionNavigatorRoute[]) => history[history.length - 1];\n\nexport const SectionNavigator = ({\n  className,\n  createLayoutObserver = defaultCreateLayoutObserver,\n  defaultLayout = SECTION_NAVIGATOR_LAYOUT.tabs,\n  initialHistory,\n  layout: controlledLayout,\n  onLayoutChange,\n  sections,\n  tabsLayoutMinWidth = DEFAULT_TABS_LAYOUT_MIN_WIDTH,\n  ...props\n}: SectionNavigatorProps) => {\n  const rootRef = useRef<HTMLDivElement | null>(null);\n  const [internalLayout, setInternalLayout] =\n    useState<SectionNavigatorLayout>(defaultLayout);\n  const [history, setHistory] = useState<SectionNavigatorRoute[]>(\n    () => initialHistory ?? (sections[0] ? [{ id: sections[0].id }] : []),\n  );\n  const [isNavigationOpen, setIsNavigationOpen] = useState(false);\n  const layout = controlledLayout ?? internalLayout;\n  const currentRoute = getCurrentRoute(history);\n  const currentSection = sections.find((section) => section.id === currentRoute?.id);\n  const activeSection = currentSection ?? sections[0];\n  const isInlineLayout = layout === SECTION_NAVIGATOR_LAYOUT.inline;\n  const showDockedNavigation = !isInlineLayout || !currentSection;\n\n  const openNavigation = useCallback(() => setIsNavigationOpen(true), []);\n  const closeNavigation = useCallback(() => setIsNavigationOpen(false), []);\n\n  const historyPush = useCallback(\n    (route: SectionNavigatorRoute) => {\n      setHistory((history) => {\n        const currentRoute = getCurrentRoute(history);\n\n        if (currentRoute?.id === route.id) return history;\n        if (layout === SECTION_NAVIGATOR_LAYOUT.tabs) return [route];\n\n        return [...history, route];\n      });\n    },\n    [layout],\n  );\n\n  const historyPop = useCallback(() => {\n    setHistory((history) => (history.length > 1 ? history.slice(0, -1) : history));\n  }, []);\n\n  // The drawer overlay only exists in inline layout; close it on leaving inline so it does not\n  // linger after a resize to a wider layout.\n  useEffect(() => {\n    if (!isInlineLayout) setIsNavigationOpen(false);\n  }, [isInlineLayout]);\n\n  useEffect(() => {\n    if (!isNavigationOpen) return;\n\n    const handleKeyDown = (event: KeyboardEvent) => {\n      if (event.key === 'Escape') closeNavigation();\n    };\n\n    document.addEventListener('keydown', handleKeyDown);\n    return () => document.removeEventListener('keydown', handleKeyDown);\n  }, [closeNavigation, isNavigationOpen]);\n\n  useEffect(() => {\n    onLayoutChange?.(layout);\n  }, [layout, onLayoutChange]);\n\n  useEffect(() => {\n    if (controlledLayout) return;\n    if (!rootRef.current) return;\n\n    return createLayoutObserver({\n      element: rootRef.current,\n      setLayout: setInternalLayout,\n      tabsLayoutMinWidth,\n    });\n  }, [controlledLayout, createLayoutObserver, tabsLayoutMinWidth]);\n\n  useEffect(() => {\n    setHistory((history) => {\n      const currentRoute = getCurrentRoute(history);\n      const currentRouteHasSection = sections.some(\n        (section) => section.id === currentRoute?.id,\n      );\n\n      if (!currentRoute) return sections[0] ? [{ id: sections[0].id }] : [];\n\n      if (currentRouteHasSection) return history;\n\n      return sections[0] ? [{ id: sections[0].id }] : [];\n    });\n  }, [sections]);\n\n  const contextValue = useMemo<SectionNavigatorContextValue>(\n    () => ({\n      closeNavigation,\n      history,\n      historyPop,\n      historyPush,\n      isNavigationOpen,\n      layout,\n      openNavigation,\n    }),\n    [\n      closeNavigation,\n      history,\n      historyPop,\n      historyPush,\n      isNavigationOpen,\n      layout,\n      openNavigation,\n    ],\n  );\n\n  const Content = activeSection?.SectionContent;\n\n  const navigation = (\n    <div className='str-chat__section-navigator__navigation'>\n      {sections.map((section) => {\n        const NavButton = section.NavButton;\n        const selected = activeSection?.id === section.id;\n\n        return (\n          <div className='str-chat__section-navigator__navigation-item' key={section.id}>\n            <NavButton\n              className='str-chat__section-navigator__navigation-item__nav-button'\n              sectionId={section.id}\n              select={() => {\n                historyPush({ id: section.id });\n                closeNavigation();\n              }}\n              selected={selected}\n            />\n          </div>\n        );\n      })}\n    </div>\n  );\n\n  return (\n    <SectionNavigatorContext.Provider value={contextValue}>\n      <div\n        className={clsx('str-chat__section-navigator', className, {\n          'str-chat__section-navigator--inline': isInlineLayout,\n        })}\n        data-layout={layout}\n        ref={rootRef}\n        {...props}\n      >\n        {showDockedNavigation && navigation}\n        {Content && (\n          <div className='str-chat__section-navigator__content'>\n            <Content layout={layout} />\n          </div>\n        )}\n        {isInlineLayout && (\n          <div\n            className={clsx('str-chat__section-navigator__navigation-overlay', {\n              'str-chat__section-navigator__navigation-overlay--open': isNavigationOpen,\n            })}\n          >\n            <button\n              aria-hidden\n              className='str-chat__section-navigator__navigation-scrim'\n              onClick={closeNavigation}\n              tabIndex={-1}\n              type='button'\n            />\n            <div className='str-chat__section-navigator__navigation-drawer'>\n              {navigation}\n            </div>\n          </div>\n        )}\n      </div>\n    </SectionNavigatorContext.Provider>\n  );\n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