import { ArrowRightIcon, CaretDownIcon, CaretRightIcon } from '@bnb-chain/icons'; import { Box, Flex, useColorMode, Button, Divider } from '@chakra-ui/react'; import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { theme } from '../../../../modules/theme'; import { Typography } from '../../../Typography'; import { ItemWithIcon } from '../../../Header/components/ItemWithIcon'; import { ItemWithIconWarning } from '../../../Header/components/ItemWithIconWarning'; import { Level1Props, Level2Props } from '../../../Header/types'; // import { SubItemMainTopWithImage } from '../../components/SubItemMainTopWithImage'; import { SubItemMainTop } from '../../components/SubItemMainTop'; import { LEVEL1_PADDING_TOP, MENU_WIDTH, MENU_WIDTH_PX, TRANSITION } from '../../constants'; import { AddNetwork } from '../../components/AddNetwork'; interface Level1ContentProps { level1: Level1Props; level1Index: number; } interface LevelProps { level2: Level2Props; level1Index: number; level2Index: number; } interface NumberProps { [key: number]: number; } let level2Height: NumberProps = {} as NumberProps; export const Level1List = ({ data }: { data: Level1ContentProps }) => { const { colorMode } = useColorMode(); const canUseDOM = typeof window !== 'undefined'; const useIsomorphicLayoutEffect = canUseDOM ? useLayoutEffect : useEffect; const { level1, level1Index } = data; const [level2Data, setLevel2] = useState({} as LevelProps); const containerRef = useRef(null); const flexRef = useRef(null); const level2ElRef = useRef(null); const level3ElRef = useRef(null); const level3ElWrapRef = useRef(null); const level3Data = useMemo(() => { const data: Level2Props['children'] = []; level1.children?.forEach((level2) => { data.push(...(level2.children || [])); }); return data; }, [level1]); useIsomorphicLayoutEffect(() => { let level2 = level2Data.level2; if (!level2) return; let level1Index = level2Data.level1Index; let level2Index = level2Data.level2Index; if (!level2Height[level1Index]) return; if (level2?.children) { level2ElRef.current?.classList.add('hover'); } else { level2ElRef.current?.classList.remove('hover'); } const hover = level2ElRef.current?.querySelector('.hover'); const item = level2ElRef.current?.querySelector(`.accordion__level2-item-${level2Index}`); hover?.classList.remove('hover'); item?.classList.add('hover'); //64: Add network to wallet, 16: paddingY, 80: submenu top const childrenNum = level2?.children?.length ? level2?.children?.length : 0; const estimateHeight = childrenNum * 60 + childrenNum * 4 + 64 + 80 + 16; let maxHeight = typeof level3ElWrapRef.current?.clientHeight === 'number' ? level3ElWrapRef.current?.clientHeight > level2Height[level1Index] ? level3ElWrapRef.current?.clientHeight : level2Height[level1Index] : level2Height[level1Index]; const customHeight = maxHeight - estimateHeight > 0 ? maxHeight : estimateHeight; const level2ClientWith = typeof level2ElRef.current?.clientWidth === 'number' ? level2ElRef.current?.clientWidth : 0; const level3ClientWidth = typeof level3ElRef.current?.clientWidth === 'number' ? level3ElRef.current?.clientWidth : 0; let maxWidth = level3ClientWidth + level2ClientWith + 'px'; flexRef.current && (flexRef.current.style.height = customHeight + LEVEL1_PADDING_TOP + 'px'); flexRef.current && (flexRef.current.style.width = maxWidth); level2ElRef.current && (level2ElRef.current.style.height = customHeight + 'px'); level3ElRef.current && (level3ElRef.current.style.height = customHeight + 'px'); }, [level2Data]); return ( { // setTimeout(() => { // Fix the issue of menu modal disappearing when moving the mouse quickly left and right. level2Height = { ...level2Height, [level1Index]: level2ElRef.current?.clientHeight ?? 0, }; const level2ClientHeight = typeof level2ElRef.current?.clientHeight === 'number' ? level2ElRef.current?.clientHeight : 0; flexRef.current && (flexRef.current.style.height = LEVEL1_PADDING_TOP + level2ClientHeight + 'px'); flexRef.current && (flexRef.current.style.width = MENU_WIDTH_PX); level3ElRef.current && (level3ElRef.current.style.minHeight = level2ClientHeight + 'px'); // }, 0); }} onMouseLeave={() => { containerRef.current?.querySelectorAll('.hover')?.forEach((item) => { item.classList.remove('hover'); }); level2ElRef.current && (level2ElRef.current.style.height = level2Height[level1Index] + 'px'); flexRef.current && (flexRef.current.style.height = LEVEL1_PADDING_TOP + level2Height[level1Index] + 'px'); flexRef.current && (flexRef.current.style.width = MENU_WIDTH_PX); level2ElRef.current?.classList.remove('hover'); }} _hover={{ '.accordion__level2-level3-container': { display: 'inline-block', }, '.accordion__level1-each-item': { p: { color: theme.colors[colorMode].button.brand.default }, svg: { color: theme.colors[colorMode].button.brand.default, }, }, }} className={`accordion__level1-container-${level1Index}`} sx={{ '>div:hover': { '.accordion__level1-each-item': { p: { color: theme.colors[colorMode].button.brand.default }, '.accordion--caretdown-icon': { svg: { color: theme.colors[colorMode].button.brand.default, }, }, }, }, }} data-analytics-id={level1.analyticsId} > {level1.name} {/* {level1.title && ( { const hover = level2ElRef.current?.querySelector('.hover'); hover?.classList.remove('hover'); }} /> )} */} {!!level1.children && level1.children.map((level2, level2Index) => { return ( { setLevel2({ level2, level1Index, level2Index, }); }} sx={{ '&.hover': { '.accordion__level2-flex-item': { bg: theme.colors[colorMode].background['2'], '.accordion__caretright-icon': { color: theme.colors[colorMode].button.primary.default, }, }, '.level2-inner-item': { bg: theme.colors[colorMode].border['2'], borderRadius: theme.sizes['2'], }, }, }} > {!!level2.children && ( )} ); })} {!!level1.button && ( )} {level2Data.level2?.children && ( {level2Data.level2?.title && ( { const hover = level2ElRef.current?.querySelector('.hover'); hover?.classList.remove('hover'); }} analyticsId={level2Data.level2?.thumbnailAnalyticsId ?? ''} /> )} {!!level2Data.level2.children && level2Data.level2?.children.map((level3, level3Index) => { if ( level1.key === 'Developers' && level2Data.level2?.buttonBottom && typeof level2Data.level2?.children?.length === 'number' && level3Index === level2Data.level2?.children?.length - 1 ) { return ( {level3.name} ); } else { return ( {level3.analyticsId === 'click_menu_solution_beacon_fusion' ? ( ) : ( {level3.tag ? ( ) : ( )} )} ); } })} {level2Data.level2.wallet && ( )} )} {level3Data.map((level3, index) => ( {level3.name} ))} ); };