Skip to content

Commit 94f117f

Browse files
committed
refactor
1 parent ee8bcf3 commit 94f117f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

resources/js/components/two-factor-setup-modal.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { confirm } from '@/routes/two-factor';
66
import { Form } from '@inertiajs/react';
77
import { useClipboard } from '@reactuses/core';
88
import { REGEXP_ONLY_DIGITS } from 'input-otp';
9-
import { Check, Copy, Loader2, ScanLine } from 'lucide-react';
9+
import { Check, Copy, Loader2, LucideIcon, ScanLine } from 'lucide-react';
1010
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1111

12+
const OTP_MAX_LENGTH = 6;
13+
1214
interface TwoFactorSetupModalProps {
1315
isOpen: boolean;
1416
onClose: () => void;
@@ -31,7 +33,6 @@ export default function TwoFactorSetupModal({
3133
fetchSetupData,
3234
}: TwoFactorSetupModalProps) {
3335
const [copiedText, copy] = useClipboard();
34-
const OTP_MAX_LENGTH = 6;
3536

3637
const [showVerificationStep, setShowVerificationStep] = useState<boolean>(false);
3738
const [code, setCode] = useState<string>('');
@@ -62,39 +63,37 @@ export default function TwoFactorSetupModal({
6263
};
6364
}, [twoFactorEnabled, showVerificationStep]);
6465

65-
const handleModalNextStep = () => {
66+
const handleModalNextStep = useCallback(() => {
6667
if (requiresConfirmation) {
6768
setShowVerificationStep(true);
6869
setTimeout(() => {
6970
pinInputContainerRef.current?.querySelector('input')?.focus();
7071
}, 0);
71-
7272
return;
7373
}
7474
clearSetupData();
7575
onClose();
76-
};
76+
}, [requiresConfirmation, clearSetupData, onClose]);
7777

7878
const resetModalState = useCallback(() => {
79+
setShowVerificationStep(false);
80+
setCode('');
7981
if (twoFactorEnabled) {
8082
clearSetupData();
8183
}
82-
setShowVerificationStep(false);
83-
setCode('');
8484
}, [twoFactorEnabled, clearSetupData]);
8585

8686
useEffect(() => {
8787
if (!isOpen) {
8888
resetModalState();
89-
9089
return;
9190
}
92-
9391
if (!qrCodeSvg) {
94-
fetchSetupData().then();
92+
fetchSetupData();
9593
}
9694
}, [isOpen, qrCodeSvg, fetchSetupData, resetModalState]);
9795

96+
const CopyIcon: LucideIcon = copiedText === manualSetupKey ? Check : Copy;
9897
return (
9998
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
10099
<DialogContent className="sm:max-w-md">
@@ -167,7 +166,7 @@ export default function TwoFactorSetupModal({
167166
onClick={() => copy(manualSetupKey)}
168167
className="relative block h-auto border-l border-border px-3 hover:bg-muted"
169168
>
170-
{copiedText === manualSetupKey ? <Check className="w-4 text-green-500" /> : <Copy className="w-4" />}
169+
<CopyIcon className="w-4" />
171170
</button>
172171
</>
173172
)}
@@ -206,7 +205,7 @@ export default function TwoFactorSetupModal({
206205
>
207206
Back
208207
</Button>
209-
<Button type="submit" className="w-auto flex-1" disabled={processing || code.length < 6}>
208+
<Button type="submit" className="w-auto flex-1" disabled={processing || code.length < OTP_MAX_LENGTH}>
210209
{processing ? 'Confirming...' : 'Confirm'}
211210
</Button>
212211
</div>

0 commit comments

Comments
 (0)