Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed window title update & occasional sqlite error for packages #560

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions src/components/DropZone/DropZone.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,60 @@
import { toast } from 'react-toastify';
import React, { DragEvent } from 'react'
import { useNavigate } from 'react-router-dom';
import { toast } from "react-toastify";
import React, { DragEvent } from "react";
import { useNavigate } from "react-router-dom";

import { useWorkbenchDB } from '../../contexts/workbenchContext';
import { ROUTES } from '../../constants/routes';
import { useWorkbenchDB } from "../../contexts/workbenchContext";
import { ROUTES } from "../../constants/routes";


const lastLogs:{ [key: string]: number} = {}
const lastLogs: { [key: string]: number } = {};
export const CustomLogger = (id: string, ...args: unknown[]) => {
const currentTime = (new Date()).getTime();
if(!(lastLogs[id] && currentTime < lastLogs[id]+1000)){
const currentTime = new Date().getTime();
if (!(lastLogs[id] && currentTime < lastLogs[id] + 1000)) {
console.log(id, ...args);
lastLogs[id] = currentTime;
}
}
};

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

function DragOverHandler(e: DragEvent){
function DragOverHandler(e: DragEvent) {
e.preventDefault();
e.stopPropagation();
// CustomLogger("Drag detected", e, e.dataTransfer.files);
}
function DropHandler(e: DragEvent){
function DropHandler(e: DragEvent) {
e.preventDefault();
e.stopPropagation();

const regex = {
json: /\.(json)+$/i,
sqlite: /\.(sqlite)+$/i,
}
};

const validFiles =
Array.from(e.dataTransfer.files)
.filter(file => file.name.match(regex.json) || file.name.match(regex.sqlite))
.filter(file => file !== null);
const validFiles = Array.from(e.dataTransfer.files)
.filter(
(file) => file.name.match(regex.json) || file.name.match(regex.sqlite)
)
.filter((file) => file !== null);
const fileToImport = validFiles[0];

console.log("Dropped files:", e, e.dataTransfer.files);
console.log("Valid files:", validFiles);

if(!validFiles.length){
return toast('Only json/sqlite file can be imported !', {
type: 'error',
if (!validFiles.length) {
return toast.error("Only json/sqlite file can be imported !", {
style: { width: 320 },
});
}

if(validFiles.length > 1){
toast(`Only 1 json/sqlite file will be imported at a time`, {
type: 'warning',
if (validFiles.length > 1) {
toast.warn(`Only 1 json/sqlite file can be imported at a time`, {
style: { width: 375 },
});
}

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

return (
<div onDragOver={DragOverHandler} onDrop={DropHandler}>
{ props.children }
{props.children}
</div>
)
}
);
};

export default DropZone
export default DropZone;
27 changes: 13 additions & 14 deletions src/contexts/workbenchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
}
console.error("Err trying to import sqlite:");
console.error(err);
toast(`Unexpected error while importing json \nPlease check console for more info`, {
type: 'error'
});
toast.error(`Unexpected error while importing json \nPlease check console for more info`);
abortImport();
});
})
Expand All @@ -233,9 +231,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
}
console.error("Err trying to import sqlite:");
console.error(err);
toast(`Unexpected error while finalising json import \nPlease check console for more info`, {
type: 'error'
});
toast.error(`Unexpected error while finalising json import \nPlease check console for more info`);
abortImport();
});
}
Expand Down Expand Up @@ -305,6 +301,13 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
if(!preventNavigation)
navigate(DEFAULT_ROUTE_ON_IMPORT);
});
})
.catch(err => {
abortImport();
console.error("Some error parsing data (caught in workbenchContext) !!", err);
toast.error(
"Some error parsing data !! \nPlease check console for more info"
);
});
}

Expand All @@ -330,9 +333,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
} catch (err) {
console.log(`some error importing json - ${message.jsonFilePath}`, err);
abortImport();
toast(`Unexpected error while importing json \nPlease check console for more info`, {
type: 'error'
});
toast.error(`Unexpected error while importing json \nPlease check console for more info`);
}
});
ipcRenderer.on(IMPORT_REPLY_CHANNEL.SQLITE, (_, message: SQLITE_IMPORT_REPLY_FORMAT) => {
Expand All @@ -341,14 +342,12 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
} catch (err) {
console.log(`some error importing sqlite - ${message.sqliteFilePath}`, err);
abortImport();
toast(`Unexpected error while importing sqlite \nPlease check console for more info`, {
type: 'error'
});
toast.error(`Unexpected error while importing sqlite \nPlease check console for more info`);
}
});
ipcRenderer.on(SAVE_REPLY_CHANNEL.SQLITE, (_, message: SQLITE_SAVE_REPLY_FORMAT) => {
if(!value.db || !value.initialized){
return toast("No JSON/Sqlite imported to save as new SQLite file", {
return toast.error("No JSON/Sqlite imported to save as new SQLite file", {
type: "error",
style: { width: 400 },
});
Expand All @@ -365,7 +364,7 @@ export const WorkbenchDBProvider = (props: React.PropsWithChildren<Record<string
reader.pipe(writer);
reader.on('end', () => {
console.log("Saved", newFileName);
toast("Saved sqlite file, loading from new file", { type: 'success' });
toast.success("Saved sqlite file, loading from new file");
sqliteParser(newFileName, true);
});
}
Expand Down
Loading