Skip to content

Commit f82aee2

Browse files
committed
yarn format; create PersistenceHelper.ts for regex
1 parent 128a34a commit f82aee2

32 files changed

+979
-696
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export const defaultEditorValue = '// Type your program in here!';
362362
*
363363
* @param workspaceLocation the location of the workspace, used for context
364364
*/
365-
export const createDefaultWorkspace = (workspaceLocation: WorkspaceLocation): WorkspaceState => ({ // TODO remove default js
365+
export const createDefaultWorkspace = (workspaceLocation: WorkspaceLocation): WorkspaceState => ({
366366
autogradingResults: [],
367367
context: createContext<WorkspaceLocation>(
368368
Constants.defaultSourceChapter,

src/commons/application/actions/SessionActions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ export const setAdminPanelCourseRegistrations = createAction(
208208

209209
export const setGoogleUser = createAction(SET_GOOGLE_USER, (user?: string) => ({ payload: user }));
210210

211-
export const setGoogleAccessToken =
212-
createAction(SET_GOOGLE_ACCESS_TOKEN, (accessToken?: string) => ({ payload: accessToken}));
211+
export const setGoogleAccessToken = createAction(
212+
SET_GOOGLE_ACCESS_TOKEN,
213+
(accessToken?: string) => ({ payload: accessToken })
214+
);
213215

214216
export const setGitHubOctokitObject = createAction(
215217
SET_GITHUB_OCTOKIT_OBJECT,
@@ -226,7 +228,8 @@ export const removeGitHubOctokitObjectAndAccessToken = createAction(
226228
);
227229

228230
export const removeGoogleUserAndAccessToken = createAction(
229-
REMOVE_GOOGLE_USER_AND_ACCESS_TOKEN, () => ({ payload: {} })
231+
REMOVE_GOOGLE_USER_AND_ACCESS_TOKEN,
232+
() => ({ payload: {} })
230233
);
231234

232235
export const submitAnswer = createAction(

src/commons/assessmentWorkspace/AssessmentWorkspace.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,14 @@ const AssessmentWorkspace: React.FC<AssessmentWorkspaceProps> = props => {
423423
const resetWorkspaceOptions = assertType<WorkspaceState>()({
424424
autogradingResults: options.autogradingResults ?? [],
425425
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
426-
editorTabs: [{ value: options.editorValue ?? '', highlightedLines: [], breakpoints: [], githubSaveInfo: {repoName: '', filePath: ''} }],
426+
editorTabs: [
427+
{
428+
value: options.editorValue ?? '',
429+
highlightedLines: [],
430+
breakpoints: [],
431+
githubSaveInfo: { repoName: '', filePath: '' }
432+
}
433+
],
427434
programPrependValue: options.programPrependValue ?? '',
428435
programPostpendValue: options.programPostpendValue ?? '',
429436
editorTestcases: options.editorTestcases ?? []

src/commons/controlBar/ControlBarToggleFolderModeButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const ControlBarToggleFolderModeButton: React.FC<Props> = ({
3232
iconColor: isFolderModeEnabled ? Colors.BLUE4 : undefined
3333
}}
3434
onClick={toggleFolderMode}
35-
isDisabled ={false}
35+
isDisabled={false}
3636
//isDisabled={isSessionActive || isPersistenceActive}
3737
/>
3838
</Tooltip2>

src/commons/controlBar/github/ControlBarGitHubButtons.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Popover2, Tooltip2 } from '@blueprintjs/popover2';
44
import { Octokit } from '@octokit/rest';
55
import React from 'react';
66
import { useResponsive } from 'src/commons/utils/Hooks';
7+
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
78

89
import { GitHubSaveInfo } from '../../../features/github/GitHubTypes';
910
import ControlButton from '../../ControlButton';
10-
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
1111

1212
type Props = {
1313
isFolderModeEnabled: boolean;
@@ -39,7 +39,8 @@ export const ControlBarGitHubButtons: React.FC<Props> = props => {
3939
const hasFilePath = filePath !== '';
4040
const hasOpenFile = isLoggedIn && hasFilePath;
4141

42-
const mainButtonDisplayText = (props.currPersistenceFile && props.currPersistenceFile.name) || 'GitHub';
42+
const mainButtonDisplayText =
43+
(props.currPersistenceFile && hasOpenFile && props.currPersistenceFile.name) || 'GitHub';
4344
let mainButtonIntent: Intent = Intent.NONE;
4445
if (hasOpenFile) {
4546
mainButtonIntent = props.isDirty ? Intent.WARNING : Intent.PRIMARY;

src/commons/fileSystem/FileSystemActions.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { createAction } from '@reduxjs/toolkit';
22
import { FSModule } from 'browserfs/dist/node/core/FS';
33
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
44

5-
import {
5+
import {
66
ADD_GITHUB_SAVE_INFO,
77
ADD_PERSISTENCE_FILE,
88
DELETE_ALL_GITHUB_SAVE_INFO,
9-
DELETE_ALL_PERSISTENCE_FILES, DELETE_GITHUB_SAVE_INFO,
10-
DELETE_PERSISTENCE_FILE,
9+
DELETE_ALL_PERSISTENCE_FILES,
10+
DELETE_GITHUB_SAVE_INFO,
11+
DELETE_PERSISTENCE_FILE,
1112
SET_IN_BROWSER_FILE_SYSTEM,
12-
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
13-
UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH,
13+
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
1414
UPDATE_LAST_EDITED_FILE_PATH,
15-
UPDATE_REFRESH_FILE_VIEW_KEY,
16-
UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH} from './FileSystemTypes';
15+
UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH,
16+
UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH,
17+
UPDATE_REFRESH_FILE_VIEW_KEY
18+
} from './FileSystemTypes';
1719

1820
export const setInBrowserFileSystem = createAction(
1921
SET_IN_BROWSER_FILE_SYSTEM,
@@ -27,50 +29,51 @@ export const addGithubSaveInfo = createAction(
2729

2830
export const deleteGithubSaveInfo = createAction(
2931
DELETE_GITHUB_SAVE_INFO,
30-
(persistenceFile : PersistenceFile) => ({ payload: persistenceFile })
32+
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
3133
);
3234

33-
export const deleteAllGithubSaveInfo = createAction(
34-
DELETE_ALL_GITHUB_SAVE_INFO,
35-
() => ({ payload: {} })
36-
);
35+
export const deleteAllGithubSaveInfo = createAction(DELETE_ALL_GITHUB_SAVE_INFO, () => ({
36+
payload: {}
37+
}));
3738

3839
export const addPersistenceFile = createAction(
3940
ADD_PERSISTENCE_FILE,
40-
( persistenceFile: PersistenceFile ) => ({ payload: persistenceFile })
41+
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
4142
);
4243

4344
export const deletePersistenceFile = createAction(
4445
DELETE_PERSISTENCE_FILE,
45-
( persistenceFile: PersistenceFile ) => ({ payload: persistenceFile })
46+
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
4647
);
4748

4849
export const updatePersistenceFilePathAndNameByPath = createAction(
4950
UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH,
50-
(oldPath: string, newPath: string, newFileName: string) => ({ payload: {oldPath, newPath, newFileName}})
51+
(oldPath: string, newPath: string, newFileName: string) => ({
52+
payload: { oldPath, newPath, newFileName }
53+
})
5154
);
5255

5356
export const updatePersistenceFolderPathAndNameByPath = createAction(
5457
UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH,
55-
(oldPath: string, newPath: string, oldFolderName: string, newFolderName: string) => ({ payload: {oldPath, newPath, oldFolderName, newFolderName}})
58+
(oldPath: string, newPath: string, oldFolderName: string, newFolderName: string) => ({
59+
payload: { oldPath, newPath, oldFolderName, newFolderName }
60+
})
5661
);
5762

58-
export const deleteAllPersistenceFiles = createAction(
59-
DELETE_ALL_PERSISTENCE_FILES,
60-
() => ({ payload: {} })
61-
);
63+
export const deleteAllPersistenceFiles = createAction(DELETE_ALL_PERSISTENCE_FILES, () => ({
64+
payload: {}
65+
}));
6266

6367
export const setPersistenceFileLastEditByPath = createAction(
6468
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
65-
(path: string, date: Date) => ({ payload: {path, date} })
69+
(path: string, date: Date) => ({ payload: { path, date } })
6670
);
6771

6872
export const updateLastEditedFilePath = createAction(
6973
UPDATE_LAST_EDITED_FILE_PATH,
70-
( lastEditedFilePath: string) => ({ payload: {lastEditedFilePath} })
74+
(lastEditedFilePath: string) => ({ payload: { lastEditedFilePath } })
7175
);
7276

73-
export const updateRefreshFileViewKey = createAction(
74-
UPDATE_REFRESH_FILE_VIEW_KEY,
75-
() => ({ payload: {} })
76-
);
77+
export const updateRefreshFileViewKey = createAction(UPDATE_REFRESH_FILE_VIEW_KEY, () => ({
78+
payload: {}
79+
}));

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import { Reducer } from 'redux';
33

44
import { defaultFileSystem } from '../application/ApplicationTypes';
55
import { SourceActionType } from '../utils/ActionsHelper';
6-
import {
6+
import { filePathRegex } from '../utils/PersistenceHelper';
7+
import {
78
addGithubSaveInfo,
89
addPersistenceFile,
910
deleteAllGithubSaveInfo,
10-
deleteAllPersistenceFiles, deleteGithubSaveInfo,
11+
deleteAllPersistenceFiles,
12+
deleteGithubSaveInfo,
1113
deletePersistenceFile,
12-
setInBrowserFileSystem,
14+
setInBrowserFileSystem,
1315
setPersistenceFileLastEditByPath,
1416
updateLastEditedFilePath,
15-
updateRefreshFileViewKey,
1617
updatePersistenceFilePathAndNameByPath,
1718
updatePersistenceFolderPathAndNameByPath,
18-
} from './FileSystemActions';
19+
updateRefreshFileViewKey
20+
} from './FileSystemActions';
1921
import { FileSystemState } from './FileSystemTypes';
2022

2123
export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = createReducer(
@@ -136,50 +138,55 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
136138
// get current level of folder
137139
const regexResult = /^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$/.exec(action.payload.newPath)!;
138140

139-
const currFolderSplit: string[] = regexResult[0].slice(1).split("/");
140-
const currFolderIndex = currFolderSplit.length - 1;
141+
const currFolderSplit: string[] = regexResult[0].slice(1).split('/');
142+
const currFolderIndex = currFolderSplit.length - 1;
141143

142-
// /fold1/ becomes ["fold1"]
143-
// /fold1/fold2/ becomes ["fold1", "fold2"]
144-
// If in top level folder, becomes [""]
144+
// /fold1/ becomes ["fold1"]
145+
// /fold1/fold2/ becomes ["fold1", "fold2"]
146+
// If in top level folder, becomes [""]
145147

146-
console.log(regexResult, currFolderSplit, "a1");
148+
console.log(regexResult, currFolderSplit, 'a1');
147149

148-
// update all files that are its children
149-
state.persistenceFileArray = filesState.filter(e => e.path).map((e => {
150-
const r = /^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$/.exec(e.path!)!;
151-
const currParentFolders = r[0].slice(1).split("/");
152-
console.log("currParentFolders", currParentFolders, "folderLevel", currFolderIndex);
153-
if (currParentFolders.length <= currFolderIndex) {
154-
return e; // not a child of folder
155-
}
156-
if (currParentFolders[currFolderIndex] !== action.payload.oldFolderName) {
157-
return e; // not a child of folder
150+
// update all files that are its children
151+
state.persistenceFileArray = filesState
152+
.filter(e => e.path)
153+
.map(e => {
154+
const r = filePathRegex.exec(e.path!)!;
155+
const currParentFolders = r[0].slice(1).split('/');
156+
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderIndex);
157+
if (currParentFolders.length <= currFolderIndex) {
158+
return e; // not a child of folder
159+
}
160+
if (currParentFolders[currFolderIndex] !== action.payload.oldFolderName) {
161+
return e; // not a child of folder
162+
}
163+
// only children remain
164+
currParentFolders[currFolderIndex] = action.payload.newFolderName;
165+
currParentFolders[0] = '/' + currParentFolders[0];
166+
const newPath = currParentFolders.join('/');
167+
console.log('from', e.path, 'to', newPath);
168+
return { ...e, path: newPath };
169+
});
170+
})
171+
.addCase(setPersistenceFileLastEditByPath, (state, action) => {
172+
const filesState = state['persistenceFileArray'];
173+
const persistenceFileFindIndex = filesState.findIndex(e => e.path === action.payload.path);
174+
if (persistenceFileFindIndex === -1) {
175+
return;
158176
}
159-
// only children remain
160-
currParentFolders[currFolderIndex] = action.payload.newFolderName;
161-
currParentFolders[0] = "/" + currParentFolders[0];
162-
const newPath = currParentFolders.join("/");
163-
console.log("from", e.path, "to", newPath);
164-
return {...e, path: newPath};
165-
}));
166-
})
167-
.addCase(setPersistenceFileLastEditByPath, (state, action) => {
168-
const filesState = state['persistenceFileArray'];
169-
const persistenceFileFindIndex = filesState.findIndex(e => e.path === action.payload.path);
170-
if (persistenceFileFindIndex === -1) {
171-
return;
172-
}
173-
const newPersistenceFile = {...filesState[persistenceFileFindIndex], lastEdit: action.payload.date};
174-
filesState[persistenceFileFindIndex] = newPersistenceFile;
175-
state.persistenceFileArray = filesState;
176-
})
177-
.addCase(updateLastEditedFilePath, (state, action) => {
178-
state.lastEditedFilePath = action.payload.lastEditedFilePath;
179-
})
180-
.addCase(updateRefreshFileViewKey, (state, action) => {
181-
state.refreshFileViewKey = (state.refreshFileViewKey + 1) % 2;
182-
state.lastEditedFilePath = "";
183-
})
177+
const newPersistenceFile = {
178+
...filesState[persistenceFileFindIndex],
179+
lastEdit: action.payload.date
180+
};
181+
filesState[persistenceFileFindIndex] = newPersistenceFile;
182+
state.persistenceFileArray = filesState;
183+
})
184+
.addCase(updateLastEditedFilePath, (state, action) => {
185+
state.lastEditedFilePath = action.payload.lastEditedFilePath;
186+
})
187+
.addCase(updateRefreshFileViewKey, (state, action) => {
188+
state.refreshFileViewKey = (state.refreshFileViewKey + 1) % 2;
189+
state.lastEditedFilePath = '';
190+
});
184191
}
185192
);

src/commons/fileSystem/FileSystemTypes.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export const DELETE_GITHUB_SAVE_INFO = 'DELETE_GITHUB_SAVE_INFO';
88
export const DELETE_PERSISTENCE_FILE = 'DELETE_PERSISTENCE_FILE';
99
export const DELETE_ALL_GITHUB_SAVE_INFO = 'DELETE_ALL_GITHUB_SAVE_INFO';
1010
export const DELETE_ALL_PERSISTENCE_FILES = 'DELETE_ALL_PERSISTENCE_FILES';
11-
export const UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH = 'UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH';
12-
export const UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH = 'UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH';
11+
export const UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH =
12+
'UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH';
13+
export const UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH =
14+
'UPDATE_PERSISTENCE_FOLDER_PATH_AND_NAME_BY_PATH';
1315
export const SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH = 'SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH';
1416
export const UPDATE_LAST_EDITED_FILE_PATH = 'UPDATE_LAST_EDITED_FILE_PATH';
1517
export const UPDATE_REFRESH_FILE_VIEW_KEY = 'UPDATE_REFRESH_FILE_VIEW_KEY';
@@ -18,5 +20,5 @@ export type FileSystemState = {
1820
inBrowserFileSystem: FSModule | null;
1921
persistenceFileArray: PersistenceFile[];
2022
lastEditedFilePath: string;
21-
refreshFileViewKey: integer
23+
refreshFileViewKey: integer;
2224
};

src/commons/fileSystem/FileSystemUtils.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { FSModule } from 'browserfs/dist/node/core/FS';
22
import Stats from 'browserfs/dist/node/core/node_fs_stats';
33
import path from 'path';
44
import { GitHubSaveInfo } from 'src/features/github/GitHubTypes';
5+
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
56
import { store } from 'src/pages/createStore';
67

78
import { WORKSPACE_BASE_PATHS } from '../../pages/fileSystem/createInBrowserFileSystem';
89
import { WorkspaceLocation } from '../workspace/WorkspaceTypes';
9-
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
1010

1111
type File = {
1212
path: string;
@@ -269,19 +269,20 @@ export const writeFileRecursively = (
269269

270270
export const getGithubSaveInfo = () => {
271271
const persistenceFileArray = store.getState().fileSystem.persistenceFileArray;
272-
const {
273-
editorTabs,
274-
activeEditorTabIndex
275-
} = store.getState().workspaces['playground'];
272+
const { editorTabs, activeEditorTabIndex } = store.getState().workspaces['playground'];
276273
let currentFilePath = '';
277274
if (activeEditorTabIndex !== null) {
278275
currentFilePath = editorTabs[activeEditorTabIndex].filePath || '';
279276
}
280-
const PersistenceFile: PersistenceFile = persistenceFileArray.find(e => e.path === currentFilePath) || {name: '', id: '', repoName: ''};
281-
const githubSaveInfo: GitHubSaveInfo = {
282-
filePath: PersistenceFile.path,
283-
lastSaved: PersistenceFile.lastSaved,
284-
repoName: PersistenceFile.repoName || (persistenceFileArray[0] === undefined ? '' : persistenceFileArray[0].repoName)
277+
const PersistenceFile: PersistenceFile = persistenceFileArray.find(
278+
e => e.path === currentFilePath
279+
) || { name: '', id: '', repoName: '' };
280+
const githubSaveInfo: GitHubSaveInfo = {
281+
filePath: PersistenceFile.path,
282+
lastSaved: PersistenceFile.lastSaved,
283+
repoName:
284+
PersistenceFile.repoName ||
285+
(persistenceFileArray[0] === undefined ? '' : persistenceFileArray[0].repoName)
285286
};
286287
return githubSaveInfo;
287288
}

src/commons/fileSystemView/FileSystemView.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ type Props = {
1919
isContextMenuDisabled: boolean;
2020
};
2121

22-
const FileSystemView: React.FC<Props> = ({ workspaceLocation, basePath, lastEditedFilePath, isContextMenuDisabled }) => {
22+
const FileSystemView: React.FC<Props> = ({
23+
workspaceLocation,
24+
basePath,
25+
lastEditedFilePath,
26+
isContextMenuDisabled
27+
}) => {
2328
const fileSystem = useTypedSelector(state => state.fileSystem.inBrowserFileSystem);
2429
const persistenceFileArray = useTypedSelector(state => state.fileSystem.persistenceFileArray);
2530

26-
console.log("lefp", lastEditedFilePath, "pfa", persistenceFileArray);
31+
console.log('lefp', lastEditedFilePath, 'pfa', persistenceFileArray);
2732

2833
const [isAddingNewFile, setIsAddingNewFile] = React.useState(false);
2934
const [isAddingNewDirectory, setIsAddingNewDirectory] = React.useState(false);

0 commit comments

Comments
 (0)