moved stores back into shared

This commit is contained in:
Sriram Hariharan
2023-03-10 23:38:39 -06:00
parent d06b0f9f7a
commit 32b73da959
12 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,27 @@
import { createLocalStore, debugStore } from 'chrome-extension-toolkit';
/**
* A store that is used to store data that is only relevant during development
*/
interface IDevStore {
/** the tabId for the debug tab */
debugTabId?: number;
/** whether the debug tab is visible */
wasDebugTabVisible?: boolean;
/** whether we should enable extension reloading */
isExtensionReloading?: boolean;
/** whether we should enable tab reloading */
isTabReloading?: boolean;
/** The id of the tab that we want to reload (after the extension reloads itself ) */
reloadTabId?: number;
}
export const DevStore = createLocalStore<IDevStore>({
debugTabId: undefined,
isTabReloading: true,
wasDebugTabVisible: false,
isExtensionReloading: true,
reloadTabId: undefined,
});
debugStore({ DevStore });

View File

@@ -0,0 +1,39 @@
import { v4 as uuidv4 } from 'uuid';
import { createLocalStore, debugStore } from 'chrome-extension-toolkit';
/**
* A store that is used for storing user options
*/
interface IExtensionStore {
/** These values are cached in storage, so that we can know the previous version that the extension was before the current update. Is only used for onUpdate */
version: string;
/** When was the last update */
lastUpdate: number;
/** A unique identifier generated for the current user in lieu of a userId */
deviceId: string;
}
interface Actions {
getDeviceId(): Promise<string>;
}
const ExtensionStore = createLocalStore<IExtensionStore, Actions>(
{
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 });

View File

@@ -0,0 +1,18 @@
import { createSyncStore, debugStore } from 'chrome-extension-toolkit';
/**
* A store that is used for storing user options
*/
interface IOptionsStore {
/** whether we should automatically highlight conflicts on the course schedule page */
shouldHighlightConflicts: boolean;
/** whether we should automatically scroll to load more courses on the course schedule page (without having to click next) */
shouldScrollToLoad: boolean;
}
export const OptionsStore = createSyncStore<IOptionsStore>({
shouldHighlightConflicts: true,
shouldScrollToLoad: true,
});
debugStore({ OptionsStore });

View File

@@ -0,0 +1,11 @@
import { createSessionStore, debugStore } from 'chrome-extension-toolkit';
interface ISessionStore {
chromeSessionId?: string;
}
export const SessionStore = createSessionStore<ISessionStore>({
chromeSessionId: undefined,
});
debugStore({ SessionStore });

View File

@@ -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<void>;
addCourseToSchedule(name: string, course: Course): Promise<void>;
removeCourseFromSchedule(name: string, course: Course): Promise<void>;
removeSchedule(name: string): Promise<void>;
getSchedule(name: string): Promise<Course[] | undefined>;
}
const UserScheduleStore = createLocalStore<IUserScheduleStore, Actions>(
{
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<Course>);
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 });