Skip to content

Commit 0fbced4

Browse files
committed
fix: error response
- Adds error response to importPubky - Ensures the mnemonic form no longer closes on error - Updates isNewFormat check - Adds totast message to MnemonicForm.tsx.
1 parent 7131016 commit 0fbced4

File tree

6 files changed

+51
-39
lines changed

6 files changed

+51
-39
lines changed

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ android {
8282
applicationId "to.pubky.ring"
8383
minSdkVersion rootProject.ext.minSdkVersion
8484
targetSdkVersion rootProject.ext.targetSdkVersion
85-
versionCode 7
85+
versionCode 8
8686
versionName "1.3"
8787
}
8888

ios/pubkyring.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
buildSettings = {
290290
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
291291
CLANG_ENABLE_MODULES = YES;
292-
CURRENT_PROJECT_VERSION = 1;
292+
CURRENT_PROJECT_VERSION = 2;
293293
DEVELOPMENT_TEAM = KYH47R284B;
294294
ENABLE_BITCODE = NO;
295295
INFOPLIST_FILE = pubkyring/Info.plist;
@@ -320,7 +320,7 @@
320320
buildSettings = {
321321
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
322322
CLANG_ENABLE_MODULES = YES;
323-
CURRENT_PROJECT_VERSION = 1;
323+
CURRENT_PROJECT_VERSION = 2;
324324
DEVELOPMENT_TEAM = KYH47R284B;
325325
INFOPLIST_FILE = pubkyring/Info.plist;
326326
INFOPLIST_KEY_CFBundleDisplayName = "Pubky Ring";

src/components/AddPubky.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, {
66
useState,
77
} from 'react';
88
import {
9+
Dimensions,
910
Image,
1011
Platform,
1112
StyleSheet,
@@ -27,13 +28,25 @@ import ModalIndicator from './ModalIndicator.tsx';
2728
import MnemonicForm from './MnemonicForm.tsx';
2829
import { AUTHORIZE_KEY_GRADIENT } from '../utils/constants.ts';
2930
import absoluteFillObject = StyleSheet.absoluteFillObject;
31+
import { Result } from '@synonymdev/result';
32+
import { toastConfig } from '../theme/toastConfig.tsx';
33+
import Toast from 'react-native-toast-message';
3034

3135
const ACTION_SHEET_HEIGHT = Platform.OS === 'ios' ? '95%' : '100%';
36+
const { height } = Dimensions.get('window');
37+
const isSmallScreen = height < 700;
38+
const toastStyle = {
39+
top: Platform.select({
40+
ios: isSmallScreen ? -9 : -50,
41+
android: isSmallScreen ? -9 : -50,
42+
}),
43+
};
44+
3245

3346
const AddPubky = ({ payload }: {
3447
payload: {
3548
createPubky: () => void;
36-
importPubky: (mnemonic?: string) => Promise<void>;
49+
importPubky: (mnemonic?: string) => Promise<Result<string>>;
3750
};
3851
}): ReactElement => {
3952
const navigationAnimation = useSelector(getNavigationAnimation);
@@ -70,11 +83,6 @@ const AddPubky = ({ payload }: {
7083
setCurrentScreen('import-options');
7184
}, []);
7285

73-
const onMnemonicImport = useCallback(async (mnemonicPhrase: string) => {
74-
await importPubky(mnemonicPhrase);
75-
closeSheet();
76-
}, [closeSheet, importPubky]);
77-
7886
const onMnemonicBack = useCallback(() => {
7987
setCurrentScreen('import-options');
8088
}, []);
@@ -183,7 +191,7 @@ const AddPubky = ({ payload }: {
183191
<MnemonicForm
184192
onBack={onMnemonicBack}
185193
onCancel={onMnemonicCancel}
186-
onImport={onMnemonicImport}
194+
onImport={importPubky}
187195
/>
188196
);
189197
}
@@ -215,7 +223,7 @@ const AddPubky = ({ payload }: {
215223
</View>
216224
</>
217225
);
218-
}, [currentScreen, getButtonConfig, getHeaderText, getImage, messageText, onMnemonicBack, onMnemonicCancel, onMnemonicImport, renderBackButton, shouldShowBackButton, titleText]);
226+
}, [currentScreen, getButtonConfig, getHeaderText, getImage, importPubky, messageText, onMnemonicBack, onMnemonicCancel, renderBackButton, shouldShowBackButton, titleText]);
219227

220228
return (
221229
<View style={styles.container}>
@@ -235,6 +243,7 @@ const AddPubky = ({ payload }: {
235243
>
236244
{getContent()}
237245
</RadialGradient>
246+
<Toast config={toastConfig({ style: toastStyle })} />
238247
</ActionSheetContainer>
239248
</View>
240249
);

src/components/MnemonicForm.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ import {
2727
import { ScrollView } from 'react-native';
2828
import Button from '../components/Button.tsx';
2929
import ModalIndicator from './ModalIndicator.tsx';
30+
import { Result } from '@synonymdev/result';
3031

3132
interface MnemonicFormProps {
3233
onBack: () => void;
3334
onCancel: () => void;
34-
onImport: (mnemonicPhrase: string) => void;
35+
onImport: (mnemonicPhrase: string) => Promise<Result<string>>
3536
}
3637

3738
const MnemonicForm = ({ onBack, onCancel, onImport }: MnemonicFormProps): ReactElement => {

src/screens/HomeScreen.tsx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import PubkyRingLogo from '../images/pubky-ring.png';
4444
// @ts-ignore
4545
import DeviceMobileLogo from '../images/device-mobile.png';
4646
import { PUBKY_APP_URL } from '../utils/constants.ts';
47-
import { Result } from '@synonymdev/result';
47+
import { err, ok, Result } from '@synonymdev/result';
4848
import { mnemonicPhraseToKeypair, IGenerateSecretKey } from '@synonymdev/react-native-pubky';
4949

5050
type NavigationProp = NativeStackNavigationProp<RootStackParamList, 'Home'>;
@@ -103,58 +103,62 @@ const HomeScreen = (): ReactElement => {
103103
}, 200);
104104
}, [dispatch]);
105105

106-
const importPubky = useCallback(async (mnemonic = ''): Promise<void> => {
106+
const importPubky = useCallback(async (mnemonic = ''): Promise<Result<string>> => {
107107
if (mnemonic) {
108108
const secretKeyRes: Result<IGenerateSecretKey> = await mnemonicPhraseToKeypair(mnemonic);
109109
if (secretKeyRes.isErr()) {
110+
const msg = secretKeyRes.error.message;
110111
showToast({
111112
type: 'error',
112113
title: 'Error',
113-
description: 'An error occurred while creating the Pubky',
114+
description: msg,
114115
});
115-
return;
116+
return err(msg);
116117
}
117118

118119
const secretKey: string = secretKeyRes.value.secret_key;
119120
const pubky = await importPubkyUtil({ secretKey, dispatch, mnemonic });
120121
if (pubky.isErr()) {
122+
const msg = pubky.error.message;
121123
showToast({
122124
type: 'error',
123125
title: 'Error',
124-
description: pubky.error.message,
126+
description: msg,
125127
});
126-
return;
128+
return err(msg);
127129
}
130+
await SheetManager.hide('add-pubky');
128131
setTimeout( () => {
129132
showEditPubkyPrompt({
130133
title: 'Setup',
131134
pubky: pubky.value,
132135
});
133136
}, 200);
134-
return;
137+
return ok('Successfully created pubky.');
135138
}
136139
const res = await importFile(dispatch);
137140
if (res.isErr()) {
138-
if (res.error?.message) {
139-
showToast({
140-
type: 'error',
141-
title: 'Error',
142-
description: res.error.message,
143-
});
144-
}
145-
} else {
146-
setTimeout( () => {
147-
showEditPubkyPrompt({
148-
title: 'Setup',
149-
pubky: res.value,
150-
});
151-
}, 200);
141+
const msg = res.error?.message ?? 'Unable to import file.';
152142
showToast({
153-
type: 'success',
154-
title: 'Success',
155-
description: 'Pubky imported successfully',
143+
type: 'error',
144+
title: 'Error',
145+
description: msg,
156146
});
147+
return err(msg);
157148
}
149+
setTimeout( () => {
150+
showEditPubkyPrompt({
151+
title: 'Setup',
152+
pubky: res.value,
153+
});
154+
}, 200);
155+
const msg = 'Pubky imported successfully';
156+
showToast({
157+
type: 'success',
158+
title: 'Success',
159+
description: msg,
160+
});
161+
return ok(msg);
158162
}, [dispatch]);
159163

160164
const _handleDeepLink = useCallback(async () => {

src/utils/pubky.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ export const savePubky = async ({
272272
const isNewFormat = (value: string): boolean => {
273273
try {
274274
const parsed = JSON.parse(value);
275-
return typeof parsed === 'object' &&
276-
'secretKey' in parsed &&
277-
typeof parsed.secretKey === 'string';
275+
return 'secretKey' in parsed && 'mnemonic' in parsed;
278276
} catch {
279277
return false;
280278
}

0 commit comments

Comments
 (0)