Skip to content

Commit 096cec4

Browse files
fixed fileview colour to update on saving and fixed github opening folders bug where repeatedly opening the same folder would lead to errors
1 parent ddb2e26 commit 096cec4

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,9 @@ export const createDefaultStoriesEnv = (
548548

549549
export const defaultFileSystem: FileSystemState = {
550550
inBrowserFileSystem: null,
551-
persistenceFileArray: []
551+
persistenceFileArray: [],
552+
lastEditedFilePath: '',
553+
refreshFileViewKey: 0
552554
};
553555

554556
export const defaultSideContent: SideContentState = {

src/commons/fileSystem/FileSystemActions.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
DELETE_PERSISTENCE_FILE,
1212
SET_IN_BROWSER_FILE_SYSTEM,
1313
UPDATE_GITHUB_SAVE_INFO,
14-
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH } from './FileSystemTypes';
14+
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
15+
UPDATE_LAST_EDITED_FILE_PATH,
16+
UPDATE_REFRESH_FILE_VIEW_KEY} from './FileSystemTypes';
1517

1618
export const setInBrowserFileSystem = createAction(
1719
SET_IN_BROWSER_FILE_SYSTEM,
@@ -56,5 +58,15 @@ export const deleteAllPersistenceFiles = createAction(
5658

5759
export const setPersistenceFileLastEditByPath = createAction(
5860
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
59-
(path: string, date: Date) => ({ payload: {path, date}})
61+
(path: string, date: Date) => ({ payload: {path, date} })
62+
);
63+
64+
export const updateLastEditedFilePath = createAction(
65+
UPDATE_LAST_EDITED_FILE_PATH,
66+
( lastEditedFilePath: string) => ({ payload: {lastEditedFilePath} })
67+
);
68+
69+
export const updateRefreshFileViewKey = createAction(
70+
UPDATE_REFRESH_FILE_VIEW_KEY,
71+
() => ({ payload: {} })
6072
);

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
deletePersistenceFile,
1212
setInBrowserFileSystem,
1313
setPersistenceFileLastEditByPath,
14+
updateLastEditedFilePath,
15+
updateRefreshFileViewKey,
1416
} from './FileSystemActions';
1517
import { FileSystemState } from './FileSystemTypes';
1618

@@ -88,5 +90,11 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
8890
filesState[persistenceFileFindIndex] = newPersistenceFile;
8991
state.persistenceFileArray = filesState;
9092
})
93+
.addCase(updateLastEditedFilePath, (state, action) => {
94+
state.lastEditedFilePath = action.payload.lastEditedFilePath;
95+
})
96+
.addCase(updateRefreshFileViewKey, (state, action) => {
97+
state.refreshFileViewKey = (state.refreshFileViewKey + 1) % 2;
98+
})
9199
}
92100
);

src/commons/fileSystem/FileSystemTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export const DELETE_ALL_GITHUB_SAVE_INFO = 'DELETE_ALL_GITHUB_SAVE_INFO';
1010
export const UPDATE_GITHUB_SAVE_INFO = 'UPDATE_GITHUB_SAVE_INFO';
1111
export const DELETE_ALL_PERSISTENCE_FILES = 'DELETE_ALL_PERSISTENCE_FILES';
1212
export const SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH = 'SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH';
13+
export const UPDATE_LAST_EDITED_FILE_PATH = 'UPDATE_LAST_EDITED_FILE_PATH';
14+
export const UPDATE_REFRESH_FILE_VIEW_KEY = 'UPDATE_REFRESH_FILE_VIEW_KEY';
1315

1416
export type FileSystemState = {
1517
inBrowserFileSystem: FSModule | null;
1618
persistenceFileArray: PersistenceFile[];
19+
lastEditedFilePath: string;
20+
refreshFileViewKey: integer
1721
};

src/commons/fileSystemView/FileSystemViewList.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ const FileSystemViewList: React.FC<Props> = ({
7474
});
7575
};
7676

77-
refreshFileView = readDirectory;
78-
7977
React.useEffect(readDirectory, [fileSystem, basePath]);
8078

8179
if (!fileNames || !dirNames) {

src/commons/sagas/GitHubPersistenceSaga.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function* githubSaveFile(): any {
119119
const githubLoginId = authUser.data.login;
120120
const githubSaveInfo = getGithubSaveInfo();
121121
const repoName = githubSaveInfo.repoName;
122-
const filePath = githubSaveInfo.filePath;
122+
const filePath = githubSaveInfo.filePath || '';
123123
const githubEmail = authUser.data.email;
124124
const githubName = authUser.data.name;
125125
const commitMessage = 'Changes made from Source Academy';
@@ -138,12 +138,15 @@ function* githubSaveFile(): any {
138138
octokit,
139139
githubLoginId,
140140
repoName || '',
141-
filePath || '',
141+
filePath,
142142
githubEmail,
143143
githubName,
144144
commitMessage,
145145
content
146146
);
147+
148+
// forces lasteditedfilepath in filesystem to be updated which causes the colors to be updated
149+
store.dispatch(actions.updateLastEditedFilePath(''));
147150
}
148151

149152
function* githubSaveFileAs(): any {
@@ -195,6 +198,7 @@ function* githubSaveFileAs(): any {
195198

196199
yield call(promisifiedFileExplorer);
197200
}
201+
198202
}
199203

200204
function* githubSaveAll(): any {
@@ -237,6 +241,8 @@ function* githubSaveAll(): any {
237241
githubName,
238242
{ commitMessage: commitMessage, files: modifiedcurrFiles});
239243

244+
// forces lasteditedfilepath in filesystem to be updated which causes the colors to be updated
245+
store.dispatch(actions.updateLastEditedFilePath(''));
240246
}
241247

242248
export default GitHubPersistenceSaga;

src/features/github/GitHubUtils.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
showWarningMessage
1515
} from '../../commons/utils/notifications/NotificationsHelper';
1616
import { store } from '../../pages/createStore';
17+
import { WORKSPACE_BASE_PATHS } from 'src/pages/fileSystem/createInBrowserFileSystem';
18+
import { updateRefreshFileViewKey } from 'src/commons/fileSystem/FileSystemActions';
1719

