| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							|  |  |  | import type { ImageFile } from '@/types/app' | 
					
						
							|  |  |  | import { TransferMethod } from '@/types/app' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ImageLinkInputProps = { | 
					
						
							|  |  |  |   onUpload: (imageFile: ImageFile) => void | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   disabled?: boolean | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | const regex = /^(https?|ftp):\/\// | 
					
						
							|  |  |  | const ImageLinkInput: FC<ImageLinkInputProps> = ({ | 
					
						
							|  |  |  |   onUpload, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   disabled, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [imageLink, setImageLink] = useState('') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleClick = () => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     if (disabled) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |     const imageFile = { | 
					
						
							|  |  |  |       type: TransferMethod.remote_url, | 
					
						
							|  |  |  |       _id: `${Date.now()}`, | 
					
						
							|  |  |  |       fileId: '', | 
					
						
							|  |  |  |       progress: regex.test(imageLink) ? 0 : -1, | 
					
						
							|  |  |  |       url: imageLink, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onUpload(imageFile) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className='flex h-8 items-center rounded-lg border border-components-panel-border bg-components-panel-bg pl-1.5 pr-1 shadow-xs'> | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |       <input | 
					
						
							| 
									
										
										
										
											2024-03-10 13:11:41 +08:00
										 |  |  |         type="text" | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         className='mr-0.5 h-[18px] grow appearance-none bg-transparent px-1 text-[13px] text-text-primary outline-none' | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |         value={imageLink} | 
					
						
							|  |  |  |         onChange={e => setImageLink(e.target.value)} | 
					
						
							|  |  |  |         placeholder={t('common.imageUploader.pasteImageLinkInputPlaceholder') || ''} | 
					
						
							|  |  |  |       /> | 
					
						
							|  |  |  |       <Button | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |         variant='primary' | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |         size='small' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         disabled={!imageLink || disabled} | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |         onClick={handleClick} | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         {t('common.operation.ok')} | 
					
						
							|  |  |  |       </Button> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default ImageLinkInput |