image-link-input.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Button from '@/app/components/base/button'
  5. import type { ImageFile } from '@/types/app'
  6. import { TransferMethod } from '@/types/app'
  7. type ImageLinkInputProps = {
  8. onUpload: (imageFile: ImageFile) => void
  9. }
  10. const regex = /^(https?|ftp):\/\//
  11. const ImageLinkInput: FC<ImageLinkInputProps> = ({
  12. onUpload,
  13. }) => {
  14. const { t } = useTranslation()
  15. const [imageLink, setImageLink] = useState('')
  16. const handleClick = () => {
  17. const imageFile = {
  18. type: TransferMethod.remote_url,
  19. _id: `${Date.now()}`,
  20. fileId: '',
  21. progress: regex.test(imageLink) ? 0 : -1,
  22. url: imageLink,
  23. }
  24. onUpload(imageFile)
  25. }
  26. return (
  27. <div className='flex items-center pl-1.5 pr-1 h-8 border border-gray-200 bg-white shadow-xs rounded-lg'>
  28. <input
  29. className='grow mr-0.5 px-1 h-[18px] text-[13px] outline-none appearance-none'
  30. value={imageLink}
  31. onChange={e => setImageLink(e.target.value)}
  32. placeholder={t('common.imageUploader.pasteImageLinkInputPlaceholder') || ''}
  33. />
  34. <Button
  35. type='primary'
  36. className='!h-6 text-xs font-medium'
  37. disabled={!imageLink}
  38. onClick={handleClick}
  39. >
  40. {t('common.operation.ok')}
  41. </Button>
  42. </div>
  43. )
  44. }
  45. export default ImageLinkInput