ExternalApiSelect.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { useEffect, useState } from 'react'
  2. import {
  3. RiAddLine,
  4. RiArrowDownSLine,
  5. } from '@remixicon/react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useRouter } from 'next/navigation'
  8. import { ApiConnectionMod } from '@/app/components/base/icons/src/vender/solid/development'
  9. import { useModalContext } from '@/context/modal-context'
  10. import { useExternalKnowledgeApi } from '@/context/external-knowledge-api-context'
  11. type ApiItem = {
  12. value: string
  13. name: string
  14. url: string
  15. }
  16. type ExternalApiSelectProps = {
  17. items: ApiItem[]
  18. value?: string
  19. onSelect: (item: ApiItem) => void
  20. }
  21. const ExternalApiSelect: React.FC<ExternalApiSelectProps> = ({ items, value, onSelect }) => {
  22. const { t } = useTranslation()
  23. const [isOpen, setIsOpen] = useState(false)
  24. const [selectedItem, setSelectedItem] = useState<ApiItem | null>(
  25. items.find(item => item.value === value) || null,
  26. )
  27. const { setShowExternalKnowledgeAPIModal } = useModalContext()
  28. const { mutateExternalKnowledgeApis } = useExternalKnowledgeApi()
  29. const router = useRouter()
  30. useEffect(() => {
  31. const newSelectedItem = items.find(item => item.value === value) || null
  32. setSelectedItem(newSelectedItem)
  33. }, [value, items])
  34. const handleAddNewAPI = () => {
  35. setShowExternalKnowledgeAPIModal({
  36. payload: { name: '', settings: { endpoint: '', api_key: '' } },
  37. onSaveCallback: async () => {
  38. mutateExternalKnowledgeApis()
  39. router.refresh()
  40. },
  41. onCancelCallback: () => {
  42. mutateExternalKnowledgeApis()
  43. },
  44. isEditMode: false,
  45. })
  46. }
  47. const handleSelect = (item: ApiItem) => {
  48. setSelectedItem(item)
  49. onSelect(item)
  50. setIsOpen(false)
  51. }
  52. return (
  53. <div className="relative w-full">
  54. <div
  55. className={`flex items-center justify-between cursor-pointer px-2 py-1 gap-0.5 self-stretch rounded-lg
  56. bg-components-input-bg-normal hover:bg-state-base-hover-alt ${isOpen && 'bg-state-base-hover-alt'}`}
  57. onClick={() => setIsOpen(!isOpen)}
  58. >
  59. {selectedItem
  60. ? (
  61. <div className="flex p-1 items-center gap-2 self-stretch rounded-lg">
  62. <ApiConnectionMod className='text-text-secondary w-4 h-4' />
  63. <div className='flex items-center flex-grow'>
  64. <span className='text-components-input-text-filled text-ellipsis system-sm-regular overflow-hidden'>{selectedItem.name}</span>
  65. </div>
  66. </div>
  67. )
  68. : (
  69. <span className='text-components-input-text-placeholder system-sm-regular'>{t('dataset.selectExternalKnowledgeAPI.placeholder')}</span>
  70. )}
  71. <RiArrowDownSLine className={`w-4 h-4 text-text-quaternary transition-transform ${isOpen ? 'text-text-secondary' : ''}`} />
  72. </div>
  73. {isOpen && (
  74. <div className="absolute z-10 w-full mt-1 bg-components-panel-bg-blur border rounded-xl shadow-lg">
  75. {items.map(item => (
  76. <div
  77. key={item.value}
  78. className="flex p-1 items-center cursor-pointer"
  79. onClick={() => handleSelect(item)}
  80. >
  81. <div className="flex p-2 items-center gap-2 self-stretch rounded-lg hover:bg-state-base-hover w-full">
  82. <ApiConnectionMod className='text-text-secondary w-4 h-4' />
  83. <span className='text-text-secondary text-ellipsis system-sm-medium overflow-hidden flex-grow'>{item.name}</span>
  84. <span className='text-text-tertiary overflow-hidden text-right text-ellipsis system-xs-regular'>{item.url}</span>
  85. </div>
  86. </div>
  87. ))}
  88. <div className='flex p-1 flex-col items-start self-stretch'>
  89. <div
  90. className='flex p-2 items-center gap-2 self-stretch rounded-lg cursor-pointer hover:bg-state-base-hover'
  91. onClick={handleAddNewAPI}
  92. >
  93. <RiAddLine className='text-text-secondary w-4 h-4' />
  94. <span className='flex-grow overflow-hidden text-text-secondary text-ellipsis system-sm-medium'>{t('dataset.createNewExternalAPI')}</span>
  95. </div>
  96. </div>
  97. </div>
  98. )}
  99. </div>
  100. )
  101. }
  102. export default ExternalApiSelect