'use client' import type { FC } from 'react' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import { useClickAway } from 'ahooks' import Toast from '../../base/toast' import { Plus } from '../../base/icons/src/vender/line/general' import { ChevronDown } from '../../base/icons/src/vender/line/arrows' import examples from './examples' import Button from '@/app/components/base/button' import { importSchemaFromURL } from '@/service/tools' type Props = { onChange: (value: string) => void } const GetSchema: FC = ({ onChange, }) => { const { t } = useTranslation() const [showImportFromUrl, setShowImportFromUrl] = useState(false) const [importUrl, setImportUrl] = useState('') const [isParsing, setIsParsing] = useState(false) const handleImportFromUrl = async () => { if (!importUrl.startsWith('http://') && !importUrl.startsWith('https://')) { Toast.notify({ type: 'error', message: t('tools.createTool.urlError'), }) return } setIsParsing(true) try { const { schema } = await importSchemaFromURL(importUrl) as any setImportUrl('') onChange(schema) } finally { setIsParsing(false) setShowImportFromUrl(false) } } const importURLRef = React.useRef(null) useClickAway(() => { setShowImportFromUrl(false) }, importURLRef) const [showExamples, setShowExamples] = useState(false) const showExamplesRef = React.useRef(null) useClickAway(() => { setShowExamples(false) }, showExamplesRef) return (
{showImportFromUrl && (
setImportUrl(e.target.value)} />
)}
{showExamples && (
{examples.map(item => (
{ onChange(item.content) setShowExamples(false) }} 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' > {t(`tools.createTool.exampleOptions.${item.key}`)}
))}
)}
) } export default React.memo(GetSchema)