Skip to content

Commit f0fc2cc

Browse files
committed
Prototype for Google Drive instant sync - create file
1 parent 7c2559a commit f0fc2cc

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

src/commons/fileSystemView/FileSystemViewFileNode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const FileSystemViewFileNode: React.FC<Props> = ({
5050
? Colors.ORANGE4
5151
: Colors.BLUE4
5252
: Colors.BLUE4
53-
: Colors.BLUE4
53+
: Colors.ORANGE4
5454
: undefined;
5555
setCurrColor(checkColor(myFileMetadata));
5656
}, [lastEditedFilePath]);

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ export function* persistenceSaga(): SagaIterator {
126126
return;
127127
}
128128

129-
// Close folder mode TODO disable the button
130-
//yield call(store.dispatch, actions.setFolderMode("playground", false));
131129
yield call(store.dispatch, actions.disableFileSystemContextMenus());
132130

133131
// Note: for mimeType, text/plain -> file, application/vnd.google-apps.folder -> folder
@@ -153,7 +151,6 @@ export function* persistenceSaga(): SagaIterator {
153151
yield call(console.log, "no filesystem!");
154152
return;
155153
}
156-
yield call(console.log, "there is a filesystem");
157154

158155
// Begin
159156

@@ -163,7 +160,15 @@ export function* persistenceSaga(): SagaIterator {
163160
// clear all persistence files
164161
yield call(store.dispatch, actions.deleteAllPersistenceFiles());
165162

163+
// add tlrf
164+
yield put(actions.addPersistenceFile({ id, parentId, name, path: "/playground/" + name, isFolder: true }));
165+
166166
for (const currFile of fileList) {
167+
if (currFile.isFolder == true) {
168+
yield call(console.log, "not file ", currFile);
169+
yield put(actions.addPersistenceFile({ id: currFile.id, parentId: currFile.parentId, name: currFile.name, path: "/playground" + currFile.path, isFolder: true }));
170+
continue;
171+
}
167172
yield put(actions.addPersistenceFile({ id: currFile.id, parentId: currFile.parentId, name: currFile.name, path: "/playground" + currFile.path, lastSaved: new Date() }));
168173
const contents = yield call([gapi.client.drive.files, 'get'], { fileId: currFile.id, alt: 'media' });
169174
yield call(writeFileRecursively, fileSystem, "/playground" + currFile.path, contents.body);
@@ -427,7 +432,7 @@ export function* persistenceSaga(): SagaIterator {
427432
external
428433
};
429434

430-
// check if top level folder has been renamed
435+
// check if top level folder has been renamed TODO remove once instant sync done
431436
// assuming only 1 top level folder exists, so get 1 file
432437
const testPath = Object.keys(currFiles)[0];
433438
const regexResult = /^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$/.exec(testPath);
@@ -444,8 +449,6 @@ export function* persistenceSaga(): SagaIterator {
444449
}
445450

446451
// Start actually saving
447-
// Turn off folder mode TODO disable folder mode
448-
//yield call (store.dispatch, actions.setFolderMode("playground", false));
449452
yield call(store.dispatch, actions.disableFileSystemContextMenus());
450453

451454
if (topLevelFolderName !== currFolderObject.name) {
@@ -477,9 +480,16 @@ export function* persistenceSaga(): SagaIterator {
477480

478481
const currPersistenceFile = persistenceFileArray.find(e => e.path === currFullFilePath);
479482
if (currPersistenceFile === undefined) {
480-
yield call(console.log, "error");
481-
return;
483+
yield call(console.log, "this file is not in persistenceFileArray: ", currFullFilePath);
484+
continue;
485+
}
486+
487+
if (!currPersistenceFile.id || !currPersistenceFile.parentId) {
488+
// get folder
489+
yield call(console.log, "this file does not have id/parentId: ", currFullFilePath);
490+
continue;
482491
}
492+
483493
const currFileId = currPersistenceFile.id!;
484494
const currFileParentFolderId = currPersistenceFile.parentId!;
485495

@@ -589,6 +599,47 @@ export function* persistenceSaga(): SagaIterator {
589599
function* ({ payload }: ReturnType<typeof actions.persistenceCreateFile>) {
590600
const newFilePath = payload;
591601
yield call(console.log, "create file ", newFilePath);
602+
603+
// const persistenceFileArray: PersistenceFile[] = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
604+
605+
// look for parent folder persistenceFile
606+
const regexResult = /^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$/.exec(newFilePath);
607+
const parentFolderPath = regexResult ? regexResult[1].slice(0, -1) : undefined;
608+
if (!parentFolderPath) {
609+
yield call(console.log, "parent not found ", newFilePath);
610+
return;
611+
}
612+
const newFileName = regexResult![2];
613+
const persistenceFileArray: PersistenceFile[] = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
614+
const parentFolderPersistenceFile = persistenceFileArray.find(e => e.path === parentFolderPath);
615+
if (!parentFolderPersistenceFile) {
616+
yield call(console.log, "parent pers file not found ", newFilePath, " parent path ", parentFolderPath, " persArr ", persistenceFileArray, " reg res ", regexResult);
617+
return;
618+
}
619+
620+
yield call(console.log, "parent found ", parentFolderPersistenceFile, " for file ", newFilePath);
621+
622+
// create file
623+
const parentFolderId = parentFolderPersistenceFile.id;
624+
const [chapter, variant, external] = yield select(
625+
(state: OverallState) => [
626+
state.workspaces.playground.context.chapter,
627+
state.workspaces.playground.context.variant,
628+
state.workspaces.playground.externalLibrary
629+
]
630+
);
631+
const config: IPlaygroundConfig = {
632+
chapter,
633+
variant,
634+
external
635+
};
636+
const newFilePersistenceFile: PersistenceFile = yield call(createFile, newFileName, parentFolderId, MIME_SOURCE, '', config);
637+
yield put(actions.addPersistenceFile({ ...newFilePersistenceFile, lastSaved: new Date(), path: newFilePath }));
638+
yield call(
639+
showSuccessMessage,
640+
`${newFileName} successfully saved to Google Drive.`,
641+
1000
642+
);
592643
}
593644
);
594645

@@ -798,7 +849,7 @@ async function getFilesOfFolder( // recursively get files
798849
name: currFolderName,
799850
id: folderId,
800851
path: currPath + '/' + currFolderName,
801-
isFile: false
852+
isFolder: true
802853
}];
803854
}
804855

@@ -809,15 +860,20 @@ async function getFilesOfFolder( // recursively get files
809860
ans = ans.concat(await
810861
getFilesOfFolder(currFile.id!, currFile.name!, currPath + '/' + currFolderName)
811862
);
863+
ans.push({
864+
name: currFile.name,
865+
id: currFile.id,
866+
parentId: folderId,
867+
path: currPath + '/' + currFolderName + '/' + currFile.name,
868+
isFolder: true
869+
});
812870
}
813871
else { // file
814-
console.log("found file " + currFile.name);
815872
ans.push({
816873
name: currFile.name,
817874
id: currFile.id,
818875
parentId: folderId,
819-
path: currPath + '/' + currFolderName + '/' + currFile.name,
820-
isFile: true
876+
path: currPath + '/' + currFolderName + '/' + currFile.name
821877
});
822878
}
823879
}
@@ -934,7 +990,7 @@ function createFile(
934990
headers,
935991
body
936992
})
937-
.then(({ result }) => ({ id: result.id, name: result.name, isFile: true }));
993+
.then(({ result }) => ({ id: result.id, parentId: parent, name: result.name }));
938994
}
939995

940996
function updateFile(

0 commit comments

Comments
 (0)