image-link-input.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 h-8 items-center rounded-lg border border-components-panel-border bg-components-panel-bg pl-1.5 pr-1 shadow-xs'>
  32. <input
  33. type="text"
  34. className='mr-0.5 h-[18px] grow appearance-none bg-transparent px-1 text-[13px] text-text-primary outline-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