-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppendAfterErrorScreen.tsx
More file actions
93 lines (79 loc) · 3.18 KB
/
AppendAfterErrorScreen.tsx
File metadata and controls
93 lines (79 loc) · 3.18 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { ConnectError } from '@corbado/web-core';
import { ConnectErrorType } from '@corbado/web-core';
import log from 'loglevel';
import React, { useCallback, useState } from 'react';
import useAppendProcess from '../../hooks/useAppendProcess';
import useShared from '../../hooks/useShared';
import { AppendScreenType } from '../../types/screenTypes';
import { AppendSituationCode, getAppendErrorMessage } from '../../types/situations';
import AppendAfterError from './append-init/AppendAfterError';
const AppendAfterErrorScreen = ({ attestationOptions }: { attestationOptions: string }) => {
const { navigateToScreen, handleErrorSoft, handleErrorHard, handleCredentialExistsError, handleSkip } =
useAppendProcess();
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState(false);
const [skipping, setSkipping] = useState(false);
const { getConnectService } = useShared();
const onSubmitClick = async () => {
if (loading) {
return;
}
setLoading(true);
setErrorMessage(undefined);
const res = await getConnectService().completeAppend(attestationOptions, 'manual');
if (res.err) {
if (res.val.type === ConnectErrorType.ExcludeCredentialsMatch) {
return handleSituation(AppendSituationCode.ClientExcludeCredentialsMatch, res.val);
}
if (res.val.type === ConnectErrorType.Cancel) {
return handleSituation(AppendSituationCode.ClientPasskeyOperationCancelled, res.val);
}
return handleSituation(AppendSituationCode.CboApiNotAvailablePostAuthenticator, res.val);
}
setLoading(false);
navigateToScreen(AppendScreenType.Success, {
aaguidName: res.val.passkeyOperation.aaguidDetails?.name,
aaguidIcon: res.val.passkeyOperation.aaguidDetails?.iconLight,
});
};
const handleSituation = (situationCode: AppendSituationCode, error?: ConnectError) => {
log.debug(`situation: ${situationCode}`);
const message = getAppendErrorMessage(situationCode);
if (message) {
setErrorMessage(message);
}
switch (situationCode) {
case AppendSituationCode.CtApiNotAvailablePreAuthenticator:
case AppendSituationCode.CboApiNotAvailablePreAuthenticator:
case AppendSituationCode.CboApiNotAvailablePostAuthenticator:
void handleErrorHard(situationCode, false, error);
break;
case AppendSituationCode.ClientPasskeyOperationCancelled:
setLoading(false);
void handleErrorSoft(situationCode, true, true, error);
break;
case AppendSituationCode.ClientExcludeCredentialsMatch:
void handleCredentialExistsError(error);
break;
case AppendSituationCode.ExplicitSkipByUser:
void handleSkip(situationCode, true);
break;
}
};
const onSkip = useCallback(() => {
if (skipping || loading) {
return;
}
setSkipping(true);
void handleSituation(AppendSituationCode.ExplicitSkipByUser);
}, [skipping, loading]);
return (
<AppendAfterError
appendLoading={loading}
errorMessage={errorMessage}
handleSubmit={() => void onSubmitClick()}
handleSkip={onSkip}
/>
);
};
export default AppendAfterErrorScreen;