selector.tsx 4.5 KB

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