11// @flow
22import { action , computed , observable , runInAction } from 'mobx' ;
3+ import { orderBy } from 'lodash' ;
34import Store from './lib/Store' ;
45import Request from './lib/LocalizedRequest' ;
56import Wallet from '../domains/Wallet' ;
@@ -88,6 +89,15 @@ export default class WalletMigrationStore extends Store {
8889 getExportedWalletById = (id: string): ?ExportedByronWallet =>
8990 this . exportedWallets . find ( w => w . id === id ) ;
9091
92+ getExportedWalletDuplicatesById = (
93+ id : string ,
94+ index : number
95+ ) : Array < ExportedByronWallet > =>
96+ this . exportedWallets . filter ( w => w . id === id && w . index !== index ) ;
97+
98+ getExportedWalletByIndex = ( index : number ) : ?ExportedByronWallet =>
99+ this . exportedWallets . find ( w => w . index === index ) ;
100+
91101 @action _selectExportSourcePath = async ( ) => {
92102 const params = {
93103 defaultPath : global . legacyStateDir ,
@@ -116,34 +126,52 @@ export default class WalletMigrationStore extends Store {
116126 }
117127 } ;
118128
119- @action _toggleWalletImportSelection = ( id : string ) => {
120- const wallet = this . getExportedWalletById ( id ) ;
129+ @action _toggleWalletImportSelection = ( { index } : { index : number } ) => {
130+ const wallet = this . getExportedWalletByIndex ( index ) ;
121131 if ( wallet ) {
122132 const { status } = wallet . import ;
123133 const isPending = status === WalletImportStatuses . PENDING ;
124134 this . _updateWalletImportStatus (
125- id ,
135+ index ,
126136 isPending
127137 ? WalletImportStatuses . UNSTARTED
128138 : WalletImportStatuses . PENDING
129139 ) ;
140+
141+ const walletDuplicates = this . getExportedWalletDuplicatesById (
142+ wallet . id ,
143+ index
144+ ) ;
145+ if ( walletDuplicates . length ) {
146+ walletDuplicates . forEach ( w => {
147+ if ( w . import . status === WalletImportStatuses . PENDING ) {
148+ w . import . status = WalletImportStatuses . UNSTARTED ;
149+ }
150+ } ) ;
151+ }
130152 }
131153 } ;
132154
133155 @action _updateWalletImportStatus = (
134- id : string ,
156+ index : number ,
135157 status : WalletImportStatus ,
136158 error ? : LocalizableError
137159 ) => {
138- const wallet = this . getExportedWalletById ( id ) ;
160+ const wallet = this . getExportedWalletByIndex ( index ) ;
139161 if ( wallet ) {
140162 wallet . import . status = status ;
141163 wallet . import . error = error || null ;
142164 }
143165 } ;
144166
145- @action _updateWalletName = ( { id, name } : { id : string , name : string } ) => {
146- const wallet = this . getExportedWalletById ( id ) ;
167+ @action _updateWalletName = ( {
168+ index,
169+ name,
170+ } : {
171+ index : number ,
172+ name : string ,
173+ } ) => {
174+ const wallet = this . getExportedWalletByIndex ( index ) ;
147175 if ( wallet ) {
148176 wallet . name = name ;
149177 }
@@ -164,18 +192,28 @@ export default class WalletMigrationStore extends Store {
164192 locale : this . stores . profile . currentLocale ,
165193 } ) ;
166194 runInAction ( 'update exportedWallets and exportErrors' , ( ) => {
167- this . exportedWallets = wallets . map ( wallet => {
168- const hasName = wallet . name !== null ;
169- const importedWallet = this . stores . wallets . getWalletById (
170- `legacy_${ wallet . id } `
171- ) ;
172- const isImported = typeof importedWallet !== 'undefined' ;
173- if ( isImported && importedWallet ) wallet . name = importedWallet . name ;
174- const status = isImported
175- ? WalletImportStatuses . EXISTS
176- : WalletImportStatuses . UNSTARTED ;
177- return { ...wallet , hasName, import : { status, error : null } } ;
195+ this . exportedWallets = orderBy (
196+ wallets . map ( wallet => {
197+ const hasName = wallet . name !== null ;
198+ const importedWallet = this . stores . wallets . getWalletById (
199+ `legacy_${ wallet . id } `
200+ ) ;
201+ const isImported = typeof importedWallet !== 'undefined' ;
202+ if ( isImported && importedWallet ) wallet . name = importedWallet . name ;
203+ const status = isImported
204+ ? WalletImportStatuses . EXISTS
205+ : WalletImportStatuses . UNSTARTED ;
206+ return { ...wallet , hasName, import : { status, error : null } } ;
207+ } ) ,
208+ [ 'hasName' , 'name' , 'id' , 'is_passphrase_empty' ] ,
209+ [ 'desc' , 'asc' , 'asc' , 'asc' ]
210+ ) ;
211+
212+ // Guard against duplicated wallet ids
213+ this . exportedWallets . forEach ( ( wallet , index ) => {
214+ wallet . index = index + 1 ;
178215 } ) ;
216+
179217 this . exportErrors =
180218 errors || ! this . exportedWalletsCount ? 'No wallets found' : '' ;
181219 } ) ;
@@ -227,8 +265,8 @@ export default class WalletMigrationStore extends Store {
227265 // Reset restore requests to clear previous errors
228266 this . restoreExportedWalletRequest . reset ( ) ;
229267
230- const { id } = exportedWallet ;
231- this . _updateWalletImportStatus ( id , WalletImportStatuses . RUNNING ) ;
268+ const { id, index } = exportedWallet ;
269+ this . _updateWalletImportStatus ( index , WalletImportStatuses . RUNNING ) ;
232270 try {
233271 const restoredWallet = await this . restoreExportedWalletRequest . execute (
234272 exportedWallet
@@ -237,13 +275,30 @@ export default class WalletMigrationStore extends Store {
237275 throw new Error ( 'Restored wallet was not received correctly' ) ;
238276
239277 runInAction ( 'update restoredWallets' , ( ) => {
240- this . _updateWalletImportStatus ( id , WalletImportStatuses . COMPLETED ) ;
278+ this . _updateWalletImportStatus ( index , WalletImportStatuses . COMPLETED ) ;
279+
280+ const walletDuplicates = this . getExportedWalletDuplicatesById (
281+ id ,
282+ index
283+ ) ;
284+ if ( walletDuplicates . length ) {
285+ walletDuplicates . forEach ( w => {
286+ if ( w . import . status !== WalletImportStatuses . COMPLETED ) {
287+ w . import . status = WalletImportStatuses . COMPLETED ;
288+ }
289+ } ) ;
290+ }
291+
241292 this . restoredWallets . push ( restoredWallet ) ;
242293 } ) ;
243294 } catch ( error ) {
244295 runInAction ( 'update restorationErrors' , ( ) => {
245296 const { name, is_passphrase_empty : hasPassword } = exportedWallet ;
246- this . _updateWalletImportStatus ( id , WalletImportStatuses . ERRORED , error ) ;
297+ this . _updateWalletImportStatus (
298+ index ,
299+ WalletImportStatuses . ERRORED ,
300+ error
301+ ) ;
247302 this . restorationErrors . push ( {
248303 error,
249304 wallet : { id, name, hasPassword } ,
0 commit comments