Skip to content

Commit f5531af

Browse files
authored
Merge pull request #368 from avinxshKD/fix/non-chrome-fallback
Fix/non chrome fallback
2 parents c65cc04 + bfc20cf commit f5531af

3 files changed

Lines changed: 47 additions & 28 deletions

File tree

src/component/fileBrowser.jsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,34 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
9494
};
9595

9696
const newFeature = async () => {
97-
const dirHandle = await window.showDirectoryPicker();
98-
let state = [];
99-
// eslint-disable-next-line no-restricted-syntax
100-
for await (const [key, value] of dirHandle.entries()) {
101-
if (value.kind === 'file') {
102-
const fileData = await value.getFile();
103-
state = state.concat([{
104-
key: `${dirHandle.name}/${key}`,
105-
modified: fileData.lastModified,
106-
size: fileData.size,
107-
fileObj: fileData,
108-
fileHandle: value,
109-
}]);
110-
} else if (value.kind === 'directory') {
111-
const res = await handleFileInDirs(dirHandle.name, value);
112-
state = state.concat(res);
97+
try {
98+
const dirHandle = await window.showDirectoryPicker();
99+
let state = [];
100+
// eslint-disable-next-line no-restricted-syntax
101+
for await (const [key, value] of dirHandle.entries()) {
102+
if (value.kind === 'file') {
103+
const fileData = await value.getFile();
104+
state = state.concat([{
105+
key: `${dirHandle.name}/${key}`,
106+
modified: fileData.lastModified,
107+
size: fileData.size,
108+
fileObj: fileData,
109+
fileHandle: value,
110+
}]);
111+
} else if (value.kind === 'directory') {
112+
const res = await handleFileInDirs(dirHandle.name, value);
113+
state = state.concat(res);
114+
}
115+
}
116+
setFileState([]);
117+
setFileState(state);
118+
dispatcher({ type: T.SET_DIR_NAME, payload: state[0].key.split('/')[0] });
119+
dispatcher({ type: T.SET_FILE_STATE, payload: state });
120+
} catch (error) {
121+
if (error.name !== 'AbortError') {
122+
toast.info('Switch to Edge/Chrome!');
113123
}
114124
}
115-
setFileState([]);
116-
setFileState(state);
117-
dispatcher({ type: T.SET_DIR_NAME, payload: state[0].key.split('/')[0] });
118-
dispatcher({ type: T.SET_FILE_STATE, payload: state });
119125
};
120126

121127
const newFeatureFile = async () => {
@@ -161,6 +167,10 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
161167
onChange={(e) => {
162168
const { files } = e.target;
163169
if (files && files.length > 0) {
170+
if (!files[0].webkitRelativePath) {
171+
toast.info('Switch to Edge/Chrome!');
172+
return;
173+
}
164174
tempFilesRef.current = files;
165175
const folderName = files[0].webkitRelativePath.split('/')[0];
166176
setPendingFolderName(folderName);

src/component/modals/FileEdit.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const FileEditModal = ({ superState, dispatcher }) => {
1616
const [showFilenameModal, setShowFilenameModal] = useState(false);
1717

1818
useEffect(() => {
19-
if (navigator.userAgent.indexOf('Edg') !== -1 || navigator.userAgent.indexOf('Chrome') !== -1) {
19+
if ('showSaveFilePicker' in window) {
2020
setDirButton(true);
2121
}
2222
}, []);
@@ -28,11 +28,16 @@ const FileEditModal = ({ superState, dispatcher }) => {
2828
};
2929

3030
async function submit() {
31-
if (superState.fileHandle) {
31+
if (!superState.fileHandle || !superState.fileHandle.createWritable) {
32+
toast.warn('Switch to Edge/Chrome!');
33+
dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
34+
return;
35+
}
36+
try {
3237
const stream = await superState.fileHandle.createWritable();
3338
await stream.write(codeStuff);
3439
await stream.close();
35-
} else {
40+
} catch (error) {
3641
toast.warn('Switch to Edge/Chrome!');
3742
}
3843
dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });

src/toolbarActions/toolbarFunctions.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ const saveAction = (state) => {
8585
async function saveGraphMLFile(state) {
8686
if (state.curGraphInstance) {
8787
const graph = state.graphs[state.curGraphIndex];
88-
if (graph.fileHandle) {
89-
const stream = await graph.fileHandle.createWritable();
90-
await stream.write(getGraphFun(state).saveToFolder());
91-
await stream.close();
92-
toast.success('File saved Successfully');
88+
if (graph.fileHandle && graph.fileHandle.createWritable) {
89+
try {
90+
const stream = await graph.fileHandle.createWritable();
91+
await stream.write(getGraphFun(state).saveToFolder());
92+
await stream.close();
93+
toast.success('File saved Successfully');
94+
} catch (error) {
95+
getGraphFun(state).saveWithoutFileHandle();
96+
}
9397
} else if (!graph.fileHandle) {
9498
getGraphFun(state).saveWithoutFileHandle();
9599
} else {

0 commit comments

Comments
 (0)