feat: export/import functionality (backup/restore/share with friends) + a new input component (#433)

* feat: export schedule function to be added to handler

* feat: use UserScheduleStore and return json

* feat: download functionality

* feat: oh wow we already have a blob download util that is very very nice

* feat: return empty json if none found

* feat: import function completion

* feat: file uploading done

* feat: new input component-stories made-settings input replaced with component

* feat: attempt 1 to hook settings.tsx to importSchedule

* feat: it works horray aka using right Course constructor it works

* chore: fix jsdoc

* chore: comments and debug style

* docs: extra comment

* feat: name of schedule more user friendly

* feat: reworked how schedule is passed and check for file being schedule

* feat: color is kept on import

* fix: add sendResponse to exportSchedule

---------

Co-authored-by: doprz <52579214+doprz@users.noreply.github.com>
This commit is contained in:
2024-11-21 12:55:48 -06:00
committed by GitHub
parent 8b922082a7
commit 7dbffc6eef
8 changed files with 329 additions and 2 deletions

View File

@@ -4,19 +4,23 @@ import { getUnusedColor } from '@shared/util/colors';
/**
* Adds a course to a user's schedule.
*
* @param scheduleId - The id of the schedule to add the course to.
* @param course - The course to add.
* @param hasColor - If the course block already has colors manually set
* @returns A promise that resolves to void.
* @throws An error if the schedule is not found.
*/
export default async function addCourse(scheduleId: string, course: Course): Promise<void> {
export default async function addCourse(scheduleId: string, course: Course, hasColor = false): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const activeSchedule = schedules.find(s => s.id === scheduleId);
if (!activeSchedule) {
throw new Error('Schedule not found');
}
course.colors = getUnusedColor(activeSchedule, course);
if (!hasColor) {
course.colors = getUnusedColor(activeSchedule, course);
}
activeSchedule.courses.push(course);
activeSchedule.updatedAt = Date.now();
await UserScheduleStore.set('schedules', schedules);

View File

@@ -0,0 +1,24 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Exports the provided schedule to a portable JSON
*
* @param scheduleId - The Id matching the to-be-exported schedule
* @returns JSON format of the provided schedule ID, empty if one was not found
*/
export default async function exportSchedule(scheduleId: string): Promise<string | undefined> {
try {
const storageData = await UserScheduleStore.get('schedules');
const selectedSchedule = storageData.find(s => s.id === scheduleId);
if (!selectedSchedule) {
console.warn(`Schedule ${scheduleId} does not exist`);
return JSON.stringify({});
}
console.log(selectedSchedule);
return JSON.stringify(selectedSchedule, null, 2);
} catch (error) {
console.error('Error getting storage data:', error);
}
}

View File

@@ -0,0 +1,35 @@
import { Course } from '@shared/types/Course';
import type { UserSchedule } from '@shared/types/UserSchedule';
import type { Serialized } from 'chrome-extension-toolkit';
import addCourse from './addCourse';
import createSchedule from './createSchedule';
import switchSchedule from './switchSchedule';
function isValidSchedule(data: unknown): data is Serialized<UserSchedule> {
if (typeof data !== 'object' || data === null) return false;
const schedule = data as Record<string, unknown>;
return typeof schedule.id === 'string' && typeof schedule.name === 'string' && Array.isArray(schedule.courses);
}
/**
* Imports a user schedule from portable file, creating a new schedule for it
* @param scheduleData - Data to be parsed back into a course schedule
*/
export default async function importSchedule(scheduleData: unknown): Promise<void> {
if (isValidSchedule(scheduleData)) {
const newScheduleId = await createSchedule(scheduleData.name);
await switchSchedule(newScheduleId);
for (const c of scheduleData.courses) {
const course = new Course(c);
// eslint-disable-next-line no-await-in-loop
await addCourse(newScheduleId, course, true);
console.log(course.colors);
}
console.log('Course schedule successfully parsed!');
} else {
console.error('No schedule data provided for import');
}
}