Skip to content

Commit 1b7e7f5

Browse files
committed
Fixed window not found & occasional sqlite error for packages
Signed-off-by: Omkar Phansopkar <[email protected]>
1 parent cec79dc commit 1b7e7f5

File tree

7 files changed

+410
-330
lines changed

7 files changed

+410
-330
lines changed
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
1-
import { toast } from 'react-toastify';
2-
import React, { DragEvent } from 'react'
3-
import { useNavigate } from 'react-router-dom';
1+
import { toast } from "react-toastify";
2+
import React, { DragEvent } from "react";
3+
import { useNavigate } from "react-router-dom";
44

5-
import { useWorkbenchDB } from '../../contexts/workbenchContext';
6-
import { ROUTES } from '../../constants/routes';
5+
import { useWorkbenchDB } from "../../contexts/workbenchContext";
6+
import { ROUTES } from "../../constants/routes";
77

8-
9-
const lastLogs:{ [key: string]: number} = {}
8+
const lastLogs: { [key: string]: number } = {};
109
export const CustomLogger = (id: string, ...args: unknown[]) => {
11-
const currentTime = (new Date()).getTime();
12-
if(!(lastLogs[id] && currentTime < lastLogs[id]+1000)){
10+
const currentTime = new Date().getTime();
11+
if (!(lastLogs[id] && currentTime < lastLogs[id] + 1000)) {
1312
console.log(id, ...args);
1413
lastLogs[id] = currentTime;
1514
}
16-
}
15+
};
1716

1817
const DropZone = (props: React.PropsWithChildren) => {
1918
const navigate = useNavigate();
2019
const { sqliteParser, importJsonFile } = useWorkbenchDB();
2120

22-
function DragOverHandler(e: DragEvent){
21+
function DragOverHandler(e: DragEvent) {
2322
e.preventDefault();
2423
e.stopPropagation();
2524
// CustomLogger("Drag detected", e, e.dataTransfer.files);
2625
}
27-
function DropHandler(e: DragEvent){
26+
function DropHandler(e: DragEvent) {
2827
e.preventDefault();
2928
e.stopPropagation();
3029

3130
const regex = {
3231
json: /\.(json)+$/i,
3332
sqlite: /\.(sqlite)+$/i,
34-
}
33+
};
3534

36-
const validFiles =
37-
Array.from(e.dataTransfer.files)
38-
.filter(file => file.name.match(regex.json) || file.name.match(regex.sqlite))
39-
.filter(file => file !== null);
35+
const validFiles = Array.from(e.dataTransfer.files)
36+
.filter(
37+
(file) => file.name.match(regex.json) || file.name.match(regex.sqlite)
38+
)
39+
.filter((file) => file !== null);
4040
const fileToImport = validFiles[0];
41-
41+
4242
console.log("Dropped files:", e, e.dataTransfer.files);
4343
console.log("Valid files:", validFiles);
4444

45-
if(!validFiles.length){
46-
return toast('Only json/sqlite file can be imported !', {
47-
type: 'error',
45+
if (!validFiles.length) {
46+
return toast.error("Only json/sqlite file can be imported !", {
4847
style: { width: 320 },
4948
});
5049
}
5150

52-
if(validFiles.length > 1){
53-
toast(`Only 1 json/sqlite file will be imported at a time`, {
54-
type: 'warning',
51+
if (validFiles.length > 1) {
52+
toast.warn(`Only 1 json/sqlite file can be imported at a time`, {
5553
style: { width: 375 },
5654
});
5755
}
5856

59-
if(regex.sqlite.test(fileToImport.name)){
57+
if (regex.sqlite.test(fileToImport.name)) {
6058
console.log("Parsing sqlite file:", fileToImport.name);
6159
sqliteParser(fileToImport.path);
6260
navigate(ROUTES.HOME);
@@ -69,9 +67,9 @@ const DropZone = (props: React.PropsWithChildren) => {
6967

7068
return (
7169
<div onDragOver={DragOverHandler} onDrop={DropHandler}>
72-
{ props.children }
70+
{props.children}
7371
</div>
74-
)
75-
}
72+
);
73+
};
7674

77-
export default DropZone
75+
export default DropZone;

src/contexts/workbenchContext.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
220220
}
221221
console.error("Err trying to import sqlite:");
222222
console.error(err);
223-
toast(`Unexpected error while importing json \nPlease check console for more info`, {
224-
type: 'error'
225-
});
223+
toast.error(`Unexpected error while importing json \nPlease check console for more info`);
226224
abortImport();
227225
});
228226
})
@@ -233,9 +231,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
233231
}
234232
console.error("Err trying to import sqlite:");
235233
console.error(err);
236-
toast(`Unexpected error while finalising json import \nPlease check console for more info`, {
237-
type: 'error'
238-
});
234+
toast.error(`Unexpected error while finalising json import \nPlease check console for more info`);
239235
abortImport();
240236
});
241237
}
@@ -305,6 +301,13 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
305301
if(!preventNavigation)
306302
navigate(DEFAULT_ROUTE_ON_IMPORT);
307303
});
304+
})
305+
.catch(err => {
306+
abortImport();
307+
console.error("Some error parsing data (caught in workbenchContext) !!", err);
308+
toast.error(
309+
"Some error parsing data !! \nPlease check console for more info"
310+
);
308311
});
309312
}
310313

@@ -330,9 +333,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
330333
} catch (err) {
331334
console.log(`some error importing json - ${message.jsonFilePath}`, err);
332335
abortImport();
333-
toast(`Unexpected error while importing json \nPlease check console for more info`, {
334-
type: 'error'
335-
});
336+
toast.error(`Unexpected error while importing json \nPlease check console for more info`);
336337
}
337338
});
338339
ipcRenderer.on(IMPORT_REPLY_CHANNEL.SQLITE, (_, message: SQLITE_IMPORT_REPLY_FORMAT) => {
@@ -341,14 +342,12 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
341342
} catch (err) {
342343
console.log(`some error importing sqlite - ${message.sqliteFilePath}`, err);
343344
abortImport();
344-
toast(`Unexpected error while importing sqlite \nPlease check console for more info`, {
345-
type: 'error'
346-
});
345+
toast.error(`Unexpected error while importing sqlite \nPlease check console for more info`);
347346
}
348347
});
349348
ipcRenderer.on(SAVE_REPLY_CHANNEL.SQLITE, (_, message: SQLITE_SAVE_REPLY_FORMAT) => {
350349
if(!value.db || !value.initialized){
351-
return toast("No JSON/Sqlite imported to save as new SQLite file", {
350+
return toast.error("No JSON/Sqlite imported to save as new SQLite file", {
352351
type: "error",
353352
style: { width: 400 },
354353
});
@@ -365,7 +364,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
365364
reader.pipe(writer);
366365
reader.on('end', () => {
367366
console.log("Saved", newFileName);
368-
toast("Saved sqlite file, loading from new file", { type: 'success' });
367+
toast.success("Saved sqlite file, loading from new file");
369368
sqliteParser(newFileName, true);
370369
});
371370
}

0 commit comments

Comments
 (0)