Skip to content
Draft
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
16 changes: 12 additions & 4 deletions src/component/fileBrowser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
const [dirButton, setDirButton] = useState(false);
const [fileState, setFileState] = useState([]);

const getLocalFileState = (state) => state.map((file) => ({
key: file.key,
modified: file.modified,
size: file.size,
fileName: file.fileObj ? file.fileObj.name : null,
}));

useEffect(() => {
if ('showDirectoryPicker' in window) {
setDirButton(true);
Expand All @@ -35,7 +42,7 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
// setFileState({ files: allFiles });
// }
try {
window.localStorage.setItem('fileList', JSON.stringify(fileState));
window.localStorage.setItem('fileList', JSON.stringify(getLocalFileState(fileState)));
} catch (e) {
toast.error(e.message);
}
Expand All @@ -47,13 +54,14 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {

const handleSelectFile = (data) => {
const fileExtensions = ['jpeg', 'jpg', 'png', 'exe'];
if (fileExtensions.includes(data.fileObj.name.split('.').pop())) {
const fileExt = data.fileObj.name.split('.').pop().toLowerCase();
if (fileExtensions.includes(fileExt)) {
// eslint-disable-next-line no-alert
alert('Wrong file extension');
return;
}

if (data.fileObj.name.split('.').pop() === 'graphml') {
if (fileExt === 'graphml') {
let foundi = -1;
superState.graphs.forEach((g, i) => {
if ((g.fileName === data.fileObj.name)) {
Expand Down Expand Up @@ -251,7 +259,7 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {

setFileState(filesArray);
try {
window.localStorage.setItem('fileList', JSON.stringify(filesArray));
window.localStorage.setItem('fileList', JSON.stringify(getLocalFileState(filesArray)));
} catch (e) {
toast.error(e.message);
}
Expand Down
9 changes: 9 additions & 0 deletions src/toolbarActions/toolbarFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import parser from '../graph-builder/graphml/parser';
import { actionType as T } from '../reducer';

const getGraphFun = (superState) => superState.curGraphInstance;
const MAX_FILE_SIZE = 10 * 1024 * 1024;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create 10 as a constant instead.
(Then you can compute 1010241024 from that variable)
Then use that constant in the message in line 111. Currently 10 is hard-coded in that error message.

Also, briefly document this in a convenient place in concore-editor.

As in....
"To change the file limit, this is what you do:"
Maybe a newly created DEV-GUIDE.md where we keep documenting similar concore-editor configuration trivia?


const createNode = (state, setState) => {
setState({
Expand Down Expand Up @@ -106,6 +107,10 @@ async function saveGraphMLFile(state) {

const readFile = async (state, setState, file, fileHandle) => {
if (file) {
if (file.size > MAX_FILE_SIZE) {
toast.error('File size exceeds 10MB');
return;
}
const fr = new FileReader();
const projectName = file.name;
if (file.name.split('.').pop() === 'graphml') {
Expand All @@ -127,6 +132,10 @@ const readFile = async (state, setState, file, fileHandle) => {

const readTextFile = (state, setState, file, fileHandle) => {
if (file) {
if (file.size > MAX_FILE_SIZE) {
toast.error('File size exceeds 10MB');
return;
}
setState({
type: T.EDIT_TEXTFILE,
payload: { show: true, fileObj: file, fileHandle },
Expand Down