index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use client'
  2. import { useCallback } from 'react'
  3. import { ChevronDownIcon, PlusIcon } from '@heroicons/react/24/solid'
  4. import { Menu } from '@headlessui/react'
  5. import { useRouter } from 'next/navigation'
  6. import { debounce } from 'lodash-es'
  7. import Indicator from '../../indicator'
  8. import AppIcon from '@/app/components/base/app-icon'
  9. import { useAppContext } from '@/context/app-context'
  10. type NavItem = {
  11. id: string
  12. name: string
  13. link: string
  14. icon: string
  15. icon_background: string
  16. }
  17. export type INavSelectorProps = {
  18. navs: NavItem[]
  19. curNav?: Omit<NavItem, 'link'>
  20. createText: string
  21. onCreate: () => void
  22. onLoadmore?: () => void
  23. }
  24. const itemClassName = `
  25. flex items-center w-full h-10 px-3 text-gray-700 text-[14px]
  26. rounded-lg font-normal hover:bg-gray-100 cursor-pointer truncate
  27. `
  28. const NavSelector = ({ curNav, navs, createText, onCreate, onLoadmore }: INavSelectorProps) => {
  29. const router = useRouter()
  30. const { isCurrentWorkspaceManager } = useAppContext()
  31. const handleScroll = useCallback(debounce((e) => {
  32. if (typeof onLoadmore === 'function') {
  33. const { clientHeight, scrollHeight, scrollTop } = e.target
  34. if (clientHeight + scrollTop > scrollHeight - 50)
  35. onLoadmore()
  36. }
  37. }, 50), [])
  38. return (
  39. <div className="">
  40. <Menu as="div" className="relative inline-block text-left">
  41. <div>
  42. <Menu.Button
  43. className="
  44. inline-flex items-center w-full h-7 justify-center
  45. rounded-[10px] pl-2 pr-2.5 text-[14px] font-semibold
  46. text-[#1C64F2] hover:bg-[#EBF5FF]
  47. "
  48. >
  49. <div className='max-w-[180px] truncate' title={curNav?.name}>{curNav?.name}</div>
  50. <ChevronDownIcon
  51. className="shrink-0 w-3 h-3 ml-1"
  52. aria-hidden="true"
  53. />
  54. </Menu.Button>
  55. </div>
  56. <Menu.Items
  57. className="
  58. absolute -left-11 right-0 mt-1.5 w-60 max-w-80
  59. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  60. shadow-lg
  61. "
  62. >
  63. <div className="px-1 py-1 overflow-auto" style={{ maxHeight: '50vh' }} onScroll={handleScroll}>
  64. {
  65. navs.map(nav => (
  66. <Menu.Item key={nav.id}>
  67. <div className={itemClassName} onClick={() => router.push(nav.link)} title={nav.name}>
  68. <div className='relative w-6 h-6 mr-2 bg-[#D5F5F6] rounded-[6px]'>
  69. <AppIcon size='tiny' icon={nav.icon} background={nav.icon_background}/>
  70. <div className='flex justify-center items-center absolute -right-0.5 -bottom-0.5 w-2.5 h-2.5 bg-white rounded'>
  71. <Indicator />
  72. </div>
  73. </div>
  74. {nav.name}
  75. </div>
  76. </Menu.Item>
  77. ))
  78. }
  79. </div>
  80. {isCurrentWorkspaceManager && <Menu.Item>
  81. <div className='p-1' onClick={onCreate}>
  82. <div
  83. className='flex items-center h-12 rounded-lg cursor-pointer hover:bg-gray-100'
  84. >
  85. <div
  86. className='
  87. flex justify-center items-center
  88. ml-4 mr-2 w-6 h-6 bg-gray-100 rounded-[6px]
  89. border-[0.5px] border-gray-200 border-dashed
  90. '
  91. >
  92. <PlusIcon className='w-4 h-4 text-gray-500' />
  93. </div>
  94. <div className='font-normal text-[14px] text-gray-700'>{createText}</div>
  95. </div>
  96. </div>
  97. </Menu.Item>}
  98. </Menu.Items>
  99. </Menu>
  100. </div>
  101. )
  102. }
  103. export default NavSelector