@@ -73,60 +73,64 @@ export default class SettingsData extends Component {
7373 importIndexedDB = async ( ) => {
7474 if ( this . ipcRenderer ) {
7575 this . isImporting = true ;
76- await this . ipcRenderer . invoke ( 'importData' ) . then ( ( jsonString : string ) => {
77- if ( jsonString ) {
78- const DBOpenRequest = getDBOpenRequest ( ) ;
79-
80- DBOpenRequest . onsuccess = ( ) => {
81- const idbDatabase = DBOpenRequest . result ;
82-
83- IDBExportImport . clearDatabase ( idbDatabase , ( err : Event | null ) => {
84- if ( ! err ) {
85- // cleared data successfully
86- IDBExportImport . importFromJsonString (
87- idbDatabase ,
88- jsonString ,
89- // eslint-disable-next-line @typescript-eslint/no-misused-promises
90- async ( err : Event | null ) => {
91- if ( ! err ) {
92- idbDatabase . close ( ) ;
93-
94- // TODO is pulling from the backup with orbit the best "refresh" here?
95- const backup =
96- this . dataCoordinator . getSource < IndexedDBSource > (
97- 'backup' ,
98- ) ;
99-
100- if ( backup ) {
101- const records = await backup . query < InitializedRecord [ ] > (
102- ( q ) => q . findRecords ( ) ,
103- ) ;
104-
105- await this . store . sync ( ( t ) =>
106- records . map ( ( r ) => {
107- if ( r ?. attributes ?. [ 'hex' ] ) {
108- delete r . attributes [ 'hex' ] ;
109- }
110-
111- return t . addRecord ( r ) ;
112- } ) ,
113- ) ;
114- this . flashMessages . success (
115- 'Data successfully replaced.' ,
116- ) ;
117- }
76+ const jsonString = await this . ipcRenderer . invoke ( 'importData' ) as string ;
77+
78+ if ( jsonString ) {
79+ const DBOpenRequest = getDBOpenRequest ( ) ;
80+
81+ DBOpenRequest . onsuccess = ( ) => {
82+ const idbDatabase = DBOpenRequest . result ;
83+
84+ IDBExportImport . clearDatabase ( idbDatabase , ( err : Event | null ) => {
85+ if ( ! err ) {
86+ // cleared data successfully
87+ IDBExportImport . importFromJsonString (
88+ idbDatabase ,
89+ jsonString ,
90+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
91+ async ( err : Event | null ) => {
92+ if ( ! err ) {
93+ idbDatabase . close ( ) ;
94+
95+ // TODO is pulling from the backup with orbit the best "refresh" here?
96+ const backup =
97+ this . dataCoordinator . getSource < IndexedDBSource > ( 'backup' ) ;
98+
99+ if ( backup ) {
100+ const records = await backup . query < InitializedRecord [ ] > (
101+ ( q ) => q . findRecords ( ) ,
102+ ) ;
103+
104+ await this . store . sync ( ( t ) =>
105+ records . map ( ( r ) => {
106+ if ( r ?. attributes ?. [ 'hex' ] ) {
107+ delete r . attributes [ 'hex' ] ;
108+ }
109+
110+ // We have to make sure these are all Date objects
111+ // otherwise orbit will throw a validation error
112+ if ( r ?. attributes ?. [ 'createdAt' ] ) {
113+ r . attributes [ 'createdAt' ] = new Date (
114+ r . attributes [ 'createdAt' ] as string ,
115+ ) ;
116+ }
117+
118+ return t . addRecord ( r ) ;
119+ } ) ,
120+ ) ;
121+ this . flashMessages . success ( 'Data successfully replaced.' ) ;
118122 }
123+ }
119124
120- this . isImporting = false ;
121- } ,
122- ) ;
123- }
124- } ) ;
125- } ;
126- } else {
127- this . isImporting = false ;
128- }
129- } ) ;
125+ this . isImporting = false ;
126+ } ,
127+ ) ;
128+ }
129+ } ) ;
130+ } ;
131+ } else {
132+ this . isImporting = false ;
133+ }
130134 }
131135 } ;
132136}
0 commit comments