index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { useEffect } from 'react'
  2. import { useShallow } from 'zustand/react/shallow'
  3. import { RiLayoutRight2Line } from '@remixicon/react'
  4. import { LayoutRight2LineMod } from '../base/icons/src/public/knowledge'
  5. import NavLink from './navLink'
  6. import type { NavIcon } from './navLink'
  7. import AppBasic from './basic'
  8. import AppInfo from './app-info'
  9. import DatasetInfo from './dataset-info'
  10. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  11. import { useStore as useAppStore } from '@/app/components/app/store'
  12. import cn from '@/utils/classnames'
  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. flex shrink-0 flex-col border-r border-divider-burn bg-background-default-subtle transition-all
  49. ${expand ? 'w-[216px]' : 'w-14'}
  50. `}
  51. >
  52. <div
  53. className={`
  54. shrink-0
  55. ${expand ? 'p-2' : 'p-1'}
  56. `}
  57. >
  58. {iconType === 'app' && (
  59. <AppInfo expand={expand} />
  60. )}
  61. {iconType === 'dataset' && (
  62. <DatasetInfo
  63. name={title}
  64. description={desc}
  65. isExternal={isExternal}
  66. expand={expand}
  67. extraInfo={extraInfo && extraInfo(appSidebarExpand)}
  68. />
  69. )}
  70. {!['app', 'dataset'].includes(iconType) && (
  71. <AppBasic
  72. mode={appSidebarExpand}
  73. iconType={iconType}
  74. icon={icon}
  75. icon_background={icon_background}
  76. name={title}
  77. type={desc}
  78. isExternal={isExternal}
  79. />
  80. )}
  81. </div>
  82. <div className='px-4'>
  83. <div className={cn('mx-auto mt-1 h-[1px] bg-divider-subtle', !expand && 'w-6')} />
  84. </div>
  85. <nav
  86. className={`
  87. grow space-y-1
  88. ${expand ? 'p-4' : 'px-2.5 py-4'}
  89. `}
  90. >
  91. {navigation.map((item, index) => {
  92. return (
  93. <NavLink key={index} mode={appSidebarExpand} iconMap={{ selected: item.selectedIcon, normal: item.icon }} name={item.name} href={item.href} />
  94. )
  95. })}
  96. </nav>
  97. {
  98. !isMobile && (
  99. <div
  100. className={`
  101. shrink-0 py-3
  102. ${expand ? 'px-6' : 'px-4'}
  103. `}
  104. >
  105. <div
  106. className='flex h-6 w-6 cursor-pointer items-center justify-center text-gray-500'
  107. onClick={() => handleToggle(appSidebarExpand)}
  108. >
  109. {
  110. expand
  111. ? <RiLayoutRight2Line className='h-5 w-5 text-components-menu-item-text' />
  112. : <LayoutRight2LineMod className='h-5 w-5 text-components-menu-item-text' />
  113. }
  114. </div>
  115. </div>
  116. )
  117. }
  118. </div>
  119. )
  120. }
  121. export default React.memo(AppDetailNav)