Skip to content

Commit 4eea489

Browse files
Merge branch 'folders-public-wip' of github.com:source-academy/frontend into folders-public-wip
2 parents efe840d + a2386df commit 4eea489

File tree

8 files changed

+105
-65
lines changed

8 files changed

+105
-65
lines changed

src/commons/controlBar/ControlBarGoogleDriveButtons.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const stateToIntent: { [state in PersistenceState]: Intent } = {
1515

1616
type Props = {
1717
isFolderModeEnabled: boolean;
18+
workspaceLocation: string;
1819
loggedInAs?: string;
1920
accessToken?: string;
2021
currPersistenceFile?: PersistenceFile;
@@ -35,12 +36,13 @@ export const ControlBarGoogleDriveButtons: React.FC<Props> = props => {
3536
? 'DIRTY'
3637
: 'SAVED'
3738
: 'INACTIVE';
39+
const isNotPlayground = props.workspaceLocation !== "playground";
3840
const mainButton = (
3941
<ControlButton
4042
label={(props.currPersistenceFile && props.currPersistenceFile.name) || 'Google Drive'}
4143
icon={IconNames.CLOUD}
4244
options={{ intent: stateToIntent[state] }}
43-
//isDisabled={props.isFolderModeEnabled}
45+
isDisabled={isNotPlayground}
4446
/>
4547
);
4648
const openButton = (
@@ -86,13 +88,12 @@ export const ControlBarGoogleDriveButtons: React.FC<Props> = props => {
8688
<ControlButton label="Log In" icon={IconNames.LOG_IN} onClick={props.onClickLogIn} />
8789
);
8890

89-
//const tooltipContent = props.isFolderModeEnabled
90-
// ? 'Currently unsupported in Folder mode'
91-
// : undefined;
92-
const tooltipContent = undefined;
91+
const tooltipContent = isNotPlayground
92+
? 'Currently unsupported in non playground workspaces'
93+
: undefined;
9394

9495
return (
95-
<Tooltip2 content={tooltipContent} disabled={false}>
96+
<Tooltip2 content={tooltipContent} disabled={tooltipContent === undefined}>
9697
<Popover2
9798
autoFocus={false}
9899
content={
@@ -108,7 +109,7 @@ export const ControlBarGoogleDriveButtons: React.FC<Props> = props => {
108109
}
109110
onOpening={props.onPopoverOpening}
110111
popoverClassName={Classes.POPOVER_DISMISS}
111-
//disabled={props.isFolderModeEnabled}
112+
disabled={isNotPlayground}
112113
>
113114
{mainButton}
114115
</Popover2>

src/commons/controlBar/github/ControlBarGitHubButtons.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ControlButton from '../../ControlButton';
1111

1212
type Props = {
1313
isFolderModeEnabled: boolean;
14+
workspaceLocation: string;
1415
currPersistenceFile?: PersistenceFile;
1516
loggedInAs?: Octokit;
1617
githubSaveInfo: GitHubSaveInfo;
@@ -34,6 +35,8 @@ export const ControlBarGitHubButtons: React.FC<Props> = props => {
3435

3536
const filePath = props.githubSaveInfo.filePath || '';
3637

38+
const isNotPlayground = props.workspaceLocation !== "playground";
39+
3740
const isLoggedIn = props.loggedInAs !== undefined;
3841
const shouldDisableButtons = !isLoggedIn;
3942
const hasFilePath = filePath !== '';
@@ -51,7 +54,7 @@ export const ControlBarGitHubButtons: React.FC<Props> = props => {
5154
label={mainButtonDisplayText}
5255
icon={IconNames.GIT_BRANCH}
5356
options={{ intent: mainButtonIntent }}
54-
//isDisabled={props.isFolderModeEnabled}
57+
isDisabled={isNotPlayground}
5558
/>
5659
);
5760

@@ -97,13 +100,12 @@ export const ControlBarGitHubButtons: React.FC<Props> = props => {
97100
<ControlButton label="Log In" icon={IconNames.LOG_IN} onClick={props.onClickLogIn} />
98101
);
99102

100-
//const tooltipContent = props.isFolderModeEnabled
101-
// ? 'Currently unsupported in Folder mode'
102-
// : undefined;
103-
const tooltipContent = undefined;
103+
const tooltipContent = isNotPlayground
104+
? 'Currently unsupported in non playground workspaces'
105+
: undefined;
104106

105107
return (
106-
<Tooltip2 content={tooltipContent} disabled={false}>
108+
<Tooltip2 content={tooltipContent} disabled={tooltipContent === undefined}>
107109
<Popover2
108110
autoFocus={false}
109111
content={
@@ -118,7 +120,7 @@ export const ControlBarGitHubButtons: React.FC<Props> = props => {
118120
</div>
119121
}
120122
popoverClassName={Classes.POPOVER_DISMISS}
121-
//disabled={props.isFolderModeEnabled}
123+
disabled={isNotPlayground}
122124
>
123125
{mainButton}
124126
</Popover2>

src/commons/fileSystem/FileSystemActions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DELETE_ALL_PERSISTENCE_FILES,
1010
DELETE_GITHUB_SAVE_INFO,
1111
DELETE_PERSISTENCE_FILE,
12+
DELETE_PERSISTENCE_FOLDER_AND_CHILDREN,
1213
SET_IN_BROWSER_FILE_SYSTEM,
1314
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
1415
UPDATE_LAST_EDITED_FILE_PATH,
@@ -46,6 +47,11 @@ export const deletePersistenceFile = createAction(
4647
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
4748
);
4849

50+
export const deletePersistenceFolderAndChildren = createAction(
51+
DELETE_PERSISTENCE_FOLDER_AND_CHILDREN,
52+
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
53+
);
54+
4955
export const updatePersistenceFilePathAndNameByPath = createAction(
5056
UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH,
5157
(oldPath: string, newPath: string, newFileName: string) => ({

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
deleteAllPersistenceFiles,
1212
deleteGithubSaveInfo,
1313
deletePersistenceFile,
14+
deletePersistenceFolderAndChildren,
1415
setInBrowserFileSystem,
1516
setPersistenceFileLastEditByPath,
1617
updateLastEditedFilePath,
@@ -133,6 +134,33 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
133134
state.persistenceFileArray = newPersistenceFileArray;
134135
}
135136
})
137+
.addCase(deletePersistenceFolderAndChildren, (state, action) => { // check if github is syncing?
138+
const newPersistenceFileArray = state['persistenceFileArray'].filter(
139+
e => e.id !== action.payload.id
140+
);
141+
// get children
142+
// get current level of folder
143+
const regexResult = filePathRegex.exec(action.payload.path!)!;
144+
145+
const currFolderSplit: string[] = regexResult[0].slice(1).split('/');
146+
const folderName = regexResult[2];
147+
const currFolderLevel = currFolderSplit.length - 1;
148+
149+
state.persistenceFileArray = newPersistenceFileArray
150+
.filter(e => e.path)
151+
.filter(e => {
152+
const r = filePathRegex.exec(e.path!)!;
153+
const currParentFolders = r[0].slice(1).split('/');
154+
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderLevel);
155+
if (currParentFolders.length <= currFolderLevel) {
156+
return true; // not a child of folder
157+
}
158+
if (currParentFolders[currFolderLevel] !== folderName) {
159+
return true; // not a child of folder
160+
}
161+
return false;
162+
});
163+
})
136164
.addCase(deleteAllPersistenceFiles, (state, action) => {
137165
state.persistenceFileArray = [];
138166
})
@@ -158,7 +186,7 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
158186
const regexResult = filePathRegex.exec(action.payload.newPath)!;
159187

160188
const currFolderSplit: string[] = regexResult[0].slice(1).split('/');
161-
const currFolderIndex = currFolderSplit.length - 1;
189+
const currFolderLevel = currFolderSplit.length - 1;
162190

163191
// /fold1/ becomes ["fold1"]
164192
// /fold1/fold2/ becomes ["fold1", "fold2"]
@@ -172,15 +200,15 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
172200
.map(e => {
173201
const r = filePathRegex.exec(e.path!)!;
174202
const currParentFolders = r[0].slice(1).split('/');
175-
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderIndex);
176-
if (currParentFolders.length <= currFolderIndex) {
203+
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderLevel);
204+
if (currParentFolders.length <= currFolderLevel) {
177205
return e; // not a child of folder
178206
}
179-
if (currParentFolders[currFolderIndex] !== action.payload.oldFolderName) {
207+
if (currParentFolders[currFolderLevel] !== action.payload.oldFolderName) {
180208
return e; // not a child of folder
181209
}
182210
// only children remain
183-
currParentFolders[currFolderIndex] = action.payload.newFolderName;
211+
currParentFolders[currFolderLevel] = action.payload.newFolderName;
184212
currParentFolders[0] = '/' + currParentFolders[0];
185213
const newPath = currParentFolders.join('/');
186214
console.log('from', e.path, 'to', newPath);

src/commons/fileSystem/FileSystemTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const ADD_GITHUB_SAVE_INFO = 'ADD_GITHUB_SAVE_INFO';
66
export const ADD_PERSISTENCE_FILE = 'ADD_PERSISTENCE_FILE';
77
export const DELETE_GITHUB_SAVE_INFO = 'DELETE_GITHUB_SAVE_INFO';
88
export const DELETE_PERSISTENCE_FILE = 'DELETE_PERSISTENCE_FILE';
9+
export const DELETE_PERSISTENCE_FOLDER_AND_CHILDREN = 'DELETE_PERSISTENCE_FOLDER_AND_CHILDREN';
910
export const DELETE_ALL_GITHUB_SAVE_INFO = 'DELETE_ALL_GITHUB_SAVE_INFO';
1011
export const DELETE_ALL_PERSISTENCE_FILES = 'DELETE_ALL_PERSISTENCE_FILES';
1112
export const UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH =

src/commons/fileSystem/FileSystemUtils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ export const rmdirRecursively = (fileSystem: FSModule, directoryPath: string): P
225225
export const writeFileRecursively = (
226226
fileSystem: FSModule,
227227
filePath: string,
228-
fileContents: string
228+
fileContents: string,
229+
onlyCreateFolder?: boolean
229230
): Promise<void> => {
230231
return new Promise((resolve, reject) => {
231232
// Create directories along the path if they do not exist.
@@ -255,15 +256,16 @@ export const writeFileRecursively = (
255256
promise.catch(err => reject(err));
256257
}
257258

258-
// Write to the file.
259-
fileSystem.writeFile(filePath, fileContents, err => {
260-
if (err) {
261-
reject(err);
262-
return;
263-
}
264-
265-
resolve();
266-
});
259+
if (!onlyCreateFolder) {
260+
// Write to the file.
261+
fileSystem.writeFile(filePath, fileContents, err => {
262+
if (err) {
263+
reject(err);
264+
return;
265+
}
266+
});
267+
}
268+
resolve();
267269
});
268270
};
269271

0 commit comments

Comments
 (0)