@@ -2,6 +2,9 @@ import { StyleSheet, View } from "react-native";
2
2
import { Button , Input , Layout , Text , Icon } from "@ui-kitten/components" ;
3
3
import { useState } from "react" ;
4
4
import * as DocumentPicker from "expo-document-picker" ;
5
+ import { useRouter } from "expo-router" ;
6
+ import { useFacade } from "@/data/facades" ;
7
+ import * as FileSystem from "expo-file-system" ;
5
8
6
9
import { Files } from "@/svgs/Files" ;
7
10
@@ -50,9 +53,16 @@ const styles = StyleSheet.create({
50
53
} ) ;
51
54
52
55
export default function ImportFile ( ) {
56
+ const router = useRouter ( ) ;
57
+ const facade = useFacade ( ) ;
58
+ const importAccount = facade . importAccount . useMutation ( ) ;
59
+
53
60
const [ fileName , setFileName ] = useState < string | null > ( null ) ;
61
+ const [ fileContent , setFileContent ] = useState < string | null > ( null ) ;
54
62
const [ accountName , setAccountName ] = useState ( "" ) ;
55
63
const [ nameError , setNameError ] = useState ( "" ) ;
64
+ const [ fileError , setFileError ] = useState ( "" ) ;
65
+ const [ debugContent , setDebugContent ] = useState < string > ( "" ) ;
56
66
57
67
const pickDocument = async ( ) => {
58
68
try {
@@ -62,15 +72,55 @@ export default function ImportFile() {
62
72
63
73
if ( ! result . canceled ) {
64
74
setFileName ( result . assets [ 0 ] . name ) ;
65
- console . log ( "Document picked:" , result . assets [ 0 ] ) ;
75
+ const content = await FileSystem . readAsStringAsync (
76
+ result . assets [ 0 ] . uri ,
77
+ ) ;
78
+ setFileContent ( content ) ;
79
+ setDebugContent ( content . slice ( 0 , 100 ) + "..." ) ; // Show first 100 chars
80
+ setFileError ( "" ) ;
66
81
}
67
82
} catch ( err ) {
68
83
console . log ( "DocumentPicker Error:" , err ) ;
84
+ setFileError ( "Error reading file. Please try again." ) ;
69
85
}
70
86
} ;
71
87
72
88
const handleRemoveFile = ( ) => {
73
89
setFileName ( null ) ;
90
+ setFileContent ( null ) ;
91
+ setFileError ( "" ) ;
92
+ } ;
93
+
94
+ const handleContinue = async ( ) => {
95
+ let hasError = false ;
96
+
97
+ if ( accountName . length < 3 ) {
98
+ setNameError ( "Account name must be at least 3 characters" ) ;
99
+ hasError = true ;
100
+ }
101
+
102
+ if ( ! fileContent ) {
103
+ setFileError ( "Please select a valid file" ) ;
104
+ hasError = true ;
105
+ }
106
+
107
+ if ( hasError ) return ;
108
+
109
+ try {
110
+ await importAccount . mutateAsync ( {
111
+ account : fileContent ,
112
+ name : accountName ,
113
+ } ) ;
114
+
115
+ // Navigate to main app after successful import
116
+ router . push ( "/(tabs)/" ) ;
117
+ } catch ( error ) {
118
+ console . error ( "Import error:" , error ) ;
119
+ // Handle other errors
120
+ setFileError (
121
+ "Failed to import account. Please check your file and try again." ,
122
+ ) ;
123
+ }
74
124
} ;
75
125
76
126
return (
@@ -91,33 +141,39 @@ export default function ImportFile() {
91
141
</ View >
92
142
93
143
{ fileName ? (
94
- < View style = { styles . fileRow } >
95
- < Text style = { styles . fileName } > { fileName } </ Text >
96
- < Button
97
- appearance = "ghost"
98
- status = "basic"
99
- accessoryLeft = { ( props ) => (
100
- < Icon { ...props } name = "trash-outline" />
101
- ) }
102
- onPress = { handleRemoveFile }
103
- style = { styles . trashButton }
104
- />
144
+ < View >
145
+ < View style = { styles . fileRow } >
146
+ < Text style = { styles . fileName } > { fileName } </ Text >
147
+ < Button
148
+ appearance = "ghost"
149
+ status = "basic"
150
+ accessoryLeft = { ( props ) => (
151
+ < Icon { ...props } name = "trash-outline" />
152
+ ) }
153
+ onPress = { handleRemoveFile }
154
+ style = { styles . trashButton }
155
+ />
156
+ </ View >
157
+ { fileError ? (
158
+ < Text style = { styles . errorText } > { fileError } </ Text >
159
+ ) : null }
105
160
</ View >
106
161
) : (
107
162
< View style = { styles . uploadContainer } >
108
163
< Files />
109
164
< Text > Upload your JSON or Bech32 file</ Text >
110
165
< Button onPress = { pickDocument } > Select File</ Button >
166
+ { fileError ? (
167
+ < Text style = { styles . errorText } > { fileError } </ Text >
168
+ ) : null }
111
169
</ View >
112
170
) }
113
171
114
172
< Button
115
- onPress = { ( ) => {
116
- /* TODO: Handle import */
117
- } }
118
- disabled = { ! accountName || ! fileName }
173
+ onPress = { handleContinue }
174
+ disabled = { ! accountName || ! fileContent || importAccount . isPending }
119
175
>
120
- Continue
176
+ { importAccount . isPending ? "Importing..." : " Continue" }
121
177
</ Button >
122
178
</ View >
123
179
</ Layout >
0 commit comments