index.tsx 3.3 KB

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