selector.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import useSWR from 'swr'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. PortalToFollowElem,
  7. PortalToFollowElemContent,
  8. PortalToFollowElemTrigger,
  9. } from '@/app/components/base/portal-to-follow-elem'
  10. import {
  11. ArrowUpRight,
  12. ChevronDown,
  13. } from '@/app/components/base/icons/src/vender/line/arrows'
  14. import { Plus } from '@/app/components/base/icons/src/vender/line/general'
  15. import { useModalContext } from '@/context/modal-context'
  16. import { fetchApiBasedExtensionList } from '@/service/common'
  17. type ApiBasedExtensionSelectorProps = {
  18. value: string
  19. onChange: (value: string) => void
  20. }
  21. const ApiBasedExtensionSelector: FC<ApiBasedExtensionSelectorProps> = ({
  22. value,
  23. onChange,
  24. }) => {
  25. const { t } = useTranslation()
  26. const [open, setOpen] = useState(false)
  27. const {
  28. setShowAccountSettingModal,
  29. setShowApiBasedExtensionModal,
  30. } = useModalContext()
  31. const { data, mutate } = useSWR(
  32. '/api-based-extension',
  33. fetchApiBasedExtensionList,
  34. )
  35. const handleSelect = (id: string) => {
  36. onChange(id)
  37. setOpen(false)
  38. }
  39. const currentItem = data?.find(item => item.id === value)
  40. return (
  41. <PortalToFollowElem
  42. open={open}
  43. onOpenChange={setOpen}
  44. placement='bottom-start'
  45. offset={4}
  46. >
  47. <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)} className='w-full'>
  48. {
  49. currentItem
  50. ? (
  51. <div className='flex items-center justify-between pl-3 pr-2.5 h-9 bg-gray-100 rounded-lg cursor-pointer'>
  52. <div className='text-sm text-gray-900'>{currentItem.name}</div>
  53. <div className='flex items-center'>
  54. <div className='mr-1.5 w-[270px] text-xs text-gray-400 truncate text-right'>
  55. {currentItem.api_endpoint}
  56. </div>
  57. <ChevronDown className={`w-4 h-4 text-gray-700 ${!open && 'opacity-60'}`} />
  58. </div>
  59. </div>
  60. )
  61. : (
  62. <div className='flex items-center justify-between pl-3 pr-2.5 h-9 bg-gray-100 rounded-lg text-sm text-gray-400 cursor-pointer'>
  63. {t('common.apiBasedExtension.selector.placeholder')}
  64. <ChevronDown className={`w-4 h-4 text-gray-700 ${!open && 'opacity-60'}`} />
  65. </div>
  66. )
  67. }
  68. </PortalToFollowElemTrigger>
  69. <PortalToFollowElemContent className='w-[calc(100%-32px)] max-w-[576px] z-[102]'>
  70. <div className='w-full rounded-lg border-[0.5px] border-gray-200 bg-white shadow-lg z-10'>
  71. <div className='p-1'>
  72. <div className='flex items-center justify-between px-3 pt-2 pb-1'>
  73. <div className='text-xs font-medium text-gray-500'>
  74. {t('common.apiBasedExtension.selector.title')}
  75. </div>
  76. <div
  77. className='flex items-center text-xs text-primary-600 cursor-pointer'
  78. onClick={() => setShowAccountSettingModal({ payload: 'api-based-extension' })}
  79. >
  80. {t('common.apiBasedExtension.selector.manage')}
  81. <ArrowUpRight className='ml-0.5 w-3 h-3' />
  82. </div>
  83. </div>
  84. <div className='max-h-[250px] overflow-y-auto'>
  85. {
  86. data?.map(item => (
  87. <div
  88. key={item.id}
  89. className='px-3 py-1.5 w-full cursor-pointer hover:bg-gray-50 rounded-md text-left'
  90. onClick={() => handleSelect(item.id!)}
  91. >
  92. <div className='text-sm text-gray-900'>{item.name}</div>
  93. <div className='text-xs text-gray-500'>{item.api_endpoint}</div>
  94. </div>
  95. ))
  96. }
  97. </div>
  98. </div>
  99. <div className='h-[1px] bg-gray-100' />
  100. <div className='p-1'>
  101. <div
  102. className='flex items-center px-3 h-8 text-sm text-primary-600 cursor-pointer'
  103. onClick={() => setShowApiBasedExtensionModal({ payload: {}, onSaveCallback: () => mutate() })}
  104. >
  105. <Plus className='mr-2 w-4 h-4' />
  106. {t('common.operation.add')}
  107. </div>
  108. </div>
  109. </div>
  110. </PortalToFollowElemContent>
  111. </PortalToFollowElem>
  112. )
  113. }
  114. export default ApiBasedExtensionSelector