11import React , { ReactElement , useCallback , useMemo } from 'react' ;
22import { StyleSheet } from 'react-native' ;
33import { View , Text , ArrowRight , Plus , Button , NavButton , CircleAlert } from '../theme/components.ts' ;
4- import { createNewPubky } from '../utils/pubky.ts' ;
4+ import {
5+ createNewPubky ,
6+ importPubky as importPubkyUtil ,
7+ } from '../utils/pubky.ts' ;
58import { useDispatch } from 'react-redux' ;
69import PubkyRingHeader from './PubkyRingHeader' ;
710import { importFile } from '../utils/rnfs.ts' ;
811import { showEditPubkyPrompt , showToast } from '../utils/helpers.ts' ;
912import { SheetManager } from 'react-native-actions-sheet' ;
1013import { useNavigation } from '@react-navigation/native' ;
14+ import { err , ok , Result } from '@synonymdev/result' ;
15+ import { IGenerateSecretKey , mnemonicPhraseToKeypair } from '@synonymdev/react-native-pubky' ;
1116
1217const EmptyState = ( ) : ReactElement => {
1318 const dispatch = useDispatch ( ) ;
@@ -31,29 +36,62 @@ const EmptyState = (): ReactElement => {
3136 } , 200 ) ;
3237 } , [ dispatch ] ) ;
3338
34- const importPubky = useCallback ( async ( ) => {
35- const res = await importFile ( dispatch ) ;
36- if ( res . isErr ( ) ) {
37- if ( res . error ?. message ) {
39+ const importPubky = useCallback ( async ( mnemonic = '' ) : Promise < Result < string > > => {
40+ if ( mnemonic ) {
41+ const secretKeyRes : Result < IGenerateSecretKey > = await mnemonicPhraseToKeypair ( mnemonic ) ;
42+ if ( secretKeyRes . isErr ( ) ) {
43+ const msg = secretKeyRes . error . message ;
44+ showToast ( {
45+ type : 'error' ,
46+ title : 'Error' ,
47+ description : msg ,
48+ } ) ;
49+ return err ( msg ) ;
50+ }
51+
52+ const secretKey : string = secretKeyRes . value . secret_key ;
53+ const pubky = await importPubkyUtil ( { secretKey, dispatch, mnemonic } ) ;
54+ if ( pubky . isErr ( ) ) {
55+ const msg = pubky . error . message ;
3856 showToast ( {
3957 type : 'error' ,
4058 title : 'Error' ,
41- description : res . error . message ,
59+ description : msg ,
4260 } ) ;
61+ return err ( msg ) ;
4362 }
44- } else {
63+ await SheetManager . hide ( 'add-pubky' ) ;
4564 setTimeout ( ( ) => {
4665 showEditPubkyPrompt ( {
4766 title : 'Setup' ,
48- pubky : res . value ,
67+ pubky : pubky . value ,
4968 } ) ;
5069 } , 200 ) ;
70+ return ok ( 'Successfully created pubky.' ) ;
71+ }
72+ const res = await importFile ( dispatch ) ;
73+ if ( res . isErr ( ) ) {
74+ const msg = res . error ?. message ?? 'Unable to import file.' ;
5175 showToast ( {
52- type : 'success ' ,
53- title : 'Success ' ,
54- description : 'Pubky imported successfully' ,
76+ type : 'error ' ,
77+ title : 'Error ' ,
78+ description : msg ,
5579 } ) ;
80+ return err ( msg ) ;
5681 }
82+ setTimeout ( ( ) => {
83+ showEditPubkyPrompt ( {
84+ title : 'Setup' ,
85+ pubky : res . value ,
86+ } ) ;
87+ } , 200 ) ;
88+ const msg = 'Pubky imported successfully' ;
89+ showToast ( {
90+ type : 'success' ,
91+ title : 'Success' ,
92+ description : msg ,
93+ } ) ;
94+ return ok ( msg ) ;
5795 } , [ dispatch ] ) ;
5896
5997 const onPress = useCallback ( ( ) => {
0 commit comments