From d06b0f9f7ac0c1253a831ff5183b4ce7d85939a3 Mon Sep 17 00:00:00 2001 From: Sriram Hariharan Date: Fri, 10 Mar 2023 23:37:57 -0600 Subject: [PATCH] infrastructure for multiple schedules --- package-lock.json | 14 ++--- package.json | 2 +- src/background/storage/DevStore.ts | 6 +- src/background/storage/ExtensionStore.ts | 38 +++++++----- src/background/storage/OptionsStore.ts | 6 +- src/background/storage/SessionStore.ts | 16 ++--- src/background/storage/UserScheduleStore.ts | 62 +++++++++++++++++++ src/shared/types/Course.ts | 1 + .../GradeDistribution.module.scss | 3 +- .../GradeDistribution/GradeDistribution.tsx | 1 + todo.md | 4 +- 11 files changed, 113 insertions(+), 40 deletions(-) create mode 100644 src/background/storage/UserScheduleStore.ts diff --git a/package-lock.json b/package-lock.json index 7f368712..72e7111e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0", "dependencies": { "@types/sql.js": "^1.4.4", - "chrome-extension-toolkit": "^0.0.23", + "chrome-extension-toolkit": "^0.0.32", "classnames": "^2.3.2", "clean-webpack-plugin": "^4.0.0", "highcharts": "^10.3.3", @@ -4808,9 +4808,9 @@ } }, "node_modules/chrome-extension-toolkit": { - "version": "0.0.23", - "resolved": "https://registry.npmjs.org/chrome-extension-toolkit/-/chrome-extension-toolkit-0.0.23.tgz", - "integrity": "sha512-Xjj96nyXDbnD/1sCInEW7WtQ+bsOHFXSyjfV5KAUX7IdhhNb0BXfTX4Q7FGh00JhxPW2rUwe0l0u0iYN9GaxZw==", + "version": "0.0.32", + "resolved": "https://registry.npmjs.org/chrome-extension-toolkit/-/chrome-extension-toolkit-0.0.32.tgz", + "integrity": "sha512-0MZFp7MfVVgW1OSZbJD120+21GLS5DFjUG3sAsmAUoPUxbOhmqltGkQT9S23KuipjslcRCwzSlWR+lIkxeXHKw==", "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0" @@ -21236,9 +21236,9 @@ } }, "chrome-extension-toolkit": { - "version": "0.0.23", - "resolved": "https://registry.npmjs.org/chrome-extension-toolkit/-/chrome-extension-toolkit-0.0.23.tgz", - "integrity": "sha512-Xjj96nyXDbnD/1sCInEW7WtQ+bsOHFXSyjfV5KAUX7IdhhNb0BXfTX4Q7FGh00JhxPW2rUwe0l0u0iYN9GaxZw==", + "version": "0.0.32", + "resolved": "https://registry.npmjs.org/chrome-extension-toolkit/-/chrome-extension-toolkit-0.0.32.tgz", + "integrity": "sha512-0MZFp7MfVVgW1OSZbJD120+21GLS5DFjUG3sAsmAUoPUxbOhmqltGkQT9S23KuipjslcRCwzSlWR+lIkxeXHKw==", "requires": { "react": "^18.2.0", "react-dom": "^18.2.0" diff --git a/package.json b/package.json index 9fe24b88..2c9ddbdb 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "dependencies": { "@types/sql.js": "^1.4.4", - "chrome-extension-toolkit": "^0.0.23", + "chrome-extension-toolkit": "^0.0.32", "classnames": "^2.3.2", "clean-webpack-plugin": "^4.0.0", "highcharts": "^10.3.3", diff --git a/src/background/storage/DevStore.ts b/src/background/storage/DevStore.ts index 81c83a6a..9e959e91 100644 --- a/src/background/storage/DevStore.ts +++ b/src/background/storage/DevStore.ts @@ -1,4 +1,4 @@ -import { createStore } from 'chrome-extension-toolkit'; +import { createLocalStore, debugStore } from 'chrome-extension-toolkit'; /** * A store that is used to store data that is only relevant during development @@ -16,10 +16,12 @@ interface IDevStore { reloadTabId?: number; } -export const DevStore = createStore('DEV_STORE', { +export const DevStore = createLocalStore({ debugTabId: undefined, isTabReloading: true, wasDebugTabVisible: false, isExtensionReloading: true, reloadTabId: undefined, }); + +debugStore({ DevStore }); diff --git a/src/background/storage/ExtensionStore.ts b/src/background/storage/ExtensionStore.ts index 843a171f..89d912dc 100644 --- a/src/background/storage/ExtensionStore.ts +++ b/src/background/storage/ExtensionStore.ts @@ -1,5 +1,5 @@ import { v4 as uuidv4 } from 'uuid'; -import { createStore } from 'chrome-extension-toolkit'; +import { createLocalStore, debugStore } from 'chrome-extension-toolkit'; /** * A store that is used for storing user options @@ -13,19 +13,27 @@ interface IExtensionStore { deviceId: string; } -const base = createStore('EXTENSION_STORE', { - version: chrome.runtime.getManifest().version, - lastUpdate: Date.now(), - deviceId: '', -}); +interface Actions { + getDeviceId(): Promise; +} -export const ExtensionStore = base.modify({ - async getDeviceId() { - const deviceId = await base.getDeviceId(); - if (deviceId) { - return deviceId; - } - const newDeviceId = uuidv4(); - return newDeviceId; +const ExtensionStore = createLocalStore( + { + version: chrome.runtime.getManifest().version, + lastUpdate: Date.now(), + deviceId: '', }, -}); + store => ({ + getDeviceId: async () => { + const deviceId = await store.getDeviceId(); + if (deviceId) { + return deviceId; + } + const newDeviceId = uuidv4(); + await store.setDeviceId(newDeviceId); + return newDeviceId; + }, + }) +); + +debugStore({ ExtensionStore }); diff --git a/src/background/storage/OptionsStore.ts b/src/background/storage/OptionsStore.ts index 74523067..bc5cccbf 100644 --- a/src/background/storage/OptionsStore.ts +++ b/src/background/storage/OptionsStore.ts @@ -1,4 +1,4 @@ -import { createStore } from 'chrome-extension-toolkit'; +import { createSyncStore, debugStore } from 'chrome-extension-toolkit'; /** * A store that is used for storing user options @@ -10,7 +10,9 @@ interface IOptionsStore { shouldScrollToLoad: boolean; } -export const OptionsStore = createStore('OPTIONS_STORE', { +export const OptionsStore = createSyncStore({ shouldHighlightConflicts: true, shouldScrollToLoad: true, }); + +debugStore({ OptionsStore }); diff --git a/src/background/storage/SessionStore.ts b/src/background/storage/SessionStore.ts index 71b666a9..a8879d58 100644 --- a/src/background/storage/SessionStore.ts +++ b/src/background/storage/SessionStore.ts @@ -1,15 +1,11 @@ -import { createStore, Store } from 'chrome-extension-toolkit'; +import { createSessionStore, debugStore } from 'chrome-extension-toolkit'; interface ISessionStore { chromeSessionId?: string; } -export const SessionStore = createStore( - 'SESSION_STORE', - { - chromeSessionId: undefined, - }, - { - area: 'session', - } -); +export const SessionStore = createSessionStore({ + chromeSessionId: undefined, +}); + +debugStore({ SessionStore }); diff --git a/src/background/storage/UserScheduleStore.ts b/src/background/storage/UserScheduleStore.ts new file mode 100644 index 00000000..5eabed42 --- /dev/null +++ b/src/background/storage/UserScheduleStore.ts @@ -0,0 +1,62 @@ +import { createLocalStore, debugStore, Serialized } from 'chrome-extension-toolkit'; +import { Course } from 'src/shared/types/Course'; +/** + * A store that is used for storing user options + */ +interface IUserScheduleStore { + current: string; + schedules: { + [id: string]: Course[]; + }; +} + +interface Actions { + createSchedule(name: string): Promise; + addCourseToSchedule(name: string, course: Course): Promise; + removeCourseFromSchedule(name: string, course: Course): Promise; + removeSchedule(name: string): Promise; + getSchedule(name: string): Promise; +} + +const UserScheduleStore = createLocalStore( + { + current: 'Schedule 1', + schedules: {}, + }, + store => ({ + async createSchedule(name: string) { + const schedules = await store.getSchedules(); + if (!schedules[name]) { + schedules[name] = []; + await store.setSchedules(schedules as any); + } + }, + async removeSchedule(name: string) { + const schedules = await store.getSchedules(); + delete schedules[name]; + await store.setSchedules(schedules); + }, + async getSchedule(name) { + const schedules = await store.getSchedules(); + return schedules[name]?.map(course => new Course(course)); + }, + async addCourseToSchedule(name, course) { + const schedules = await store.getSchedules(); + const scheduleToEdit = schedules[name]; + if (scheduleToEdit) { + scheduleToEdit.push(course as Serialized); + await store.setSchedules(schedules); + } + }, + async removeCourseFromSchedule(name, course) { + const schedules = await store.getSchedules(); + const scheduleToEdit = schedules[name]; + if (scheduleToEdit) { + schedules[name] = scheduleToEdit.filter(c => c.uniqueId !== course.uniqueId); + await store.setSchedules(schedules); + } + }, + }) +); + +debugStore({ UserScheduleStore }); diff --git a/src/shared/types/Course.ts b/src/shared/types/Course.ts index 1f02976f..3862df68 100644 --- a/src/shared/types/Course.ts +++ b/src/shared/types/Course.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-classes-per-file */ import { Serialized } from 'chrome-extension-toolkit'; import { capitalize } from '../util/string'; import { CourseSchedule } from './CourseSchedule'; diff --git a/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.module.scss b/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.module.scss index 3af1a719..a1a909a5 100644 --- a/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.module.scss +++ b/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.module.scss @@ -16,7 +16,8 @@ select { z-index: $MAX_Z_INDEX; padding: 4px; - border-radius: 12px; + font-family: 'Inter'; + border-radius: 8px; border-color: $charcoal; } } diff --git a/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.tsx b/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.tsx index 579f8752..024391fb 100644 --- a/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.tsx +++ b/src/views/components/injected/CoursePopup/GradeDistribution/GradeDistribution.tsx @@ -14,6 +14,7 @@ import { queryAggregateDistribution, querySemesterDistribution, } from 'src/views/lib/database/queryDistribution'; +import { bMessenger } from 'src/shared/messages'; import styles from './GradeDistribution.module.scss'; enum DataStatus { diff --git a/todo.md b/todo.md index 503dceb1..df9f7e2c 100644 --- a/todo.md +++ b/todo.md @@ -23,7 +23,7 @@ Last Updated: 03/4/2023 - [ ] Tooltip that says which classes conflict, maybe suggest a different section or time that doesn't conflict - [ ] import / export schedules from JSON file - [ ] Search for classes from within extension -- [ ] Grade distribution +- [x] Grade distribution - [x] Course description - [x] Highlight and use rich text to make course description more readable - [ ] calendar @@ -71,7 +71,7 @@ Last Updated: 03/4/2023 - [ ] polish - [ ] more 'at a glance info' - [ ] Rearrange Courses -- [ ] Firefo Support +- [ ] Firefox Support - [ ] RMP (cumulative score for class based off CIS, RMP, grade dist., etc.) - [ ] Conflict indicator ( Possible different section? ) - [ ] Compare classes mode