All files / src/components DeviceCodeEntry.tsx

100% Statements 16/16
90% Branches 9/10
100% Functions 3/3
100% Lines 16/16

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        13x 13x 13x 13x     2x 2x 2x   2x 2x 1x   1x   2x       13x 1x             12x                                     3x                                
import { useState, FormEvent } from 'react'
import { api } from '../api/client'
 
export default function DeviceCodeEntry() {
  const [code, setCode] = useState('')
  const [error, setError] = useState('')
  const [success, setSuccess] = useState(false)
  const [submitting, setSubmitting] = useState(false)
 
  async function handleSubmit(e: FormEvent) {
    e.preventDefault()
    setError('')
    setSubmitting(true)
 
    try {
      await api.device.authorize(code)
      setSuccess(true)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Authorization failed')
    } finally {
      setSubmitting(false)
    }
  }
 
  if (success) {
    return (
      <div className="rounded-md bg-green-50 p-4 text-center text-sm text-green-800 dark:bg-green-900/20 dark:text-green-300">
        Device authorized successfully! The other device should now be connected.
      </div>
    )
  }
 
  return (
    <form onSubmit={handleSubmit} className="space-y-4">
      {error && (
        <div role="alert" className="rounded-md bg-red-50 p-3 text-sm text-red-800 dark:bg-red-900/20 dark:text-red-300">
          {error}
        </div>
      )}
 
      <div>
        <label htmlFor="device-code" className="block text-sm font-medium text-foreground">
          Enter the code shown on the other device
        </label>
        <input
          id="device-code"
          type="text"
          maxLength={6}
          required
          autoComplete="off"
          value={code}
          onChange={(e) => setCode(e.target.value.toUpperCase())}
          placeholder="ABC123"
          className="mt-1 block w-full rounded-md border border-input bg-background px-3 py-2 text-center font-mono text-2xl tracking-widest text-foreground shadow-sm focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary"
        />
      </div>
 
      <button
        type="submit"
        disabled={submitting || code.length < 6}
        className="w-full rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 disabled:opacity-50"
      >
        {submitting ? 'Authorizing...' : 'Authorize Device'}
      </button>
    </form>
  )
}
 
← Back to Dashboard