Skip to content

Commit 8bf268a

Browse files
added githubsaveinfo to filesystemtype
1 parent d6c1ce7 commit 8bf268a

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ export const createDefaultStoriesEnv = (
549549
});
550550

551551
export const defaultFileSystem: FileSystemState = {
552-
inBrowserFileSystem: null
552+
inBrowserFileSystem: null,
553+
githubSaveInfoArray: []
553554
};
554555

555556
export const defaultSideContent: SideContentState = {
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
import { createAction } from '@reduxjs/toolkit';
22
import { FSModule } from 'browserfs/dist/node/core/FS';
33

4-
import { SET_IN_BROWSER_FILE_SYSTEM } from './FileSystemTypes';
4+
import {
5+
SET_IN_BROWSER_FILE_SYSTEM,
6+
ADD_GITHUB_SAVE_INFO,
7+
DELETE_GITHUB_SAVE_INFO,
8+
DELETE_ALL_GITHUB_SAVE_INFO
9+
} from './FileSystemTypes';
10+
import { GitHubSaveInfo } from 'src/features/github/GitHubTypes';
511

612
export const setInBrowserFileSystem = createAction(
713
SET_IN_BROWSER_FILE_SYSTEM,
814
(inBrowserFileSystem: FSModule) => ({ payload: { inBrowserFileSystem } })
915
);
16+
17+
export const addGithubSaveInfo = createAction(
18+
ADD_GITHUB_SAVE_INFO,
19+
(githubSaveInfo: GitHubSaveInfo) => ({ payload: { githubSaveInfo }})
20+
);
21+
22+
export const deleteGithubSaveInfo = createAction(
23+
DELETE_GITHUB_SAVE_INFO,
24+
(githubSaveInfo: GitHubSaveInfo) => ({ payload: { githubSaveInfo }})
25+
);
26+
27+
export const deleteAllGithubSaveInfo = createAction(
28+
DELETE_ALL_GITHUB_SAVE_INFO,
29+
() => ({ payload: {} })
30+
);

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@ import { Reducer } from 'redux';
33

44
import { defaultFileSystem } from '../application/ApplicationTypes';
55
import { SourceActionType } from '../utils/ActionsHelper';
6-
import { setInBrowserFileSystem } from './FileSystemActions';
6+
import {
7+
setInBrowserFileSystem,
8+
addGithubSaveInfo,
9+
deleteAllGithubSaveInfo,
10+
deleteGithubSaveInfo } from './FileSystemActions';
711
import { FileSystemState } from './FileSystemTypes';
812

913
export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = createReducer(
1014
defaultFileSystem,
1115
builder => {
12-
builder.addCase(setInBrowserFileSystem, (state, action) => {
13-
state.inBrowserFileSystem = action.payload.inBrowserFileSystem;
16+
builder
17+
.addCase(setInBrowserFileSystem, (state, action) => {
18+
state.inBrowserFileSystem = action.payload.inBrowserFileSystem;
19+
})
20+
.addCase(addGithubSaveInfo, (state, action) => {
21+
const githubSaveInfoPayload = action.payload.githubSaveInfo;
22+
const githubSaveInfoArray = state['githubSaveInfoArray']
23+
24+
const saveInfoIndex = githubSaveInfoArray.findIndex(e => e === githubSaveInfoPayload);
25+
if (saveInfoIndex == -1) {
26+
githubSaveInfoArray[githubSaveInfoArray.length] = githubSaveInfoPayload;
27+
} else {
28+
// file already exists, to replace file
29+
githubSaveInfoArray[saveInfoIndex] = githubSaveInfoPayload;
30+
}
31+
state.githubSaveInfoArray = githubSaveInfoArray;
32+
})
33+
.addCase(deleteGithubSaveInfo, (state, action) => {
34+
const newGithubSaveInfoArray = state['githubSaveInfoArray'].filter(e => e !== action.payload.githubSaveInfo);
35+
state.githubSaveInfoArray = newGithubSaveInfoArray;
36+
})
37+
.addCase(deleteAllGithubSaveInfo, (state, action) => {
38+
state.githubSaveInfoArray = [];
1439
});
1540
}
1641
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { FSModule } from 'browserfs/dist/node/core/FS';
2+
import { GitHubSaveInfo } from 'src/features/github/GitHubTypes';
23

34
export const SET_IN_BROWSER_FILE_SYSTEM = 'SET_IN_BROWSER_FILE_SYSTEM';
5+
export const ADD_GITHUB_SAVE_INFO = 'ADD_GITHUB_SAVE_INFO';
6+
export const DELETE_GITHUB_SAVE_INFO = 'DELETE_GITHUB_SAVE_INFO';
7+
export const DELETE_ALL_GITHUB_SAVE_INFO = 'DELETE_ALL_GITHUB_SAVE_INFO';
48

59
export type FileSystemState = {
610
inBrowserFileSystem: FSModule | null;
11+
githubSaveInfoArray: GitHubSaveInfo[];
712
};

src/features/github/GitHubUtils.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export async function openFolderInFolderMode(
238238
) {
239239
if (octokit === undefined) return;
240240

241+
store.dispatch(actions.deleteAllGithubSaveInfo());
242+
241243
//In order to get the file paths recursively, we require the tree_sha,
242244
// which is obtained from the most recent commit(any commit works but the most recent)
243245
// is the easiest
@@ -301,7 +303,15 @@ export async function openFolderInFolderMode(
301303
if (content) {
302304
const fileContent = Buffer.from(content, 'base64').toString();
303305
console.log(file);
304-
writeFileRecursively(fileSystem, "/playground/" + file, fileContent)
306+
writeFileRecursively(fileSystem, "/playground/" + file, fileContent);
307+
store.dispatch(actions.addGithubSaveInfo(
308+
{
309+
repoName: repoName,
310+
filePath: file,
311+
lastSaved: new Date()
312+
}
313+
))
314+
console.log(store.getState().fileSystem.githubSaveInfoArray);
305315
console.log("wrote one file");
306316
}
307317
}
@@ -365,6 +375,7 @@ export async function performOverwritingSave(
365375
committer: { name: githubName, email: githubEmail },
366376
author: { name: githubName, email: githubEmail }
367377
});
378+
//
368379
store.dispatch(actions.playgroundUpdateGitHubSaveInfo(repoName, filePath, new Date()));
369380
showSuccessMessage('Successfully saved file!', 1000);
370381
} catch (err) {

0 commit comments

Comments
 (0)