Skip to content

Commit 998f4fe

Browse files
authored
In Storage interface: (#204)
Replaced listModules with listModulePaths. Removed fetchModuleDateModifiedMillis In projects.ts: Replace listProjects with listProjectNames. Added fetchProject. Changed renameProject, copyProject, deleteProject, and downloadProject functions to take projectName string instead of Project. In Module type: Removed dateModifiedMillis, which was never used. Updated react components Menu, ProjectManageModal, and ProjectNameComponent.
1 parent b1b6a53 commit 998f4fe

File tree

8 files changed

+203
-234
lines changed

8 files changed

+203
-234
lines changed

src/reactComponents/Menu.tsx

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ type MenuItem = Required<Antd.MenuProps>['items'][number];
5454

5555
/** Props for the Menu component. */
5656
export interface MenuProps {
57-
setAlertErrorMessage: (message: string) => void;
5857
storage: commonStorage.Storage | null;
58+
setAlertErrorMessage: (message: string) => void;
5959
gotoTab: (tabKey: string) => void;
6060
project: storageProject.Project | null;
61-
openWPIToolboxSettings: () => void;
6261
setProject: (project: storageProject.Project | null) => void;
62+
openWPIToolboxSettings: () => void;
6363
theme: string;
6464
setTheme: (theme: string) => void;
6565
}
6666

6767
/** Default selected menu keys. */
6868
const DEFAULT_SELECTED_KEYS = ['1'];
6969

70-
/** Storage key for the most recent project. */
71-
const MOST_RECENT_PROJECT_KEY = 'mostRecentProject';
70+
/** Storage key for the most recent project name. */
71+
const MOST_RECENT_PROJECT_NAME_KEY = 'mostRecentProject';
7272

7373
/**
7474
* Creates a menu item with the specified properties.
@@ -177,7 +177,7 @@ function getMenuItems(t: (key: string) => string, project: storageProject.Projec
177177
export function Component(props: MenuProps): React.JSX.Element {
178178
const {t, i18n} = I18Next.useTranslation();
179179

180-
const [projects, setProjects] = React.useState<storageProject.Project[]>([]);
180+
const [projectNames, setProjectNames] = React.useState<string[]>([]);
181181
const [menuItems, setMenuItems] = React.useState<MenuItem[]>([]);
182182
const [fileModalOpen, setFileModalOpen] = React.useState<boolean>(false);
183183
const [projectModalOpen, setProjectModalOpen] = React.useState<boolean>(false);
@@ -191,28 +191,28 @@ export function Component(props: MenuProps): React.JSX.Element {
191191
props.setTheme(newTheme);
192192
};
193193

194-
/** Fetches the list of projects from storage. */
195-
const fetchListOfProjects = async (): Promise<storageProject.Project[]> => {
194+
/** Fetches the list of project names from storage. */
195+
const fetchListOfProjectNames = async (): Promise<string[]> => {
196196
return new Promise(async (resolve, reject) => {
197197
if (!props.storage) {
198198
reject(new Error('Storage not available'));
199199
return;
200200
}
201201
try {
202-
const array = await storageProject.listProjects(props.storage);
203-
setProjects(array);
202+
const array = await storageProject.listProjectNames(props.storage);
203+
setProjectNames(array);
204204
resolve(array);
205205
} catch (e) {
206-
console.error('Failed to load the list of projects:', e);
206+
console.error('Failed to load the list of project names:', e);
207207
props.setAlertErrorMessage(t('fail_list_projects'));
208208
reject(new Error(t('fail_list_projects')));
209209
}
210210
});
211211
};
212212

213-
/** Initializes the projects and handles empty project state. */
214-
const initializeProjects = async (): Promise<void> => {
215-
const array = await fetchListOfProjects();
213+
/** Initializes the project names and handles no projects state. */
214+
const initializeProjectNames = async (): Promise<void> => {
215+
const array = await fetchListOfProjectNames();
216216
if (array.length === 0) {
217217
setNoProjects(true);
218218
setProjectModalOpen(true);
@@ -221,30 +221,29 @@ export function Component(props: MenuProps): React.JSX.Element {
221221

222222
/** Fetches and sets the most recent project. */
223223
const fetchMostRecentProject = async (): Promise<void> => {
224-
let found = false;
225-
226224
if (props.storage) {
227-
const mostRecentProject = await props.storage.fetchEntry(
228-
MOST_RECENT_PROJECT_KEY,
225+
const mostRecentProjectName = await props.storage.fetchEntry(
226+
MOST_RECENT_PROJECT_NAME_KEY,
229227
''
230228
);
231-
projects.forEach((project) => {
232-
if (project.projectName === mostRecentProject) {
233-
props.setProject(project);
234-
found = true;
235-
}
236-
});
237-
if (!found && projects.length > 0) {
238-
props.setProject(projects[0]);
229+
let projectNameToFetch: string = '';
230+
if (projectNames.includes(mostRecentProjectName)) {
231+
projectNameToFetch = mostRecentProjectName;
232+
} else if (projectNames.length) {
233+
projectNameToFetch = projectNames[0];
234+
}
235+
if (projectNameToFetch) {
236+
const project = await storageProject.fetchProject(props.storage, projectNameToFetch);
237+
props.setProject(project);
239238
}
240239
}
241240
};
242241

243-
/** Saves the most recent project to storage. */
244-
const setMostRecentProject = async (): Promise<void> => {
242+
/** Saves the most recent project name to storage. */
243+
const setMostRecentProjectName = async (): Promise<void> => {
245244
if (props.storage) {
246245
await props.storage.saveEntry(
247-
MOST_RECENT_PROJECT_KEY,
246+
MOST_RECENT_PROJECT_NAME_KEY,
248247
props.project?.projectName || ''
249248
);
250249
}
@@ -288,11 +287,7 @@ export function Component(props: MenuProps): React.JSX.Element {
288287
} else if (key === 'theme') {
289288
setThemeModalOpen(true);
290289
} else if (key == 'deploy') {
291-
if (props.project && props.storage) {
292-
handleDeploy();
293-
} else {
294-
props.setAlertErrorMessage(t('NO_PROJECT_SELECTED'));
295-
}
290+
handleDeploy();
296291
} else if (key.startsWith('setlang:')) {
297292
const lang = key.split(':')[1];
298293
i18n.changeLanguage(lang);
@@ -305,13 +300,17 @@ export function Component(props: MenuProps): React.JSX.Element {
305300

306301
/** Handles the deploy action to generate and download Python files. */
307302
const handleDeploy = async (): Promise<void> => {
308-
if (!props.project || !props.storage) {
303+
if (!props.project) {
304+
props.setAlertErrorMessage(t('NO_PROJECT_SELECTED'));
305+
return;
306+
}
307+
if (!props.storage) {
309308
return;
310309
}
311310

312311
try {
313312
const blobUrl = await createPythonFiles.producePythonProjectBlob(props.project, props.storage);
314-
313+
315314
// Create a temporary link to download the file
316315
const link = document.createElement('a');
317316
link.href = blobUrl;
@@ -331,12 +330,16 @@ export function Component(props: MenuProps): React.JSX.Element {
331330
// TODO: Add UI for the download action.
332331
/** Handles the download action to generate and download json files. */
333332
const handleDownload = async (): Promise<void> => {
334-
if (!props.project || !props.storage) {
333+
if (!props.project) {
334+
props.setAlertErrorMessage(t('NO_PROJECT_SELECTED'));
335+
return;
336+
}
337+
if (!props.storage) {
335338
return;
336339
}
337340

338341
try {
339-
const blobUrl = await storageProject.downloadProject(props.storage, props.project);
342+
const blobUrl = await storageProject.downloadProject(props.storage, props.project.projectName);
340343
const filename = props.project.projectName + storageNames.UPLOAD_DOWNLOAD_FILE_EXTENSION;
341344

342345
// Create a temporary link to download the file
@@ -383,8 +386,8 @@ export function Component(props: MenuProps): React.JSX.Element {
383386
}
384387
const dataUrl = event.target.result as string;
385388
const existingProjectNames: string[] = [];
386-
projects.forEach(project => {
387-
existingProjectNames.push(project.projectName);
389+
projectNames.forEach(projectName => {
390+
existingProjectNames.push(projectName);
388391
});
389392
const file = options.file as RcFile;
390393
const uploadProjectName = storageProject.makeUploadProjectName(file.name, existingProjectNames);
@@ -414,23 +417,23 @@ export function Component(props: MenuProps): React.JSX.Element {
414417
setProjectModalOpen(false);
415418
};
416419

417-
// Initialize projects when storage is available
420+
// Initialize project names when storage is available
418421
React.useEffect(() => {
419422
if (!props.storage) {
420423
return;
421424
}
422-
initializeProjects();
425+
initializeProjectNames();
423426
}, [props.storage]);
424427

425-
// Fetch most recent project when projects change
428+
// Fetch most recent project when projectNames change
426429
React.useEffect(() => {
427430
fetchMostRecentProject();
428-
}, [projects]);
431+
}, [projectNames]);
429432

430433
// Update menu items and save project when project or language changes
431434
React.useEffect(() => {
432435
if (props.project) {
433-
setMostRecentProject();
436+
setMostRecentProjectName();
434437
setMenuItems(getMenuItems(t, props.project, i18n.language));
435438
setNoProjects(false);
436439
}

0 commit comments

Comments
 (0)