mirror of
https://github.com/selfxyz/self.git
synced 2026-04-05 03:00:53 -04:00
Remove navigationRef from provingMachine (#1011)
This commit is contained in:
committed by
GitHub
parent
5de4aa8c9e
commit
99c5612e04
@@ -7,13 +7,16 @@ import { Platform } from 'react-native';
|
||||
|
||||
import {
|
||||
Adapters,
|
||||
createListenersMap,
|
||||
reactNativeScannerAdapter,
|
||||
SdkEvents,
|
||||
SelfClientProvider as SDKSelfClientProvider,
|
||||
type TrackEventParams,
|
||||
webScannerShim,
|
||||
type WsConn,
|
||||
} from '@selfxyz/mobile-sdk-alpha';
|
||||
|
||||
import { navigationRef } from '@/navigation';
|
||||
import { unsafe_getPrivateKey } from '@/providers/authProvider';
|
||||
import { selfClientDocumentsAdapter } from '@/providers/passportDataProvider';
|
||||
import analytics from '@/utils/analytics';
|
||||
@@ -95,8 +98,64 @@ export const SelfClientProvider = ({ children }: PropsWithChildren) => {
|
||||
[],
|
||||
);
|
||||
|
||||
const appListeners = useMemo(() => {
|
||||
const { map, addListener } = createListenersMap();
|
||||
|
||||
addListener(SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND, () => {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('DocumentDataNotFound');
|
||||
}
|
||||
});
|
||||
|
||||
addListener(SdkEvents.PROVING_ACCOUNT_VERIFIED_SUCCESS, () => {
|
||||
setTimeout(() => {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('AccountVerifiedSuccess');
|
||||
}
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
addListener(
|
||||
SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE,
|
||||
async ({ hasValidDocument }) => {
|
||||
setTimeout(() => {
|
||||
if (navigationRef.isReady()) {
|
||||
if (hasValidDocument) {
|
||||
navigationRef.navigate('Home');
|
||||
} else {
|
||||
navigationRef.navigate('Launch');
|
||||
}
|
||||
}
|
||||
}, 3000);
|
||||
},
|
||||
);
|
||||
|
||||
addListener(
|
||||
SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED,
|
||||
({ passportData }) => {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('UnsupportedDocument', {
|
||||
passportData,
|
||||
} as any);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
addListener(SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED, () => {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('AccountRecoveryChoice');
|
||||
}
|
||||
});
|
||||
|
||||
return map;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<SDKSelfClientProvider config={config} adapters={adapters}>
|
||||
<SDKSelfClientProvider
|
||||
config={config}
|
||||
adapters={adapters}
|
||||
listeners={appListeners}
|
||||
>
|
||||
{children}
|
||||
</SDKSelfClientProvider>
|
||||
);
|
||||
|
||||
@@ -39,6 +39,7 @@ import {
|
||||
import {
|
||||
hasAnyValidRegisteredDocument,
|
||||
loadSelectedDocument,
|
||||
SdkEvents,
|
||||
SelfClient,
|
||||
} from '@selfxyz/mobile-sdk-alpha';
|
||||
import {
|
||||
@@ -47,7 +48,6 @@ import {
|
||||
} from '@selfxyz/mobile-sdk-alpha/constants/analytics';
|
||||
import { useProtocolStore } from '@selfxyz/mobile-sdk-alpha/stores';
|
||||
|
||||
import { navigationRef } from '@/navigation';
|
||||
// will need to be passed in from selfClient
|
||||
import {
|
||||
clearPassportData,
|
||||
@@ -207,6 +207,11 @@ interface ProvingState {
|
||||
_handleWsOpen: () => void;
|
||||
_handleWsError: (error: Event) => void;
|
||||
_handleWsClose: (event: CloseEvent) => void;
|
||||
|
||||
_handlePassportNotSupported: (selfClient: SelfClient) => void;
|
||||
_handleAccountRecoveryChoice: (selfClient: SelfClient) => void;
|
||||
_handleAccountVerifiedSuccess: (selfClient: SelfClient) => void;
|
||||
_handlePassportDataNotFound: (selfClient: SelfClient) => void;
|
||||
}
|
||||
|
||||
export const useProvingStore = create<ProvingState>((set, get) => {
|
||||
@@ -239,16 +244,14 @@ export const useProvingStore = create<ProvingState>((set, get) => {
|
||||
if (state.value === 'post_proving') {
|
||||
get().postProving(selfClient);
|
||||
}
|
||||
|
||||
if (
|
||||
get().circuitType !== 'disclose' &&
|
||||
(state.value === 'error' || state.value === 'failure')
|
||||
) {
|
||||
setTimeout(() => {
|
||||
if (navigationRef.isReady()) {
|
||||
get()._handleRegisterErrorOrFailure(selfClient);
|
||||
}
|
||||
}, 3000);
|
||||
get()._handleRegisterErrorOrFailure(selfClient);
|
||||
}
|
||||
|
||||
if (state.value === 'completed') {
|
||||
trackEvent(ProofEvents.PROOF_COMPLETED, {
|
||||
circuitType: get().circuitType,
|
||||
@@ -266,33 +269,27 @@ export const useProvingStore = create<ProvingState>((set, get) => {
|
||||
})();
|
||||
}
|
||||
|
||||
if (get().circuitType !== 'disclose' && navigationRef.isReady()) {
|
||||
setTimeout(() => {
|
||||
navigationRef.navigate('AccountVerifiedSuccess');
|
||||
}, 3000);
|
||||
if (get().circuitType !== 'disclose') {
|
||||
get()._handleAccountVerifiedSuccess(selfClient);
|
||||
}
|
||||
|
||||
if (get().circuitType === 'disclose') {
|
||||
useSelfAppStore.getState().handleProofResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (state.value === 'passport_not_supported') {
|
||||
if (navigationRef.isReady()) {
|
||||
const currentPassportData = get().passportData;
|
||||
(navigationRef as any).navigate('UnsupportedDocument', {
|
||||
passportData: currentPassportData,
|
||||
});
|
||||
}
|
||||
get()._handlePassportNotSupported(selfClient);
|
||||
}
|
||||
|
||||
if (state.value === 'account_recovery_choice') {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('AccountRecoveryChoice');
|
||||
}
|
||||
get()._handleAccountRecoveryChoice(selfClient);
|
||||
}
|
||||
|
||||
if (state.value === 'passport_data_not_found') {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('DocumentDataNotFound');
|
||||
}
|
||||
get()._handlePassportDataNotFound(selfClient);
|
||||
}
|
||||
|
||||
if (state.value === 'failure') {
|
||||
if (get().circuitType === 'disclose') {
|
||||
const { error_code, reason } = get();
|
||||
@@ -433,17 +430,13 @@ export const useProvingStore = create<ProvingState>((set, get) => {
|
||||
try {
|
||||
const hasValid = await hasAnyValidRegisteredDocument(selfClient);
|
||||
|
||||
if (navigationRef.isReady()) {
|
||||
if (hasValid) {
|
||||
navigationRef.navigate('Home');
|
||||
} else {
|
||||
navigationRef.navigate('Launch');
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.navigate('Launch');
|
||||
}
|
||||
selfClient.emit(SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE, {
|
||||
hasValidDocument: hasValid,
|
||||
});
|
||||
} catch (error) {
|
||||
selfClient.emit(SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE, {
|
||||
hasValidDocument: false,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1101,6 +1094,24 @@ export const useProvingStore = create<ProvingState>((set, get) => {
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
_handlePassportNotSupported: (selfClient: SelfClient) => {
|
||||
selfClient.emit(SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED, {
|
||||
passportData: get().passportData as PassportData,
|
||||
});
|
||||
},
|
||||
|
||||
_handleAccountRecoveryChoice: (selfClient: SelfClient) => {
|
||||
selfClient.emit(SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED);
|
||||
},
|
||||
|
||||
_handleAccountVerifiedSuccess: (selfClient: SelfClient) => {
|
||||
selfClient.emit(SdkEvents.PROVING_ACCOUNT_VERIFIED_SUCCESS);
|
||||
},
|
||||
|
||||
_handlePassportDataNotFound: (selfClient: SelfClient) => {
|
||||
selfClient.emit(SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user