| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import React, { useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useClickAway } from 'ahooks' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   RiAddLine, | 
					
						
							|  |  |  |   RiArrowDownSLine, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import Toast from '../../base/toast' | 
					
						
							|  |  |  | import examples from './examples' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import Input from '@/app/components/base/input' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import { importSchemaFromURL } from '@/service/tools' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Props = { | 
					
						
							|  |  |  |   onChange: (value: string) => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const GetSchema: FC<Props> = ({ | 
					
						
							|  |  |  |   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 ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className='relative flex w-[224px] justify-end space-x-1'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       <div ref={importURLRef}> | 
					
						
							|  |  |  |         <Button | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |           size='small' | 
					
						
							|  |  |  |           className='space-x-1 ' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |           onClick={() => { setShowImportFromUrl(!showImportFromUrl) }} | 
					
						
							|  |  |  |         > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <RiAddLine className='h-3 w-3' /> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           <div className='system-xs-medium text-text-secondary'>{t('tools.createTool.importFromUrl')}</div> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         </Button> | 
					
						
							|  |  |  |         {showImportFromUrl && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className=' absolute left-[-35px] top-[26px] rounded-lg border border-components-panel-border bg-components-panel-bg p-2 shadow-lg'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             <div className='relative'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |               <Input | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                 type='text' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 className='w-[244px]' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                 placeholder={t('tools.createTool.importFromUrlPlaceHolder')!} | 
					
						
							|  |  |  |                 value={importUrl} | 
					
						
							|  |  |  |                 onChange={e => setImportUrl(e.target.value)} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |               <Button | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 className='absolute right-1 top-1' | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |                 size='small' | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |                 variant='primary' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                 disabled={!importUrl} | 
					
						
							|  |  |  |                 onClick={handleImportFromUrl} | 
					
						
							|  |  |  |                 loading={isParsing} | 
					
						
							|  |  |  |               > | 
					
						
							|  |  |  |                 {isParsing ? '' : t('common.operation.ok')} | 
					
						
							|  |  |  |               </Button> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-11-07 18:10:41 +08:00
										 |  |  |       <div className='relative -mt-0.5' ref={showExamplesRef}> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         <Button | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |           size='small' | 
					
						
							|  |  |  |           className='space-x-1' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |           onClick={() => { setShowExamples(!showExamples) }} | 
					
						
							|  |  |  |         > | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           <div className='system-xs-medium text-text-secondary'>{t('tools.createTool.examples')}</div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <RiArrowDownSLine className='h-3 w-3' /> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         </Button> | 
					
						
							|  |  |  |         {showExamples && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='absolute right-0 top-7 rounded-lg bg-components-panel-bg p-1 shadow-sm'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             {examples.map(item => ( | 
					
						
							|  |  |  |               <div | 
					
						
							|  |  |  |                 key={item.key} | 
					
						
							|  |  |  |                 onClick={() => { | 
					
						
							|  |  |  |                   onChange(item.content) | 
					
						
							|  |  |  |                   setShowExamples(false) | 
					
						
							|  |  |  |                 }} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 className='system-sm-regular cursor-pointer whitespace-nowrap rounded-lg px-3 py-1.5 leading-5 text-text-secondary hover:bg-components-panel-on-panel-item-bg-hover' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |               > | 
					
						
							|  |  |  |                 {t(`tools.createTool.exampleOptions.${item.key}`)} | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ))} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export default React.memo(GetSchema) |