From 772dfd58f6798765fd5afbe43eebf1c7972c68ad Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:50:49 -0600 Subject: [PATCH] chore: clean up storage --- apps/gpa-calculator/src/scripts/storage.ts | 2 +- apps/phs-map/src/data/data-types.ts | 4 +- apps/phs-map/src/data/storage.ts | 45 ++++++++++++++++++++++ apps/phs-map/src/script.ts | 31 +++++++-------- 4 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 apps/phs-map/src/data/storage.ts diff --git a/apps/gpa-calculator/src/scripts/storage.ts b/apps/gpa-calculator/src/scripts/storage.ts index 67a0893..4079bd3 100644 --- a/apps/gpa-calculator/src/scripts/storage.ts +++ b/apps/gpa-calculator/src/scripts/storage.ts @@ -8,7 +8,7 @@ interface StorageData { gradestorage: string; } -const storage: Storage = createStorage({ +const storage: Storage = createStorage({ driver: indexedDbDriver({ base: "gpa:" }), }); diff --git a/apps/phs-map/src/data/data-types.ts b/apps/phs-map/src/data/data-types.ts index 91935be..f463f1a 100644 --- a/apps/phs-map/src/data/data-types.ts +++ b/apps/phs-map/src/data/data-types.ts @@ -106,4 +106,6 @@ type GWing = "G"; */ type HWing = "H"; -export type { Coords, Coords2D, Level, Lvl, Rooms, StairList, Wing }; +type Shade = "dark" | "light"; + +export type { Coords, Coords2D, Level, Lvl, Rooms, Shade, StairList, Wing }; diff --git a/apps/phs-map/src/data/storage.ts b/apps/phs-map/src/data/storage.ts new file mode 100644 index 0000000..96aa0b3 --- /dev/null +++ b/apps/phs-map/src/data/storage.ts @@ -0,0 +1,45 @@ +import { createStorage, type Storage } from "unstorage"; +import indexedDbDriver from "unstorage/drivers/indexedb"; + +import type { Shade } from "./data-types.ts"; +import type { ProfilesList } from "./schemas.ts"; + +interface StorageData { + profiles: ProfilesList; + shade: Shade; +} + +const storage: Storage = createStorage({ + driver: indexedDbDriver({ base: "map:" }), +}); + +const profilesStorage = "profiles"; +const shadeStorage = "shade"; + +/** Clears all website storage data. */ +async function clearAll(): Promise { + await storage.clear(); + globalThis.location.reload(); +} + +/** Sets the storage data. */ +async function setProfiles(value: ProfilesList): Promise { + await storage.setItem(profilesStorage, value); +} + +/** Gets the storage data. */ +async function getProfiles(): Promise { + return await storage.getItem(profilesStorage); +} + +/** Sets the grade. */ +async function setShade(value: Shade): Promise { + await storage.setItem(shadeStorage, value); +} + +/** Gets the grade. */ +async function getShade(): Promise { + return await storage.getItem(shadeStorage); +} + +export { clearAll, getProfiles, getShade, setProfiles, setShade }; diff --git a/apps/phs-map/src/script.ts b/apps/phs-map/src/script.ts index 7632c9d..a828b7d 100644 --- a/apps/phs-map/src/script.ts +++ b/apps/phs-map/src/script.ts @@ -24,8 +24,6 @@ import { } from "@fortawesome/free-solid-svg-icons"; import html from "html-template-tag"; import * as PF from "pathfinding"; -import { createStorage, type Storage } from "unstorage"; -import indexedDbDriver from "unstorage/drivers/indexedb"; import { fromZodError } from "zod-validation-error"; import type { Coords2D, Level, Lvl, StairList } from "./data/data-types.ts"; @@ -40,10 +38,13 @@ import { roomSchema, } from "./data/schemas.ts"; import { btmStairs, stairs } from "./data/stairs.ts"; - -const storage: Storage = createStorage({ - driver: indexedDbDriver({ base: "map:" }), -}); +import { + clearAll, + getProfiles, + getShade, + setProfiles, + setShade, +} from "./data/storage.ts"; declare global { function startApp(): Promise; @@ -96,6 +97,8 @@ library.add( ); dom.watch(); +globalThis.clearAll = clearAll; + /** * Despite the name, this function is purely functional and has no state, though it does perform a side effect. * @@ -116,12 +119,6 @@ function toggleNav(isOpen: boolean): void { } globalThis.toggleNav = toggleNav; -async function clearAll(): Promise { - await storage.clear(); - globalThis.location.reload(); -} -globalThis.clearAll = clearAll; - function createProfile(profNum: number): void { prof = profNum; const tempElementId = `tempProf${prof}`; @@ -220,7 +217,7 @@ function createCourse(num: number, profNum: number): void { const zodErrorElement = document.querySelector("#zod-error"); async function applySavedProfiles(): Promise { - const unparsedProfiles = await storage.getItem("profiles"); + const unparsedProfiles = await getProfiles(); const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles ?? []); if (parsedProfiles.success) { @@ -273,7 +270,7 @@ async function remProf(profNum: number): Promise { id="tempProf1" >`; - await storage.setItem("profiles", profiles); + await setProfiles(profiles); await applySavedProfiles(); } globalThis.remProf = remProf; @@ -371,7 +368,7 @@ async function locateCourses(profNum: number): Promise { ]; } - await storage.setItem("profiles", profiles); + await setProfiles(profiles); } globalThis.locateCourses = locateCourses; @@ -599,7 +596,7 @@ async function toggleDarkMode(): Promise { const darkModeButton = document.querySelector("#darkModeButton")!; darkModeButton.innerHTML = isDarkMode ? "Light Mode" : "Dark Mode"; - await storage.setItem("shade", isDarkMode ? "dark" : "light"); + await setShade(isDarkMode ? "dark" : "light"); } globalThis.toggleDarkMode = toggleDarkMode; @@ -607,7 +604,7 @@ async function startApp(): Promise { lvl(1); await applySavedProfiles(); - if ((await storage.getItem("shade")) === "dark") { + if ((await getShade()) === "dark") { await toggleDarkMode(); } }