ExternalApiSelection.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiAddLine } from '@remixicon/react'
  5. import { useRouter } from 'next/navigation'
  6. import ExternalApiSelect from './ExternalApiSelect'
  7. import Input from '@/app/components/base/input'
  8. import Button from '@/app/components/base/button'
  9. import { useModalContext } from '@/context/modal-context'
  10. import { useExternalKnowledgeApi } from '@/context/external-knowledge-api-context'
  11. type ExternalApiSelectionProps = {
  12. external_knowledge_api_id: string
  13. external_knowledge_id: string
  14. onChange: (data: { external_knowledge_api_id?: string; external_knowledge_id?: string }) => void
  15. }
  16. const ExternalApiSelection: React.FC<ExternalApiSelectionProps> = ({ external_knowledge_api_id, external_knowledge_id, onChange }) => {
  17. const { t } = useTranslation()
  18. const router = useRouter()
  19. const { externalKnowledgeApiList } = useExternalKnowledgeApi()
  20. const [selectedApiId, setSelectedApiId] = useState(external_knowledge_api_id)
  21. const { setShowExternalKnowledgeAPIModal } = useModalContext()
  22. const { mutateExternalKnowledgeApis } = useExternalKnowledgeApi()
  23. const apiItems = externalKnowledgeApiList.map(api => ({
  24. value: api.id,
  25. name: api.name,
  26. url: api.settings.endpoint,
  27. }))
  28. useEffect(() => {
  29. if (apiItems.length > 0) {
  30. const newSelectedId = external_knowledge_api_id || apiItems[0].value
  31. setSelectedApiId(newSelectedId)
  32. if (newSelectedId !== external_knowledge_api_id)
  33. onChange({ external_knowledge_api_id: newSelectedId, external_knowledge_id })
  34. }
  35. }, [apiItems, external_knowledge_api_id, external_knowledge_id, onChange])
  36. const handleAddNewAPI = () => {
  37. setShowExternalKnowledgeAPIModal({
  38. payload: { name: '', settings: { endpoint: '', api_key: '' } },
  39. onSaveCallback: async () => {
  40. mutateExternalKnowledgeApis()
  41. router.refresh()
  42. },
  43. onCancelCallback: () => {
  44. mutateExternalKnowledgeApis()
  45. },
  46. isEditMode: false,
  47. })
  48. }
  49. useEffect(() => {
  50. if (!external_knowledge_api_id && apiItems.length > 0)
  51. onChange({ external_knowledge_api_id: apiItems[0].value, external_knowledge_id })
  52. }, [])
  53. return (
  54. <form className='flex flex-col gap-4 self-stretch'>
  55. <div className='flex flex-col gap-1 self-stretch'>
  56. <div className='flex flex-col self-stretch'>
  57. <label className='text-text-secondary system-sm-semibold'>{t('dataset.externalAPIPanelTitle')}</label>
  58. </div>
  59. {apiItems.length > 0
  60. ? <ExternalApiSelect
  61. items={apiItems}
  62. value={selectedApiId}
  63. onSelect={(e) => {
  64. setSelectedApiId(e.value)
  65. onChange({ external_knowledge_api_id: e.value, external_knowledge_id })
  66. }}
  67. />
  68. : <Button variant={'tertiary'} onClick={handleAddNewAPI} className='justify-start gap-0.5'>
  69. <RiAddLine className='w-4 h-4 text-text-tertiary' />
  70. <span className='text-text-tertiary system-sm-regular'>{t('dataset.noExternalKnowledge')}</span>
  71. </Button>
  72. }
  73. </div>
  74. <div className='flex flex-col gap-1 self-stretch'>
  75. <div className='flex flex-col self-stretch'>
  76. <label className='text-text-secondary system-sm-semibold'>{t('dataset.externalKnowledgeId')}</label>
  77. </div>
  78. <Input
  79. value={external_knowledge_id}
  80. onChange={e => onChange({ external_knowledge_id: e.target.value, external_knowledge_api_id })}
  81. placeholder={t('dataset.externalKnowledgeIdPlaceholder') ?? ''}
  82. />
  83. </div>
  84. </form>
  85. )
  86. }
  87. export default ExternalApiSelection