Skip to content

Commit

Permalink
chore: clean up storage
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jan 16, 2025
1 parent 4ff1a85 commit 772dfd5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/gpa-calculator/src/scripts/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface StorageData {
gradestorage: string;
}

const storage: Storage<StorageData> = createStorage<StorageData>({
const storage: Storage<StorageData> = createStorage({
driver: indexedDbDriver({ base: "gpa:" }),
});

Expand Down
4 changes: 3 additions & 1 deletion apps/phs-map/src/data/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
45 changes: 45 additions & 0 deletions apps/phs-map/src/data/storage.ts
Original file line number Diff line number Diff line change
@@ -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<StorageData> = createStorage({
driver: indexedDbDriver({ base: "map:" }),
});

const profilesStorage = "profiles";
const shadeStorage = "shade";

/** Clears all website storage data. */
async function clearAll(): Promise<void> {
await storage.clear();
globalThis.location.reload();
}

/** Sets the storage data. */
async function setProfiles(value: ProfilesList): Promise<void> {
await storage.setItem(profilesStorage, value);
}

/** Gets the storage data. */
async function getProfiles(): Promise<ProfilesList | null> {
return await storage.getItem(profilesStorage);
}

/** Sets the grade. */
async function setShade(value: Shade): Promise<void> {
await storage.setItem(shadeStorage, value);
}

/** Gets the grade. */
async function getShade(): Promise<Shade | null> {
return await storage.getItem(shadeStorage);
}

export { clearAll, getProfiles, getShade, setProfiles, setShade };
31 changes: 14 additions & 17 deletions apps/phs-map/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<void>;
Expand Down Expand Up @@ -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.
*
Expand All @@ -116,12 +119,6 @@ function toggleNav(isOpen: boolean): void {
}
globalThis.toggleNav = toggleNav;

async function clearAll(): Promise<void> {
await storage.clear();
globalThis.location.reload();
}
globalThis.clearAll = clearAll;

function createProfile(profNum: number): void {
prof = profNum;
const tempElementId = `tempProf${prof}`;
Expand Down Expand Up @@ -220,7 +217,7 @@ function createCourse(num: number, profNum: number): void {
const zodErrorElement = document.querySelector("#zod-error");

async function applySavedProfiles(): Promise<void> {
const unparsedProfiles = await storage.getItem("profiles");
const unparsedProfiles = await getProfiles();
const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles ?? []);

if (parsedProfiles.success) {
Expand Down Expand Up @@ -273,7 +270,7 @@ async function remProf(profNum: number): Promise<void> {
id="tempProf1"
></div>`;

await storage.setItem("profiles", profiles);
await setProfiles(profiles);
await applySavedProfiles();
}
globalThis.remProf = remProf;
Expand Down Expand Up @@ -371,7 +368,7 @@ async function locateCourses(profNum: number): Promise<void> {
];
}

await storage.setItem("profiles", profiles);
await setProfiles(profiles);
}
globalThis.locateCourses = locateCourses;

Expand Down Expand Up @@ -599,15 +596,15 @@ async function toggleDarkMode(): Promise<void> {
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;

async function startApp(): Promise<void> {
lvl(1);
await applySavedProfiles();

if ((await storage.getItem("shade")) === "dark") {
if ((await getShade()) === "dark") {
await toggleDarkMode();
}
}
Expand Down

0 comments on commit 772dfd5

Please sign in to comment.