index.tsx 3.2 KB

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