import React, { useEffect, useRef, useState } from 'react' import Button from '../Button/Button' import DropdownMenu from '../DropdownMenu/DropdownMenu' import Icon from '../Icons/Icon' import { type IconStringList } from '../Icons/Icon.models' import Menu from '../Menu/Menu' import { SideDrawer } from '../SideDrawer/SideDrawer' import { useIsMobileView } from '../../hooks/useIsMobileView/useIsMobileView' import styles from './_account_popover.module.scss' import { c } from '../../translations/LibraryTranslationService' export type AccountPopoverProps = { /** The name of the user */ name: string /** The array of options to be displayed in the popover */ options: { icon: IconStringList label: string callout: () => void hide?: boolean keepOpen?: boolean }[] /** Function to close the mobile drawer. This is kept as optional because it is not needed in LeftNavDesktop. */ closeMobileDrawer?: () => void /** Function to close the Sidebar expanded state. This is kept as optional because it is not needed in LeftNavMobile. */ closeSidebar?: (force?: boolean) => void /** Function to open the Sidebar expanded state. */ openSidebar?: () => void /** There are 2 variations: collapsed and expanded. This prop determines when to show each. */ expanded: boolean /** Shows whether LeftNavMobile is being rendered with this component */ mobile?: boolean /** Optional prop to make username or email read only */ disabled?: boolean } const AccountPopover = ({ name, options, closeMobileDrawer, expanded, closeSidebar, openSidebar, mobile, disabled = false, }: AccountPopoverProps): React.JSX.Element => { const [isDrawerOpen, setDrawerOpen] = useState(false) const isMobileView = useIsMobileView() const contentRef = useRef(null) // Create actions array that can be reused by both DropdownMenu and Menu const createActions = ( setVisible?: React.Dispatch>, ) => options .filter((option) => !option.hide) .map((option) => ({ text: option.label, icon: option.icon, callout: () => { option.callout() if (!option.keepOpen || !mobile) { setVisible?.(false) } if (mobile && !option.keepOpen) { closeMobileDrawer?.() } // Always close sidebar in desktop mode (when closeSidebar is available) if (!isMobileView && closeSidebar) { closeSidebar(true) // Force close to override hover detection logic } }, })) const PopoverOrDrawerContent = ({ setVisible, }: { setVisible: React.Dispatch> }) => const PopoverOrSidedrawerChildren = ({ visible, setVisible, }: { visible: boolean setVisible: React.Dispatch> }) => { return (
) } return isMobileView ? ( <> {PopoverOrSidedrawerChildren({ visible: isDrawerOpen, setVisible: setDrawerOpen, })} setDrawerOpen(false)} > {PopoverOrDrawerContent({ setVisible: setDrawerOpen })} ) : ( {}} /> } popoverProps={{ position: 'top', appendTo: contentRef.current, }} /> ) } export default AccountPopover const AccountPopoverList = ({ actions, }: { actions: { text: string icon: IconStringList callout: () => void }[] }) => { return } const Container = ({ name, expanded, openSidebar, disabled, visible, setVisible, }: Pick< AccountPopoverProps, 'name' | 'expanded' | 'openSidebar' | 'disabled' > & { visible?: boolean setVisible?: React.Dispatch> }) => { useEffect(() => { if (!expanded) { setVisible?.(false) } }, [expanded, setVisible]) return (
{ !expanded && openSidebar?.() }} >
{expanded ? (
{name}
) : null}
{!disabled ? ( ) : null}
) }