All files / src/features/vaccines/components VaccineProofField.tsx

90% Statements 36/40
85% Branches 17/20
66.66% Functions 4/6
94.59% Lines 35/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                              50x 50x 50x 50x   5x 5x 5x 2x 2x   3x 3x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 1x   2x     2x   3x 3x     50x                               1x                           5x 5x 5x                                                    
import { useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
 
export function VaccineProofField({
  value,
  onChange,
  onProcessingChange,
}: {
  value: string
  onChange: (value: string) => void
  onProcessingChange?: (busy: boolean) => void
}) {
  const { t } = useTranslation()
  const input = useRef<HTMLInputElement>(null)
  const [busy, setBusy] = useState(false)
  const [error, setError] = useState(false)
  async function select(file?: File) {
    Iif (!file) return
    setError(false)
    if (!file.type.startsWith('image/') || file.size > 10 * 1024 * 1024) {
      setError(true)
      return
    }
    setBusy(true)
    onProcessingChange?.(true)
    try {
      const bitmap = await createImageBitmap(file)
      try {
        const scale = Math.min(1, 1600 / Math.max(bitmap.width, bitmap.height))
        const canvas = document.createElement('canvas')
        canvas.width = Math.round(bitmap.width * scale)
        canvas.height = Math.round(bitmap.height * scale)
        const context = canvas.getContext('2d')
        Iif (!context) throw new Error('canvas-unavailable')
        context.fillStyle = '#fff'
        context.fillRect(0, 0, canvas.width, canvas.height)
        context.drawImage(bitmap, 0, 0, canvas.width, canvas.height)
        const url = canvas.toDataURL('image/jpeg', 0.85)
        // Leave room for the other fields within the API's JSON body limit.
        if (url.length > 700_000) throw new Error('image-too-large')
        onChange(url)
      } finally {
        bitmap.close()
      }
    } catch {
      setError(true)
    } finally {
      setBusy(false)
      onProcessingChange?.(false)
    }
  }
  return (
    <div className="rounded-2xl border border-dashed border-border p-4">
      <p className="text-sm font-bold">{t('vaccines.editor.proof')}</p>
      <p className="mt-1 text-xs leading-relaxed text-ink-muted">{t('vaccines.editor.proofHint')}</p>
      <div className="mt-3 flex flex-wrap items-center gap-3">
        {value && (
          <img
            src={value}
            alt={t('vaccines.editor.proofPreview')}
            className="size-16 rounded-lg border border-border object-cover"
          />
        )}
        <Button type="button" variant="outline" disabled={busy} onClick={() => input.current?.click()}>
          {t(busy ? 'vaccines.editor.processing' : 'vaccines.editor.choosePhoto')}
        </Button>
        {value && (
          <Button type="button" variant="ghost" disabled={busy} onClick={() => onChange('')}>
            {t('vaccines.editor.removePhoto')}
          </Button>
        )}
      </div>
      <input
        ref={input}
        type="file"
        accept="image/*"
        className="sr-only"
        aria-label={t('vaccines.editor.choosePhoto')}
        tabIndex={-1}
        disabled={busy}
        onChange={(event) => {
          const file = event.target.files?.[0]
          event.target.value = ''
          void select(file)
        }}
      />
      {error && (
        <p role="alert" className="mt-2 text-sm text-destructive">
          {t('vaccines.editor.proofError')}
        </p>
      )}
      <details className="mt-3">
        <summary className="cursor-pointer text-xs text-ink-muted">{t('vaccines.editor.useLink')}</summary>
        <Label htmlFor="photoUrl" className="sr-only">
          {t('vaccines.applicationDetails.photoUrlLabel')}
        </Label>
        <Input
          id="photoUrl"
          type="url"
          disabled={busy}
          value={value.startsWith('data:') ? '' : value}
          onChange={(event) => onChange(event.target.value)}
          className="mt-2"
          placeholder="https://"
        />
      </details>
    </div>
  )
}