File size: 5,869 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable import/extensions */
import { XMarkIcon } from '@heroicons/react/20/solid';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { toast } from 'sonner';

import { PasswordStrengthCard } from '@/components/card/PasswordStrengthCard';
import { PasswordInput } from '@/components/input/PasswordInput';
import { ArrowPathIcon } from '@heroicons/react/24/outline';
import clsx from 'clsx';

type EncryptionModalProps = {
  isOpen: boolean;
  onCancel: () => void;
  handleSubmitEncrypt: (password: string) => void;
  auditName: string;
  isGeneratingPDF: boolean;
};

export const EncryptionModal: React.FC<EncryptionModalProps> = ({
  auditName,
  isOpen,
  isGeneratingPDF,
  onCancel,
  handleSubmitEncrypt,
}) => {
  const { t } = useTranslation();
  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');

  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = 'unset';
    }
  }, [isOpen]);

  const generatePassword = () => {
    const charset =
      // eslint-disable-next-line no-secrets/no-secrets
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()';
    const passwordLength = 32;
    let newPassword = '';

    const randomValues = new Uint32Array(passwordLength);
    window.crypto.getRandomValues(randomValues);

    for (let i = 0; i < passwordLength; i++) {
      if (randomValues[i] % 7 === 0) {
        newPassword += '!@#$%^&*()'.charAt(randomValues[i] % 10);
      } else if (randomValues[i] % 3 === 0) {
        newPassword += '0123456789'.charAt(randomValues[i] % 10);
      } else {
        newPassword += charset.charAt(randomValues[i] % charset.length);
      }
    }

    return newPassword;
  };

  const handlePasswordGen = () => {
    const generated = generatePassword();
    setPassword(generated);
    setConfirmPassword(generated);
    void navigator.clipboard.writeText(generated);
    toast.info(t('msg.copiedToClipboard'));
  };

  useEffect(() => {
    setPassword('');
    setConfirmPassword('');
  }, [onCancel]);

  return (
    isOpen && (
      <div className="fixed z-50 inset-0 flex items-center justify-center bg-stone-900 bg-opacity-50">
        <div className="bg-gray-900 rounded-lg shadow-lg p-6 w-full max-w-lg">
          <div className="ml-3 mt-3 flex justify-between items-center mb-2">
            <h2 className="text-xl font-bold text-center text-gray-200">
              {`${t('encryption')} (${auditName})`}
            </h2>
            <button
              className="bg-transparent text-white p-2 rounded mx-3"
              onClick={onCancel}
              type="button"
            >
              <XMarkIcon className="h-6 w-6" />
            </button>
          </div>
          <hr className="h-1 mb-3 bg-gray-600 border-0 rounded" />

          <div className="mb-2 text-gray-200 py-4">
            <div className="flex flex-col">
              <PasswordInput
                disabled={isGeneratingPDF}
                id="password"
                label={t('password')}
                name="password"
                onChange={setPassword}
                placeholder={t('password')}
                value={password}
              />
              <div className="py-2 pr-9">
                <PasswordStrengthCard password={password} />
              </div>
              <PasswordInput
                disabled={isGeneratingPDF}
                id="confirmPassword"
                label={t('confirmPassword')}
                name="confirmPassword"
                onChange={setConfirmPassword}
                placeholder={t('confirmPassword')}
                value={confirmPassword}
              />
            </div>
            <div
              className={
                password === confirmPassword || confirmPassword === ''
                  ? 'flex items-center justify-center text-red-500/75 pt-2 invisible'
                  : 'flex items-center justify-center text-red-500/75 pt-2'
              }
            >
              <p aria-live="polite">{t('err.passwordsDontMatch')}</p>
            </div>
          </div>
          <hr className="h-1 mb-3 bg-gray-600 border-0 rounded" />

          <div className="flex justify-end">
            <button
              className="bg-gray-500 text-white px-4 py-2 rounded mr-2"
              onClick={onCancel}
              type="button"
            >
              {t('btn.cancel')}
            </button>
            <button
              className={clsx('text-white px-4 py-2 rounded mr-2', {
                'bg-green-500': !isGeneratingPDF,
                'bg-green-500/70': isGeneratingPDF,
              })}
              disabled={isGeneratingPDF}
              onClick={handlePasswordGen}
              type="button"
            >
              {t('btn.generatePassword')}
            </button>
            {!isGeneratingPDF ? (
              <button
                className={
                  !(password === confirmPassword) ||
                  password.length < 1 ||
                  confirmPassword.length < 1
                    ? 'bg-blue-500/50 text-white/50 px-4 py-2 rounded'
                    : 'bg-blue-500 text-white px-4 py-2 rounded'
                }
                disabled={
                  !(password === confirmPassword) ||
                  password.length < 1 ||
                  confirmPassword.length < 1
                }
                onClick={() => handleSubmitEncrypt(password)}
                type="button"
              >
                {t('btn.submitEncryption')}
              </button>
            ) : (
              <ArrowPathIcon className="h-8 w-8 animate-spin text-blue-500" />
            )}
          </div>
        </div>
      </div>
    )
  );
};