All files / app passwordPrompt.tsx

63.33% Statements 19/30
37.5% Branches 6/16
62.5% Functions 5/8
63.33% Lines 19/30

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                              1756x 1756x 1756x 1756x 1756x 1756x 1756x 1756x                     1756x 1756x 10x 10x 10x   10x 10x 10x               1756x                                                     30x         30x                                        
import {Close, Visibility, VisibilityOff} from '@mui/icons-material'
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
  IconButton,
  TextField,
  Typography,
} from '@mui/material'
import {type ChangeEvent, useState, useContext, type KeyboardEvent} from 'react'
import {ManageDictionaries, PasswordEnterer, PasswordPrompter, PasswordResolve} from './building'
 
export function PasswordPrompt() {
  const name = useContext(PasswordEnterer)
  const reply = useContext(PasswordResolve)
  const close = useContext(PasswordPrompter)
  const manageDictionaries = useContext(ManageDictionaries)
  const [failed, setFailed] = useState(false)
  const [hide, setHide] = useState(true)
  let succeeded = false
  const bail = () => {
    setFailed(false)
    setPassword('')
    close('')
    if (!succeeded && !!name) {
      if (name !== 'coarse_sense_map') {
        manageDictionaries({type: 'set', name: 'default'})
      }
    }
    reply('')
  }
  const [password, setPassword] = useState('')
  const submit = () => {
    Eif (password) {
      setFailed(false)
      reply(password)
        .then(() => {
          succeeded = true
          close('')
          setPassword('')
        })
        .catch(() => {
          setPassword('')
          setFailed(true)
        })
    }
  }
  return (
    <Dialog open={!!name} onClose={bail}>
      <DialogTitle>Encrypted Resource</DialogTitle>
      <IconButton
        aria-label="close export menu"
        onClick={bail}
        sx={{
          position: 'absolute',
          right: 8,
          top: 12,
        }}
        className="close-button"
      >
        <Close />
      </IconButton>
      <DialogContent sx={{p: 1}}>
        <Typography sx={{mb: 2}}>
          The <span className="number">{name}</span> resource is encrypted.
        </Typography>
        <TextField
          fullWidth
          label="Password"
          size="small"
          type={hide ? 'password' : 'text'}
          value={password}
          error={failed}
          onKeyDown={(e: KeyboardEvent) => {
            Iif (e.code === 'Enter' || e.code === 'NumpadEnter') {
              submit()
            }
          }}
          onChange={(e: ChangeEvent<HTMLInputElement>) => {
            setPassword(e.target.value)
          }}
        />
        <IconButton
          sx={{position: 'absolute', right: 15}}
          aria-label="toggle password visibility"
          onClick={() => setHide(!hide)}
        >
          {hide ? <VisibilityOff /> : <Visibility />}
        </IconButton>
      </DialogContent>
      <DialogActions sx={{justifyContent: 'space-between'}}>
        <Button onClick={bail}>Cancel</Button>
        <Button variant="contained" onClick={submit} disabled={!password}>
          Decrypt
        </Button>
      </DialogActions>
    </Dialog>
  )
}