-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathindex.tsx
97 lines (78 loc) · 3.27 KB
/
index.tsx
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
import { useMemo } from 'react';
import styled from 'styled-components';
import { selectSelectedDevice } from '@suite-common/wallet-core';
import { OnboardingStepBox } from 'src/components/onboarding';
import { PinMatrix, PrerequisitesGuide, Translation } from 'src/components/suite';
import steps from 'src/config/onboarding/steps';
import { useOnboarding, useSelector } from 'src/hooks/suite';
import { selectPrerequisite } from 'src/reducers/suite/suiteReducer';
import IsSameDevice from './components/IsSameDevice';
const UnexpectedContainer = styled.div`
margin-top: 100px;
`;
interface UnexpectedStateProps {
children: JSX.Element;
}
/**
* This component handles unexpected device states across various steps in the onboarding.
*/
const UnexpectedState = ({ children }: UnexpectedStateProps) => {
const device = useSelector(selectSelectedDevice);
const prerequisite = useSelector(selectPrerequisite);
const { prevDeviceId, activeStepId, showPinMatrix } = useOnboarding();
const activeStep = steps.find(s => s.id === activeStepId);
const isNotSameDevice = useMemo(() => {
// if no device was connected before, assume it is same device
if (!prevDeviceId) {
return false;
}
const deviceId = device?.id;
if (!deviceId) {
// we don't know
return null;
}
return deviceId !== prevDeviceId;
}, [prevDeviceId, device]);
const UnexpectedStateComponent = useMemo(() => {
if (!activeStep?.prerequisites) return null;
// there may be specif onboarding prerequisites
if (activeStep?.prerequisites.includes('device-different') && isNotSameDevice) {
// in case we can 100% detect that user reconnected different device than he had previously connected
return <IsSameDevice />;
}
// otherwise handle common prerequisite which are determined and passed as prop from Preloader component
if (prerequisite && activeStep?.prerequisites?.includes(prerequisite)) {
return <PrerequisitesGuide />;
}
}, [activeStep, prerequisite, isNotSameDevice]);
const getPinComponent = () => {
// After the PIN is set it may happen that it takes too long for an user to finish the onboarding process.
// Then the device will get auto locked and requests to show a PIN matrix next before changing its setting.
// (which could happen on Final step where we set device name and homescreen)
if (!device?.features) return null;
if (activeStepId === 'set-pin') return null; // Step for setting up a PIN handles all by itself
if (showPinMatrix) {
return <PinMatrix device={device} />;
}
};
if (!activeStep) {
return null;
}
const pinComponent = getPinComponent();
if (pinComponent) {
return (
<OnboardingStepBox
heading={<Translation id="TR_ENTER_PIN" />}
device={device}
isActionAbortable={false}
>
{pinComponent}
</OnboardingStepBox>
);
}
if (UnexpectedStateComponent) {
return <UnexpectedContainer>{UnexpectedStateComponent}</UnexpectedContainer>;
}
return children;
};
export default UnexpectedState;