get-schema.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useClickAway } from 'ahooks'
  6. import Toast from '../../base/toast'
  7. import { Plus } from '../../base/icons/src/vender/line/general'
  8. import { ChevronDown } from '../../base/icons/src/vender/line/arrows'
  9. import examples from './examples'
  10. import Button from '@/app/components/base/button'
  11. import { importSchemaFromURL } from '@/service/tools'
  12. type Props = {
  13. onChange: (value: string) => void
  14. }
  15. const GetSchema: FC<Props> = ({
  16. onChange,
  17. }) => {
  18. const { t } = useTranslation()
  19. const [showImportFromUrl, setShowImportFromUrl] = useState(false)
  20. const [importUrl, setImportUrl] = useState('')
  21. const [isParsing, setIsParsing] = useState(false)
  22. const handleImportFromUrl = async () => {
  23. if (!importUrl.startsWith('http://') && !importUrl.startsWith('https://')) {
  24. Toast.notify({
  25. type: 'error',
  26. message: t('tools.createTool.urlError'),
  27. })
  28. return
  29. }
  30. setIsParsing(true)
  31. try {
  32. const { schema } = await importSchemaFromURL(importUrl) as any
  33. setImportUrl('')
  34. onChange(schema)
  35. }
  36. finally {
  37. setIsParsing(false)
  38. setShowImportFromUrl(false)
  39. }
  40. }
  41. const importURLRef = React.useRef(null)
  42. useClickAway(() => {
  43. setShowImportFromUrl(false)
  44. }, importURLRef)
  45. const [showExamples, setShowExamples] = useState(false)
  46. const showExamplesRef = React.useRef(null)
  47. useClickAway(() => {
  48. setShowExamples(false)
  49. }, showExamplesRef)
  50. return (
  51. <div className='flex space-x-1 justify-end relative w-[224px]'>
  52. <div ref={importURLRef}>
  53. <Button
  54. className='flex items-center !h-6 !px-2 space-x-1 '
  55. onClick={() => { setShowImportFromUrl(!showImportFromUrl) }}
  56. >
  57. <Plus className='w-3 h-3' />
  58. <div className='text-xs font-medium text-gray-700'>{t('tools.createTool.importFromUrl')}</div>
  59. </Button>
  60. {showImportFromUrl && (
  61. <div className=' absolute left-[-35px] top-[26px] p-2 rounded-lg border border-gray-200 bg-white shadow-lg'>
  62. <div className='relative'>
  63. <input
  64. type='text'
  65. className='w-[244px] h-8 pl-1.5 pr-[44px] overflow-x-auto border border-gray-200 rounded-lg text-[13px]'
  66. placeholder={t('tools.createTool.importFromUrlPlaceHolder')!}
  67. value={importUrl}
  68. onChange={e => setImportUrl(e.target.value)}
  69. />
  70. <Button
  71. className='absolute top-1 right-1 !h-6 !px-2 text-xs font-medium'
  72. type='primary'
  73. disabled={!importUrl}
  74. onClick={handleImportFromUrl}
  75. loading={isParsing}
  76. >
  77. {isParsing ? '' : t('common.operation.ok')}
  78. </Button>
  79. </div>
  80. </div>
  81. )}
  82. </div>
  83. <div className='relative' ref={showExamplesRef}>
  84. <Button
  85. className='flex items-center !h-6 !px-2 space-x-1'
  86. onClick={() => { setShowExamples(!showExamples) }}
  87. >
  88. <div className='text-xs font-medium text-gray-700'>{t('tools.createTool.examples')}</div>
  89. <ChevronDown className='w-3 h-3' />
  90. </Button>
  91. {showExamples && (
  92. <div className='absolute top-7 right-0 p-1 rounded-lg bg-white shadow-sm'>
  93. {examples.map(item => (
  94. <div
  95. key={item.key}
  96. onClick={() => {
  97. onChange(item.content)
  98. setShowExamples(false)
  99. }}
  100. className='px-3 py-1.5 rounded-lg hover:bg-gray-50 leading-5 text-sm font-normal text-gray-700 cursor-pointer whitespace-nowrap'
  101. >
  102. {t(`tools.createTool.exampleOptions.${item.key}`)}
  103. </div>
  104. ))}
  105. </div>
  106. )}
  107. </div>
  108. </div>
  109. )
  110. }
  111. export default React.memo(GetSchema)