image-link-input.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. disabled?: boolean
  10. }
  11. const regex = /^(https?|ftp):\/\//
  12. const ImageLinkInput: FC<ImageLinkInputProps> = ({
  13. onUpload,
  14. disabled,
  15. }) => {
  16. const { t } = useTranslation()
  17. const [imageLink, setImageLink] = useState('')
  18. const handleClick = () => {
  19. if (disabled)
  20. return
  21. const imageFile = {
  22. type: TransferMethod.remote_url,
  23. _id: `${Date.now()}`,
  24. fileId: '',
  25. progress: regex.test(imageLink) ? 0 : -1,
  26. url: imageLink,
  27. }
  28. onUpload(imageFile)
  29. }
  30. return (
  31. <div className='flex items-center pl-1.5 pr-1 h-8 border border-gray-200 bg-white shadow-xs rounded-lg'>
  32. <input
  33. type="text"
  34. className='grow mr-0.5 px-1 h-[18px] text-[13px] outline-none appearance-none'
  35. value={imageLink}
  36. onChange={e => setImageLink(e.target.value)}
  37. placeholder={t('common.imageUploader.pasteImageLinkInputPlaceholder') || ''}
  38. />
  39. <Button
  40. variant='primary'
  41. size='small'
  42. disabled={!imageLink || disabled}
  43. onClick={handleClick}
  44. >
  45. {t('common.operation.ok')}
  46. </Button>
  47. </div>
  48. )
  49. }
  50. export default ImageLinkInput