selector.tsx 4.6 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 h-9 cursor-pointer items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2.5'>
  54. <div className='text-sm text-text-primary'>{currentItem.name}</div>
  55. <div className='flex items-center'>
  56. <div className='mr-1.5 w-[270px] truncate text-right text-xs text-text-quaternary'>
  57. {currentItem.api_endpoint}
  58. </div>
  59. <RiArrowDownSLine className={`h-4 w-4 text-text-secondary ${!open && 'opacity-60'}`} />
  60. </div>
  61. </div>
  62. )
  63. : (
  64. <div className='flex h-9 cursor-pointer items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2.5 text-sm text-text-quaternary'>
  65. {t('common.apiBasedExtension.selector.placeholder')}
  66. <RiArrowDownSLine className={`h-4 w-4 text-text-secondary ${!open && 'opacity-60'}`} />
  67. </div>
  68. )
  69. }
  70. </PortalToFollowElemTrigger>
  71. <PortalToFollowElemContent className='z-[102] w-[calc(100%-32px)] max-w-[576px]'>
  72. <div className='z-10 w-full rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-lg'>
  73. <div className='p-1'>
  74. <div className='flex items-center justify-between px-3 pb-1 pt-2'>
  75. <div className='text-xs font-medium text-text-tertiary'>
  76. {t('common.apiBasedExtension.selector.title')}
  77. </div>
  78. <div
  79. className='flex cursor-pointer items-center text-xs text-text-accent'
  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 h-3 w-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='w-full cursor-pointer rounded-md px-3 py-1.5 text-left hover:stroke-state-base-hover'
  95. onClick={() => handleSelect(item.id!)}
  96. >
  97. <div className='text-sm text-text-primary'>{item.name}</div>
  98. <div className='text-xs text-text-tertiary'>{item.api_endpoint}</div>
  99. </div>
  100. ))
  101. }
  102. </div>
  103. </div>
  104. <div className='h-[1px] bg-divider-regular' />
  105. <div className='p-1'>
  106. <div
  107. className='flex h-8 cursor-pointer items-center px-3 text-sm text-text-accent'
  108. onClick={() => {
  109. setOpen(false)
  110. setShowApiBasedExtensionModal({ payload: {}, onSaveCallback: () => mutate() })
  111. }}
  112. >
  113. <RiAddLine className='mr-2 h-4 w-4' />
  114. {t('common.operation.add')}
  115. </div>
  116. </div>
  117. </div>
  118. </PortalToFollowElemContent>
  119. </PortalToFollowElem>
  120. )
  121. }
  122. export default ApiBasedExtensionSelector