import { UnfoldLess, UnfoldMore } from '@mui/icons-material' import clsx from 'clsx' import { useRouter } from 'next/router' import { ComponentType, ReactNode, useEffect, useState } from 'react' import { LinkWrapperProps, RowProps } from '@dao-dao/types' import { IconButton } from '../icon_buttons' import { Loader } from '../logo/Loader' import { Tooltip } from '../tooltip/Tooltip' export const Row = ({ Icon: _Icon, label, expandedLocalStorageKey, showBadge, onClick, children, rightNode, href, defaultExpanded = false, forceExpanded = false, hideExpand = false, compact = false, loading = false, selected = false, LinkWrapper, shallow, containerClassName, contentContainerClassName, }: RowProps) => { const { asPath } = useRouter() const [expanded, setExpanded] = useState( forceExpanded || (expandedLocalStorageKey && typeof localStorage !== 'undefined' ? localStorage.getItem(expandedLocalStorageKey) === '1' : defaultExpanded) ) useEffect(() => { if (forceExpanded) { return } if (expandedLocalStorageKey && typeof localStorage !== 'undefined') { localStorage.setItem(expandedLocalStorageKey, Number(expanded).toString()) } }, [expanded, expandedLocalStorageKey, forceExpanded]) const Icon = _Icon || ((props) =>
) const ExpandButton = expanded ? UnfoldLess : UnfoldMore // If no onClick or href, and has expandable children, set onClick to toggle // expanded. if (!onClick && !href && children && !loading) { onClick = () => setExpanded((e) => !e) } return compact ? (
{showBadge && (
)}
{children}
) : (
{showBadge && (
)}

{label}

{/* Override expand button with loader. */} {loading ? ( ) : // If children exist and not forcing expanded, display expand button. children && !forceExpanded && !hideExpand ? ( { event.stopPropagation() event.preventDefault() setExpanded((e) => !e) }} size="sm" variant="ghost" /> ) : ( // If children do not exist but rightNode does, display it. rightNode )}
{children && !hideExpand && ( // Load in background even when hidden.
{children}
)}
) } interface RowWrapperProps { LinkWrapper: ComponentType shallow?: boolean href?: string children: ReactNode } const RowWrapper = ({ LinkWrapper, children, href, shallow, }: RowWrapperProps) => href ? ( {children} ) : ( <>{children} )