Skip to content

Commit 3e377ba

Browse files
authored
Add file system directory for SICP workspace (#2408)
* Make Playground component workspace-agnostic Why does the SICP workspace make use of the playground? Who thought this was a great idea??? * Add BrowserFS directory for SICP workspace * Set default file path when resetting SICP workspace * Update tests
1 parent d654251 commit 3e377ba

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ export const createDefaultWorkspace = (workspaceLocation: WorkspaceLocation): Wo
253253
activeEditorTabIndex: 0,
254254
editorTabs: [
255255
{
256+
filePath: ['playground', 'sicp'].includes(workspaceLocation)
257+
? getDefaultFilePath(workspaceLocation)
258+
: undefined,
256259
value: ['playground', 'sourcecast', 'githubAssessments'].includes(workspaceLocation)
257260
? defaultEditorValue
258261
: '',
@@ -284,7 +287,9 @@ export const createDefaultWorkspace = (workspaceLocation: WorkspaceLocation): Wo
284287
debuggerContext: {} as DebuggerContext
285288
});
286289

287-
export const defaultPlaygroundFilePath = `${WORKSPACE_BASE_PATHS.playground}/program.js`;
290+
const defaultFileName = 'program.js';
291+
export const getDefaultFilePath = (workspaceLocation: WorkspaceLocation) =>
292+
`${WORKSPACE_BASE_PATHS[workspaceLocation]}/${defaultFileName}`;
288293

289294
export const defaultWorkspaceManager: WorkspaceManagerState = {
290295
assessment: {
@@ -302,9 +307,10 @@ export const defaultWorkspaceManager: WorkspaceManagerState = {
302307
playground: {
303308
...createDefaultWorkspace('playground'),
304309
usingSubst: false,
310+
activeEditorTabIndex: 0,
305311
editorTabs: [
306312
{
307-
filePath: defaultPlaygroundFilePath,
313+
filePath: getDefaultFilePath('playground'),
308314
value: defaultEditorValue,
309315
highlightedLines: [],
310316
breakpoints: []
@@ -348,7 +354,16 @@ export const defaultWorkspaceManager: WorkspaceManagerState = {
348354
},
349355
sicp: {
350356
...createDefaultWorkspace('sicp'),
351-
usingSubst: false
357+
usingSubst: false,
358+
activeEditorTabIndex: 0,
359+
editorTabs: [
360+
{
361+
filePath: getDefaultFilePath('sicp'),
362+
value: defaultEditorValue,
363+
highlightedLines: [],
364+
breakpoints: []
365+
}
366+
]
352367
},
353368
githubAssessment: {
354369
...createDefaultWorkspace('githubAssessment'),

src/commons/assessmentWorkspace/__tests__/__snapshots__/AssessmentWorkspace.tsx.snap

Lines changed: 8 additions & 8 deletions
Large diffs are not rendered by default.

src/commons/sagas/__tests__/PlaygroundSaga.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { updateShortURL } from '../../../features/playground/PlaygroundActions';
88
import { SHORTEN_URL } from '../../../features/playground/PlaygroundTypes';
99
import {
1010
createDefaultWorkspace,
11-
defaultPlaygroundFilePath,
1211
defaultState,
1312
defaultWorkspaceManager,
13+
getDefaultFilePath,
1414
OverallState
1515
} from '../../application/ApplicationTypes';
1616
import { ExternalLibraryName } from '../../application/types/ExternalTypes';
@@ -21,6 +21,7 @@ import PlaygroundSaga, { shortenURLRequest } from '../PlaygroundSaga';
2121
describe('Playground saga tests', () => {
2222
Constants.urlShortenerBase = 'http://url-shortener.com/';
2323
const errMsg = 'Something went wrong trying to create the link.';
24+
const defaultPlaygroundFilePath = getDefaultFilePath('playground');
2425

2526
// This test relies on BrowserFS which works in browser environments and not Node.js.
2627
// FIXME: Uncomment this test if BrowserFS adds support for running in Node.js.

src/pages/fileSystem/createInBrowserFileSystem.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const WORKSPACE_BASE_PATHS: Record<keyof WorkspaceManagerState, string> =
1818
githubAssessment: '',
1919
grading: '',
2020
playground: '/playground',
21-
sicp: '',
21+
sicp: '/sicp',
2222
sourcecast: '',
2323
sourcereel: ''
2424
};
@@ -34,6 +34,12 @@ export const createInBrowserFileSystem = (store: Store<OverallState>): Promise<v
3434
options: {
3535
storeName: 'playground'
3636
}
37+
},
38+
[WORKSPACE_BASE_PATHS.sicp]: {
39+
fs: 'IndexedDB',
40+
options: {
41+
storeName: 'sicp'
42+
}
3743
}
3844
}
3945
},

src/pages/playground/Playground.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import {
7373
} from 'src/features/playground/PlaygroundActions';
7474

7575
import {
76-
defaultPlaygroundFilePath,
76+
getDefaultFilePath,
7777
InterpreterOutput,
7878
isSourceLanguage,
7979
OverallState,
@@ -181,6 +181,7 @@ const keyMap = { goGreen: 'h u l k' };
181181
export async function handleHash(
182182
hash: string,
183183
props: PlaygroundProps,
184+
workspaceLocation: WorkspaceLocation,
184185
dispatch: Dispatch<AnyAction>,
185186
fileSystem: FSModule
186187
) {
@@ -203,40 +204,43 @@ export async function handleHash(
203204
// For backward compatibility with old share links - 'prgrm' is no longer used.
204205
const program = qs.prgrm === undefined ? '' : decompressFromEncodedURIComponent(qs.prgrm);
205206

206-
// By default, create just the default playground file.
207+
// By default, create just the default file.
208+
const defaultFilePath = getDefaultFilePath(workspaceLocation);
207209
const files: Record<string, string> =
208210
qs.files === undefined
209211
? {
210-
[defaultPlaygroundFilePath]: program
212+
[defaultFilePath]: program
211213
}
212214
: parseQuery(decompressFromEncodedURIComponent(qs.files));
213-
await overwriteFilesInWorkspace('playground', fileSystem, files);
215+
await overwriteFilesInWorkspace(workspaceLocation, fileSystem, files);
214216

215217
// BrowserFS does not provide a way of listening to changes in the file system, which makes
216218
// updating the file system view troublesome. To force the file system view to re-render
217219
// (and thus display the updated file system), we first disable Folder mode.
218-
dispatch(setFolderMode('playground', false));
220+
dispatch(setFolderMode(workspaceLocation, false));
219221
const isFolderModeEnabled = convertParamToBoolean(qs.isFolder) ?? false;
220222
// If Folder mode should be enabled, enabling it after disabling it earlier will cause the
221223
// newly-added files to be shown. Note that this has to take place after the files are
222224
// already added to the file system.
223-
dispatch(setFolderMode('playground', isFolderModeEnabled));
225+
dispatch(setFolderMode(workspaceLocation, isFolderModeEnabled));
224226

225227
// By default, open a single editor tab containing the default playground file.
226228
const editorTabFilePaths = qs.tabs?.split(',').map(decompressFromEncodedURIComponent) ?? [
227-
defaultPlaygroundFilePath
229+
defaultFilePath
228230
];
229231
// Remove all editor tabs before populating with the ones from the query string.
230-
dispatch(removeEditorTabsForDirectory('playground', WORKSPACE_BASE_PATHS.playground));
232+
dispatch(
233+
removeEditorTabsForDirectory(workspaceLocation, WORKSPACE_BASE_PATHS[workspaceLocation])
234+
);
231235
// Add editor tabs from the query string.
232236
editorTabFilePaths.forEach(filePath =>
233237
// Fall back on the empty string if the file contents do not exist.
234-
dispatch(addEditorTab('playground', filePath, files[filePath] ?? ''))
238+
dispatch(addEditorTab(workspaceLocation, filePath, files[filePath] ?? ''))
235239
);
236240

237241
// By default, use the first editor tab.
238242
const activeEditorTabIndex = convertParamToInt(qs.tabIdx) ?? 0;
239-
dispatch(updateActiveEditorTabIndex('playground', activeEditorTabIndex));
243+
dispatch(updateActiveEditorTabIndex(workspaceLocation, activeEditorTabIndex));
240244

241245
const variant: Variant =
242246
sourceLanguages.find(
@@ -344,7 +348,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
344348
return;
345349
}
346350
if (fileSystem !== null) {
347-
handleHash(hash, propsRef.current, dispatch, fileSystem);
351+
handleHash(hash, propsRef.current, workspaceLocation, dispatch, fileSystem);
348352
}
349353
}, [
350354
dispatch,
@@ -902,7 +906,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
902906
const editorContainerProps: NormalEditorContainerProps = {
903907
..._.pick(props, 'editorSessionId', 'isEditorAutorun'),
904908
editorVariant: 'normal',
905-
baseFilePath: WORKSPACE_BASE_PATHS.playground,
909+
baseFilePath: WORKSPACE_BASE_PATHS[workspaceLocation],
906910
isFolderModeEnabled,
907911
activeEditorTabIndex,
908912
setActiveEditorTabIndex,
@@ -977,7 +981,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
977981
body: (
978982
<FileSystemView
979983
workspaceLocation="playground"
980-
basePath={WORKSPACE_BASE_PATHS.playground}
984+
basePath={WORKSPACE_BASE_PATHS[workspaceLocation]}
981985
/>
982986
),
983987
iconName: IconNames.FOLDER_CLOSE,
@@ -987,7 +991,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
987991
: [])
988992
]
989993
};
990-
}, [isFolderModeEnabled]);
994+
}, [isFolderModeEnabled, workspaceLocation]);
991995

992996
const workspaceProps: WorkspaceProps = {
993997
controlBarProps: {

src/pages/playground/__tests__/Playground.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe('handleHash', () => {
9090
handleChapterSelect: mockHandleChapterSelect,
9191
handleChangeExecTime: mockHandleChangeExecTime
9292
},
93+
'playground',
9394
// We cannot make use of 'dispatch' & BrowserFS in test cases. However, the
9495
// behaviour being tested here does not actually invoke either of these. As
9596
// a workaround, we pass in 'undefined' instead & cast to the expected types.

0 commit comments

Comments
 (0)