index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useCallback, useState } from 'react'
  2. import NavLink from './navLink'
  3. import type { NavIcon } from './navLink'
  4. import AppBasic from './basic'
  5. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  6. import {
  7. AlignLeft01,
  8. AlignRight01,
  9. } from '@/app/components/base/icons/src/vender/line/layout'
  10. export type IAppDetailNavProps = {
  11. iconType?: 'app' | 'dataset' | 'notion'
  12. title: string
  13. desc: string
  14. icon: string
  15. icon_background: string
  16. navigation: Array<{
  17. name: string
  18. href: string
  19. icon: NavIcon
  20. selectedIcon: NavIcon
  21. }>
  22. extraInfo?: React.ReactNode
  23. }
  24. const AppDetailNav = ({ title, desc, icon, icon_background, navigation, extraInfo, iconType = 'app' }: IAppDetailNavProps) => {
  25. const localeMode = localStorage.getItem('app-detail-collapse-or-expand') || 'expand'
  26. const media = useBreakpoints()
  27. const isMobile = media === MediaType.mobile
  28. const mode = isMobile ? 'collapse' : 'expand'
  29. const [modeState, setModeState] = useState(isMobile ? mode : localeMode)
  30. const expand = modeState === 'expand'
  31. const handleToggle = useCallback(() => {
  32. setModeState((prev) => {
  33. const next = prev === 'expand' ? 'collapse' : 'expand'
  34. localStorage.setItem('app-detail-collapse-or-expand', next)
  35. return next
  36. })
  37. }, [])
  38. return (
  39. <div
  40. className={`
  41. shrink-0 flex flex-col bg-white border-r border-gray-200 transition-all
  42. ${expand ? 'w-[216px]' : 'w-14'}
  43. `}
  44. >
  45. <div
  46. className={`
  47. shrink-0
  48. ${expand ? 'p-4' : 'p-2'}
  49. `}
  50. >
  51. <AppBasic
  52. mode={modeState}
  53. iconType={iconType}
  54. icon={icon}
  55. icon_background={icon_background}
  56. name={title}
  57. type={desc}
  58. />
  59. </div>
  60. <nav
  61. className={`
  62. grow space-y-1 bg-white
  63. ${expand ? 'p-4' : 'px-2.5 py-4'}
  64. `}
  65. >
  66. {navigation.map((item, index) => {
  67. return (
  68. <NavLink key={index} mode={modeState} iconMap={{ selected: item.selectedIcon, normal: item.icon }} name={item.name} href={item.href} />
  69. )
  70. })}
  71. {extraInfo ?? null}
  72. </nav>
  73. {
  74. !isMobile && (
  75. <div
  76. className={`
  77. shrink-0 py-3
  78. ${expand ? 'px-6' : 'px-4'}
  79. `}
  80. >
  81. <div
  82. className='flex items-center justify-center w-6 h-6 text-gray-500 cursor-pointer'
  83. onClick={handleToggle}
  84. >
  85. {
  86. expand
  87. ? <AlignLeft01 className='w-[14px] h-[14px]' />
  88. : <AlignRight01 className='w-[14px] h-[14px]' />
  89. }
  90. </div>
  91. </div>
  92. )
  93. }
  94. </div>
  95. )
  96. }
  97. export default React.memo(AppDetailNav)