Skip to content

Commit 740d14e

Browse files
fixed numerous bugs such as opening single file not working, made opening folders behaviour correct(it would previously open the top level folder of the selected folder)
refactored some code as well
1 parent edaeb01 commit 740d14e

File tree

8 files changed

+589
-238
lines changed

8 files changed

+589
-238
lines changed

src/commons/fileSystem/FileSystemActions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createAction } from '@reduxjs/toolkit';
22
import { FSModule } from 'browserfs/dist/node/core/FS';
3-
import { GitHubSaveInfo } from 'src/features/github/GitHubTypes';
43
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
54

65
import {
@@ -23,7 +22,7 @@ export const setInBrowserFileSystem = createAction(
2322

2423
export const addGithubSaveInfo = createAction(
2524
ADD_GITHUB_SAVE_INFO,
26-
(githubSaveInfo: GitHubSaveInfo) => ({ payload: { githubSaveInfo }})
25+
(persistenceFile: PersistenceFile) => ({ payload: { persistenceFile }})
2726
);
2827

2928
export const deleteGithubSaveInfo = createAction(

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,31 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
2626
state.inBrowserFileSystem = action.payload.inBrowserFileSystem;
2727
})
2828
.addCase(addGithubSaveInfo, (state, action) => { // TODO rewrite
29-
const githubSaveInfoPayload = action.payload.githubSaveInfo;
29+
const persistenceFilePayload = action.payload.persistenceFile;
3030
const persistenceFileArray = state['persistenceFileArray'];
3131

3232
const saveInfoIndex = persistenceFileArray.findIndex(e => {
33-
return e.path === githubSaveInfoPayload.filePath &&
34-
e.repoName === githubSaveInfoPayload.repoName;
33+
return e.path === persistenceFilePayload.path &&
34+
e.repoName === persistenceFilePayload.repoName;
3535
});
3636
if (saveInfoIndex === -1) {
3737
persistenceFileArray[persistenceFileArray.length] = {
3838
id: '',
3939
name: '',
40-
path: githubSaveInfoPayload.filePath,
41-
lastSaved: githubSaveInfoPayload.lastSaved,
42-
repoName: githubSaveInfoPayload.repoName
40+
path: persistenceFilePayload.path,
41+
lastSaved: persistenceFilePayload.lastSaved,
42+
repoName: persistenceFilePayload.repoName,
43+
parentFolderPath: persistenceFilePayload.parentFolderPath
4344
};
4445
} else {
4546
// file already exists, to replace file
4647
persistenceFileArray[saveInfoIndex] = {
4748
id: '',
4849
name: '',
49-
path: githubSaveInfoPayload.filePath,
50-
lastSaved: githubSaveInfoPayload.lastSaved,
51-
repoName: githubSaveInfoPayload.repoName
50+
path: persistenceFilePayload.path,
51+
lastSaved: persistenceFilePayload.lastSaved,
52+
repoName: persistenceFilePayload.repoName,
53+
parentFolderPath: persistenceFilePayload.parentFolderPath
5254
};
5355
}
5456
state.persistenceFileArray = persistenceFileArray;
@@ -57,15 +59,42 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
5759
const newPersistenceFileArray = state['persistenceFileArray'].filter(e => e.path !== action.payload.path);
5860
const isGDriveSyncing = action.payload.id ? true: false;
5961
if (isGDriveSyncing) {
60-
const newPersFile = { id: action.payload.id, path: action.payload.path, repoName: '', name: action.payload.name};
62+
const newPersFile = {
63+
id: action.payload.id,
64+
name: action.payload.name,
65+
lastEdit: action.payload.lastEdit,
66+
lastSaved: action.payload.lastSaved,
67+
parentId: action.payload.parentId,
68+
path: action.payload.path
69+
};
6170
const newPersFileArray = newPersistenceFileArray.concat(newPersFile);
6271
state.persistenceFileArray = newPersFileArray;
6372
} else {
6473
state.persistenceFileArray = newPersistenceFileArray;
6574
}
6675
})
6776
.addCase(deleteAllGithubSaveInfo, (state, action) => {
68-
state.persistenceFileArray = [];
77+
if (state.persistenceFileArray.length !== 0) {
78+
const isGDriveSyncing = state.persistenceFileArray[0].id ? true: false;
79+
const newPersistenceFileArray = state.persistenceFileArray;
80+
if (isGDriveSyncing) {
81+
newPersistenceFileArray.forEach(
82+
(persistenceFile, index) => {
83+
newPersistenceFileArray[index] = {
84+
id: persistenceFile.id,
85+
name: persistenceFile.name,
86+
lastEdit: persistenceFile.lastEdit,
87+
lastSaved: persistenceFile.lastSaved,
88+
parentId: persistenceFile.parentId,
89+
path: persistenceFile.path
90+
}
91+
}
92+
)
93+
state.persistenceFileArray = newPersistenceFileArray;
94+
} else {
95+
state.persistenceFileArray = [];
96+
}
97+
}
6998
})
7099
.addCase(addPersistenceFile, (state, action) => { // TODO rewrite
71100
const persistenceFilePayload = action.payload;

src/commons/fileSystem/FileSystemUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,14 @@ export const getGithubSaveInfo = () => {
285285
};
286286
return githubSaveInfo;
287287
}
288+
289+
export const getPersistenceFile = (filePath: string) => {
290+
const persistenceFileArray = store.getState().fileSystem.persistenceFileArray;
291+
if (filePath === '') {
292+
const persistenceFile = persistenceFileArray[0];
293+
return persistenceFile;
294+
}
295+
const persistenceFile = persistenceFileArray.find(e => e.path === filePath);
296+
297+
return persistenceFile;
298+
}

src/commons/fileSystemView/FileSystemViewDirectoryNode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FSModule } from 'browserfs/dist/node/core/FS';
44
import path from 'path';
55
import React from 'react';
66
import { useDispatch } from 'react-redux';
7-
import { persistenceCreateFile, persistenceCreateFolder, persistenceDeleteFolder } from 'src/features/persistence/PersistenceActions';
7+
import { persistenceCreateFolder, persistenceDeleteFolder } from 'src/features/persistence/PersistenceActions';
88
import classes from 'src/styles/FileSystemView.module.scss';
99

1010
import { rmdirRecursively } from '../fileSystem/FileSystemUtils';
@@ -121,7 +121,7 @@ const FileSystemViewDirectoryNode: React.FC<Props> = ({
121121
if (err) {
122122
console.error(err);
123123
}
124-
dispatch(persistenceCreateFile(newFilePath));
124+
// dispatch(persistenceCreateFile(newFilePath));
125125
dispatch(githubCreateFile(newFilePath));
126126
forceRefreshFileSystemViewList();
127127
});

src/commons/gitHubOverlay/FileExplorerDialog.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import classNames from 'classnames';
1414
import React, { useEffect, useState } from 'react';
1515

1616
import {
17+
checkFolderLocationIsValid,
1718
checkIfFileCanBeOpened,
1819
checkIfFileCanBeSavedAndGetSaveType,
1920
checkIfUserAgreesToOverwriteEditorData,
@@ -22,10 +23,12 @@ import {
2223
openFileInEditor,
2324
openFolderInFolderMode,
2425
performCreatingSave,
25-
performOverwritingSave
26+
performMultipleCreatingSave,
27+
performOverwritingSaveForSaveAs
2628
} from '../../features/github/GitHubUtils';
2729
import { GitHubFileNodeData } from './GitHubFileNodeData';
2830
import { GitHubTreeNodeCreator } from './GitHubTreeNodeCreator';
31+
import { getPersistenceFile } from '../fileSystem/FileSystemUtils';
2932

3033
export type FileExplorerDialogProps = {
3134
repoName: string;
@@ -47,7 +50,7 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
4750
return (
4851
<Dialog className="githubDialog" isOpen={true} onClose={handleClose}>
4952
<div className={classNames('githubDialogHeader', Classes.DIALOG_HEADER)}>
50-
<h3>Select a File</h3>
53+
<h3>Select a File/Folder</h3>
5154
</div>
5255
<div className={Classes.DIALOG_BODY}>
5356
<Tree
@@ -117,8 +120,13 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
117120
}
118121
}
119122

120-
if (props.pickerType == 'Saveall') {
121-
123+
if (props.pickerType === 'Save All') {
124+
if (await checkIsFile(props.octokit, githubLoginID, props.repoName, filePath)) {
125+
} else {
126+
if (await checkFolderLocationIsValid(props.octokit, githubLoginID, props.repoName, filePath)) {
127+
performMultipleCreatingSave(props.octokit, githubLoginID, props.repoName, filePath, githubName, githubEmail, '');
128+
}
129+
}
122130
}
123131

124132
if (props.pickerType === 'Save') {
@@ -131,7 +139,7 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
131139

132140
if (canBeSaved) {
133141
if (saveType === 'Overwrite' && (await checkIfUserAgreesToPerformOverwritingSave())) {
134-
performOverwritingSave(
142+
performOverwritingSaveForSaveAs(
135143
props.octokit,
136144
githubLoginID,
137145
props.repoName,
@@ -144,15 +152,24 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
144152
}
145153

146154
if (saveType === 'Create') {
155+
const persistenceFile = getPersistenceFile(filePath);
156+
if (persistenceFile === undefined) {
157+
throw new Error("persistence file not found for this filepath: " + filePath);
158+
}
159+
const parentFolderPath = persistenceFile.parentFolderPath;
160+
if (parentFolderPath === undefined) {
161+
throw new Error("repository name or parentfolderpath not found for this persistencefile: " + persistenceFile);
162+
}
147163
performCreatingSave(
148164
props.octokit,
149165
githubLoginID,
150166
props.repoName,
151-
filePath,
167+
filePath.slice(parentFolderPath.length),
152168
githubName,
153169
githubEmail,
154170
commitMessage,
155-
props.editorContent
171+
props.editorContent,
172+
parentFolderPath
156173
);
157174
}
158175
}

0 commit comments

Comments
 (0)