Skip to content

Commit 7c2559a

Browse files
refactored githubsaveinfoarray into persistencefile and persistencefilearray
1 parent 3ff0503 commit 7c2559a

File tree

9 files changed

+53
-60
lines changed

9 files changed

+53
-60
lines changed

src/commons/application/ApplicationTypes.ts

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

549549
export const defaultFileSystem: FileSystemState = {
550550
inBrowserFileSystem: null,
551-
githubSaveInfoArray: [],
552551
persistenceFileArray: []
553552
};
554553

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
deleteAllPersistenceFiles, deleteGithubSaveInfo,
1111
deletePersistenceFile,
1212
setInBrowserFileSystem,
13-
setPersistenceFileLastEditByPath} from './FileSystemActions';
13+
setPersistenceFileLastEditByPath,
14+
} from './FileSystemActions';
1415
import { FileSystemState } from './FileSystemTypes';
1516

1617
export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = createReducer(
@@ -22,23 +23,42 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
2223
})
2324
.addCase(addGithubSaveInfo, (state, action) => {
2425
const githubSaveInfoPayload = action.payload.githubSaveInfo;
25-
const githubSaveInfoArray = state['githubSaveInfoArray']
26+
const persistenceFileArray = state['persistenceFileArray'];
2627

27-
const saveInfoIndex = githubSaveInfoArray.findIndex(e => e === githubSaveInfoPayload);
28+
const saveInfoIndex = persistenceFileArray.findIndex(e => {
29+
return e.path === githubSaveInfoPayload.filePath &&
30+
e.repoName === githubSaveInfoPayload.repoName;
31+
});
2832
if (saveInfoIndex === -1) {
29-
githubSaveInfoArray[githubSaveInfoArray.length] = githubSaveInfoPayload;
33+
persistenceFileArray[persistenceFileArray.length] = {
34+
id: '',
35+
name: '',
36+
path: githubSaveInfoPayload.filePath,
37+
lastSaved: githubSaveInfoPayload.lastSaved,
38+
repoName: githubSaveInfoPayload.repoName
39+
};
3040
} else {
3141
// file already exists, to replace file
32-
githubSaveInfoArray[saveInfoIndex] = githubSaveInfoPayload;
42+
persistenceFileArray[saveInfoIndex] = {
43+
id: '',
44+
name: '',
45+
path: githubSaveInfoPayload.filePath,
46+
lastSaved: githubSaveInfoPayload.lastSaved,
47+
repoName: githubSaveInfoPayload.repoName
48+
};
3349
}
34-
state.githubSaveInfoArray = githubSaveInfoArray;
50+
state.persistenceFileArray = persistenceFileArray;
3551
})
3652
.addCase(deleteGithubSaveInfo, (state, action) => {
37-
const newGithubSaveInfoArray = state['githubSaveInfoArray'].filter(e => e !== action.payload.githubSaveInfo);
38-
state.githubSaveInfoArray = newGithubSaveInfoArray;
53+
const newPersistenceFileArray = state['persistenceFileArray'].filter(e => {
54+
return e.path != action.payload.githubSaveInfo.filePath &&
55+
e.lastSaved != action.payload.githubSaveInfo.lastSaved &&
56+
e.repoName != action.payload.githubSaveInfo.repoName
57+
});
58+
state.persistenceFileArray = newPersistenceFileArray;
3959
})
4060
.addCase(deleteAllGithubSaveInfo, (state, action) => {
41-
state.githubSaveInfoArray = [];
61+
state.persistenceFileArray = [];
4262
})
4363
.addCase(addPersistenceFile, (state, action) => {
4464
const persistenceFilePayload = action.payload;

src/commons/fileSystem/FileSystemTypes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FSModule } from 'browserfs/dist/node/core/FS';
2-
import { GitHubSaveInfo } from 'src/features/github/GitHubTypes';
32
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
43

54
export const SET_IN_BROWSER_FILE_SYSTEM = 'SET_IN_BROWSER_FILE_SYSTEM';
@@ -14,6 +13,5 @@ export const SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH = 'SET_PERSISTENCE_FILE_LAST
1413

1514
export type FileSystemState = {
1615
inBrowserFileSystem: FSModule | null;
17-
githubSaveInfoArray: GitHubSaveInfo[];
1816
persistenceFileArray: PersistenceFile[];
1917
};

src/commons/fileSystem/FileSystemUtils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { store } from 'src/pages/createStore';
66

77
import { WORKSPACE_BASE_PATHS } from '../../pages/fileSystem/createInBrowserFileSystem';
88
import { WorkspaceLocation } from '../workspace/WorkspaceTypes';
9+
import { PersistenceFile } from 'src/features/persistence/PersistenceTypes';
910

1011
type File = {
1112
path: string;
@@ -267,17 +268,16 @@ export const writeFileRecursively = (
267268
};
268269

269270
export const getGithubSaveInfo = () => {
270-
const githubSaveInfoArray = store.getState().fileSystem.githubSaveInfoArray;
271+
const persistenceFileArray = store.getState().fileSystem.persistenceFileArray;
271272
const {
272273
editorTabs,
273274
activeEditorTabIndex
274275
} = store.getState().workspaces['playground'];
275276
let currentFilePath = '';
276277
if (activeEditorTabIndex !== null) {
277-
currentFilePath = editorTabs[activeEditorTabIndex].filePath?.slice(12) || '';
278+
currentFilePath = editorTabs[activeEditorTabIndex].filePath || '';
278279
}
279-
const nullGithubSaveInfo: GitHubSaveInfo = { repoName: 'test', filePath: '', lastSaved: new Date() };
280-
const githubSaveInfo = githubSaveInfoArray.find(githubSaveInfo => githubSaveInfo.filePath === currentFilePath) || nullGithubSaveInfo;
281-
280+
const PersistenceFile: PersistenceFile = persistenceFileArray.find(e => e.path === currentFilePath) || {name: '', id: ''};
281+
const githubSaveInfo: GitHubSaveInfo = { filePath: PersistenceFile.path, lastSaved: PersistenceFile.lastSaved, repoName: PersistenceFile.repoName};
282282
return githubSaveInfo;
283283
}

src/commons/sagas/GitHubPersistenceSaga.ts

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ function* githubSaveFile(): any {
137137
GitHubUtils.performOverwritingSave(
138138
octokit,
139139
githubLoginId,
140-
repoName,
141-
filePath,
140+
repoName || '',
141+
filePath || '',
142142
githubEmail,
143143
githubName,
144144
commitMessage,
@@ -208,6 +208,7 @@ function* githubSaveAll(): any {
208208

209209
const githubLoginId = authUser.data.login;
210210
const githubSaveInfo = getGithubSaveInfo();
211+
// console.log(githubSaveInfo);
211212
const repoName = githubSaveInfo.repoName;
212213
const githubEmail = authUser.data.email;
213214
const githubName = authUser.data.name;
@@ -231,37 +232,10 @@ function* githubSaveAll(): any {
231232
yield call(GitHubUtils.performMultipleOverwritingSave,
232233
octokit,
233234
githubLoginId,
234-
repoName,
235+
repoName || '',
235236
githubEmail,
236237
githubName,
237238
{ commitMessage: commitMessage, files: modifiedcurrFiles});
238-
239-
// for (const filePath of Object.keys(currFiles)) {
240-
// const content = currFiles[filePath];
241-
// yield call(GitHubUtils.performOverwritingSave,
242-
// octokit,
243-
// githubLoginId,
244-
// repoName,
245-
// filePath.slice(12),
246-
// githubEmail,
247-
// githubName,
248-
// commitMessage,
249-
// content);
250-
// }
251-
252-
// const activeEditorTabIndex: number | null = yield select(
253-
// (state: OverallState) => state.workspaces.playground.activeEditorTabIndex
254-
// );
255-
// if (activeEditorTabIndex === null) {
256-
// throw new Error('No active editor tab found.');
257-
// }
258-
// const editorTabs: EditorTabState[] = yield select(
259-
// (state: OverallState) => state.workspaces.playground.editorTabs
260-
// );
261-
// const content = editorTabs[activeEditorTabIndex].value;
262-
263-
264-
265239

266240
}
267241

src/features/github/GitHubTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const GITHUB_SAVE_FILE_AS = 'GITHUB_SAVE_FILE_AS';
44
export const GITHUB_SAVE_ALL = 'GITHUB_SAVE_ALL';
55

66
export type GitHubSaveInfo = {
7-
repoName: string;
8-
filePath: string;
7+
repoName?: string;
8+
filePath?: string;
99
lastSaved?: Date;
1010
};

src/features/github/GitHubUtils.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ export async function openFolderInFolderMode(
315315
store.dispatch(actions.addGithubSaveInfo(
316316
{
317317
repoName: repoName,
318-
filePath: file,
318+
filePath: "/playground/" + file,
319319
lastSaved: new Date()
320320
}
321321
))
322-
console.log(store.getState().fileSystem.githubSaveInfoArray);
322+
console.log(store.getState().fileSystem.persistenceFileArray);
323323
console.log("wrote one file");
324324
}
325325
}
@@ -363,7 +363,7 @@ export async function performOverwritingSave(
363363
const results: GetContentResponse = await octokit.repos.getContent({
364364
owner: repoOwner,
365365
repo: repoName,
366-
path: filePath
366+
path: filePath.slice(12)
367367
});
368368

369369
type GetContentData = GetResponseDataTypeFromEndpointMethod<typeof octokit.repos.getContent>;
@@ -379,15 +379,15 @@ export async function performOverwritingSave(
379379
await octokit.repos.createOrUpdateFileContents({
380380
owner: repoOwner,
381381
repo: repoName,
382-
path: filePath,
382+
path: filePath.slice(12),
383383
message: commitMessage,
384384
content: contentEncoded,
385385
sha: sha,
386386
committer: { name: githubName, email: githubEmail },
387387
author: { name: githubName, email: githubEmail }
388388
});
389389

390-
store.dispatch(actions.updateGithubSaveInfo(repoName, filePath, new Date()));
390+
store.dispatch(actions.addGithubSaveInfo( { repoName: repoName, filePath: filePath, lastSaved: new Date()} ));
391391

392392
//this is just so that playground is forcefully updated
393393
store.dispatch(actions.playgroundUpdateRepoName(repoName));
@@ -434,7 +434,7 @@ export async function performMultipleOverwritingSave(
434434
return;
435435
}
436436

437-
store.dispatch(actions.updateGithubSaveInfo(repoName, filePath.slice(12), new Date()));
437+
store.dispatch(actions.addGithubSaveInfo( { repoName: repoName, filePath: "/playground/" + filePath, lastSaved: new Date() } ));
438438
} catch (err) {
439439
console.error(err);
440440
showWarningMessage('Something went wrong when trying to save the file.', 1000);
@@ -486,13 +486,13 @@ export async function performCreatingSave(
486486
await octokit.repos.createOrUpdateFileContents({
487487
owner: repoOwner,
488488
repo: repoName,
489-
path: filePath,
489+
path: filePath.slice(12),
490490
message: commitMessage,
491491
content: contentEncoded,
492492
committer: { name: githubName, email: githubEmail },
493493
author: { name: githubName, email: githubEmail }
494494
});
495-
store.dispatch(actions.playgroundUpdateGitHubSaveInfo(repoName, filePath, new Date()));
495+
store.dispatch(actions.addGithubSaveInfo( { repoName: repoName, filePath: filePath, lastSaved: new Date() } ));
496496
showSuccessMessage('Successfully created file!', 1000);
497497
} catch (err) {
498498
console.error(err);
@@ -519,7 +519,7 @@ export async function performFolderDeletion(
519519
const results = await octokit.repos.getContent({
520520
owner: repoOwner,
521521
repo: repoName,
522-
path: filePath
522+
path: filePath.slice(12)
523523
});
524524

525525
const files = results.data;
@@ -536,7 +536,7 @@ export async function performFolderDeletion(
536536
await octokit.repos.deleteFile({
537537
owner: repoOwner,
538538
repo: repoName,
539-
path: file.path,
539+
path: file.path.slice(12),
540540
message: commitMessage,
541541
sha: file.sha
542542
});

src/features/persistence/PersistenceTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export type PersistenceFile = {
2020
lastSaved?: Date;
2121
lastEdit?: Date;
2222
isFolder?: boolean;
23+
repoName?: string; // only when synced to github
2324
};

src/pages/playground/Playground.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ const Playground: React.FC<PlaygroundProps> = props => {
629629

630630
const githubPersistenceIsDirty =
631631
githubSaveInfo && (!githubSaveInfo.lastSaved || githubSaveInfo.lastSaved < lastEdit);
632-
//console.log(githubSaveInfo);
632+
console.log(githubSaveInfo.lastSaved);
633+
console.log(lastEdit);
633634
const githubButtons = useMemo(() => {
634635
return (
635636
<ControlBarGitHubButtons

0 commit comments

Comments
 (0)