index.tsx 3.4 KB

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