1820
/**
1921
* Exchanges the Access Code with the back-end to receive an Auth-Token
@@ -234,6 +236,9 @@ export async function openFileInEditor(
234236
))
235237
showSuccessMessage('Successfully loaded file!', 1000);
236238
}
239+
240+
//refreshes editor tabs
241+
store.dispatch(actions.removeEditorTabsForDirectory("playground", WORKSPACE_BASE_PATHS["playground"])); // TODO hardcoded
237242
}
238243

239244
export async function openFolderInFolderMode(
@@ -290,9 +295,12 @@ export async function openFolderInFolderMode(
290295
const readFile = async (files: Array<string>) => {
291296
console.log(files);
292297
console.log(filePath);
293-
rmFilesInDirRecursively(fileSystem, "/playground");
294298
let promise = Promise.resolve();
299+
console.log("removing files");
300+
await rmFilesInDirRecursively(fileSystem, "/playground");
301+
console.log("files removed");
295302
type GetContentResponse = GetResponseTypeFromEndpointMethod<typeof octokit.repos.getContent>;
303+
console.log("starting to add files");
296304
files.forEach((file: string) => {
297305
promise = promise.then(async () => {
298306
let results = {} as GetContentResponse;
@@ -311,7 +319,7 @@ export async function openFolderInFolderMode(
311319
if (content) {
312320
const fileContent = Buffer.from(content, 'base64').toString();
313321
console.log(file);
314-
writeFileRecursively(fileSystem, "/playground/" + file, fileContent);
322+
await writeFileRecursively(fileSystem, "/playground/" + file, fileContent);
315323
store.dispatch(actions.addGithubSaveInfo(
316324
{
317325
repoName: repoName,
@@ -327,13 +335,19 @@ export async function openFolderInFolderMode(
327335
})
328336
promise.then(() => {
329337
store.dispatch(actions.playgroundUpdateRepoName(repoName));
330-
console.log("promises fulfilled");
338+
console.log("promises fulfilled");
339+
// store.dispatch(actions.setFolderMode('playground', true));
340+
store.dispatch(updateRefreshFileViewKey());
341+
console.log("refreshed");
331342
refreshFileView();
332343
showSuccessMessage('Successfully loaded file!', 1000);
333344
})
334345
}
335346

336347
readFile(files);
348+
349+
//refreshes editor tabs
350+
store.dispatch(actions.removeEditorTabsForDirectory("playground", WORKSPACE_BASE_PATHS["playground"])); // TODO hardcoded
337351
}
338352

339353
export async function performOverwritingSave(

src/pages/playground/Playground.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ import {
138138
makeSubstVisualizerTabFrom,
139139
mobileOnlyTabIds
140140
} from './PlaygroundTabs';
141-
import { setPersistenceFileLastEditByPath } from 'src/commons/fileSystem/FileSystemActions';
141+
import { setPersistenceFileLastEditByPath, updateLastEditedFilePath } from 'src/commons/fileSystem/FileSystemActions';
142142

143143
export type PlaygroundProps = {
144144
isSicpEditor?: boolean;
@@ -409,7 +409,8 @@ const Playground: React.FC<PlaygroundProps> = props => {
409409
[isGreen]
410410
);
411411

412-
const [lastEditedFilePath, setLastEditedFilePath] = useState<string>("");
412+
const lastEditedFilePath = useTypedSelector(state => state.fileSystem.lastEditedFilePath);
413+
const refreshFileViewKey = useTypedSelector(state => state.fileSystem.refreshFileViewKey);
413414

414415
const onEditorValueChange = (editorTabIndex: number, newEditorValue: string) => {
415416
const filePath = editorTabs[editorTabIndex]?.filePath;
@@ -418,7 +419,7 @@ const Playground: React.FC<PlaygroundProps> = props => {
418419
//console.log(editorTabs);
419420
console.log("dispatched " + filePath);
420421
dispatch(setPersistenceFileLastEditByPath(filePath, editDate));
421-
setLastEditedFilePath(filePath);
422+
dispatch(updateLastEditedFilePath(filePath));
422423
}
423424
setLastEdit(editDate);
424425
// TODO change editor tab label to reflect path of opened file?
@@ -1005,6 +1006,7 @@ const Playground: React.FC<PlaygroundProps> = props => {
10051006
basePath={WORKSPACE_BASE_PATHS[workspaceLocation]}
10061007
lastEditedFilePath={lastEditedFilePath}
10071008
isContextMenuDisabled={isContextMenuDisabled}
1009+
key={refreshFileViewKey}
10081010
/>
10091011
),
10101012
iconName: IconNames.FOLDER_CLOSE,
@@ -1014,7 +1016,7 @@ const Playground: React.FC<PlaygroundProps> = props => {
10141016
: [])
10151017
]
10161018
};
1017-
}, [isFolderModeEnabled, workspaceLocation, lastEditedFilePath, isContextMenuDisabled]);
1019+
}, [isFolderModeEnabled, workspaceLocation, lastEditedFilePath, isContextMenuDisabled, refreshFileViewKey]);
10181020

10191021
const workspaceProps: WorkspaceProps = {
10201022
controlBarProps: {

0 commit comments

Comments
 (0)