get-schema.tsx 3.9 KB

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