-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLocationCardMfaMobileView.tsx
More file actions
78 lines (71 loc) · 2.63 KB
/
LocationCardMfaMobileView.tsx
File metadata and controls
78 lines (71 loc) · 2.63 KB
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
import './style.scss';
import { useEffect, useRef, useState } from 'react';
import { ThemeSpacing } from '../../../../types';
import { Button } from '../../../Button/Button';
import { ButtonVariant } from '../../../Button/types';
import { Controls } from '../../../Controls/Controls';
import { Divider } from '../../../Divider/Divider';
import { IconKind } from '../../../Icon';
import { IconButton } from '../../../IconButton/IconButton';
import { IconButtonVariant } from '../../../IconButton/types';
import { QrCard } from '../../../QrCard/QrCard';
import { LocationViewHeader } from '../../components/LocationViewHeader/LocationViewHeader';
import { useLocationCardContext } from '../../context/context';
import { LocationCardViews } from '../../context/types';
import { useMfaMobileConnect } from '../../hooks/useMfaMobileConnect';
type Screen = 'loading' | 'qr' | 'error';
export const LocationCardMfaMobileView = () => {
const { setView } = useLocationCardContext();
const { start, startError, qrValue, connectionError, reset } = useMfaMobileConnect();
const [screen, setScreen] = useState<Screen>('loading');
const startedRef = useRef(false);
// Auto-start on mount
useEffect(() => {
if (startedRef.current) return;
startedRef.current = true;
void start();
}, [start]);
useEffect(() => {
if (startError ?? connectionError) {
setScreen('error');
} else if (qrValue) {
setScreen('qr');
}
}, [startError, connectionError, qrValue]);
const retry = () => {
reset();
setScreen('loading');
void start();
};
const errorMessage = startError ?? connectionError;
return (
<div className="location-card-mfa-mobile">
<Divider spacing={ThemeSpacing.Md} />
<LocationViewHeader title="Two-factor authentication">
{screen === 'loading' && <p>Preparing authentication...</p>}
{screen === 'qr' && (
<p>Open your Defguard mobile app and scan the QR code you see bellow.</p>
)}
{screen === 'error' && <p className="error">{errorMessage}</p>}
</LocationViewHeader>
{screen === 'qr' && qrValue && (
<div className="qr-wrapper">
<QrCard value={qrValue} size={184} />
</div>
)}
<Controls>
<IconButton
variant={IconButtonVariant.BigSelected}
icon={IconKind.ArrowBig}
iconRotation="left"
onClick={() => setView(LocationCardViews.Default)}
/>
<div className="right">
{screen === 'error' && (
<Button text="Try again" variant={ButtonVariant.Primary} onClick={retry} />
)}
</div>
</Controls>
</div>
);
};