|
| 1 | +import classNames from 'classnames' |
| 2 | +import { memo, useMemo, useState } from 'react' |
| 3 | +import { Container as SideBarContainer, SideBarHeader } from './styles' |
| 4 | +import { RIGHTBARITEMKEYS } from '@/constants' |
| 5 | +import { Tooltip } from 'zens' |
| 6 | +import { TableOfContent } from '@/extensions/table-of-content' |
| 7 | + |
| 8 | +function RightBar() { |
| 9 | + const [activeRightBarItemKey, setActiveRightBarItemKey] = useState<RIGHTBARITEMKEYS>( |
| 10 | + RIGHTBARITEMKEYS.TableOfContent, |
| 11 | + ) |
| 12 | + |
| 13 | + const rightBarDataSource: RightBarItem[] = useMemo(() => { |
| 14 | + return [ |
| 15 | + TableOfContent, |
| 16 | + ] |
| 17 | + }, []) |
| 18 | + |
| 19 | + const activeRightBarItem = useMemo(() => { |
| 20 | + const activeItem = rightBarDataSource.find((item) => item.key === activeRightBarItemKey) |
| 21 | + return activeItem |
| 22 | + }, [activeRightBarItemKey, rightBarDataSource]) |
| 23 | + |
| 24 | + const noActiveItem = !activeRightBarItemKey |
| 25 | + |
| 26 | + return ( |
| 27 | + <SideBarContainer noActiveItem={noActiveItem}> |
| 28 | + <div style={{ flex: 1, display: 'flex', flexDirection: 'column', width: '100%' }}> |
| 29 | + <SideBarHeader> |
| 30 | + {rightBarDataSource.map((item) => { |
| 31 | + const cls = classNames('icon', { |
| 32 | + 'app-sidebar-active': activeRightBarItemKey === item.key, |
| 33 | + }) |
| 34 | + |
| 35 | + const handleRightBarItemClick = () => { |
| 36 | + setActiveRightBarItemKey(item.key) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <Tooltip key={item.key} title={item.title}> |
| 41 | + <div className={cls} onClick={handleRightBarItemClick}> |
| 42 | + {item.icon} |
| 43 | + </div> |
| 44 | + </Tooltip> |
| 45 | + ) |
| 46 | + })} |
| 47 | + </SideBarHeader> |
| 48 | + {activeRightBarItem?.components ?? null} |
| 49 | + </div> |
| 50 | + </SideBarContainer> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +export interface RightBarItem { |
| 55 | + title: RIGHTBARITEMKEYS |
| 56 | + key: RIGHTBARITEMKEYS |
| 57 | + icon: React.ReactNode |
| 58 | + components: any |
| 59 | +} |
| 60 | + |
| 61 | +export default memo(RightBar) |
0 commit comments