Skip to content

Commit 772dfd5

Browse files
committed
chore: clean up storage
1 parent 4ff1a85 commit 772dfd5

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

apps/gpa-calculator/src/scripts/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface StorageData {
88
gradestorage: string;
99
}
1010

11-
const storage: Storage<StorageData> = createStorage<StorageData>({
11+
const storage: Storage<StorageData> = createStorage({
1212
driver: indexedDbDriver({ base: "gpa:" }),
1313
});
1414

apps/phs-map/src/data/data-types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,6 @@ type GWing = "G";
106106
*/
107107
type HWing = "H";
108108

109-
export type { Coords, Coords2D, Level, Lvl, Rooms, StairList, Wing };
109+
type Shade = "dark" | "light";
110+
111+
export type { Coords, Coords2D, Level, Lvl, Rooms, Shade, StairList, Wing };

apps/phs-map/src/data/storage.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { createStorage, type Storage } from "unstorage";
2+
import indexedDbDriver from "unstorage/drivers/indexedb";
3+
4+
import type { Shade } from "./data-types.ts";
5+
import type { ProfilesList } from "./schemas.ts";
6+
7+
interface StorageData {
8+
profiles: ProfilesList;
9+
shade: Shade;
10+
}
11+
12+
const storage: Storage<StorageData> = createStorage({
13+
driver: indexedDbDriver({ base: "map:" }),
14+
});
15+
16+
const profilesStorage = "profiles";
17+
const shadeStorage = "shade";
18+
19+
/** Clears all website storage data. */
20+
async function clearAll(): Promise<void> {
21+
await storage.clear();
22+
globalThis.location.reload();
23+
}
24+
25+
/** Sets the storage data. */
26+
async function setProfiles(value: ProfilesList): Promise<void> {
27+
await storage.setItem(profilesStorage, value);
28+
}
29+
30+
/** Gets the storage data. */
31+
async function getProfiles(): Promise<ProfilesList | null> {
32+
return await storage.getItem(profilesStorage);
33+
}
34+
35+
/** Sets the grade. */
36+
async function setShade(value: Shade): Promise<void> {
37+
await storage.setItem(shadeStorage, value);
38+
}
39+
40+
/** Gets the grade. */
41+
async function getShade(): Promise<Shade | null> {
42+
return await storage.getItem(shadeStorage);
43+
}
44+
45+
export { clearAll, getProfiles, getShade, setProfiles, setShade };

apps/phs-map/src/script.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import {
2424
} from "@fortawesome/free-solid-svg-icons";
2525
import html from "html-template-tag";
2626
import * as PF from "pathfinding";
27-
import { createStorage, type Storage } from "unstorage";
28-
import indexedDbDriver from "unstorage/drivers/indexedb";
2927
import { fromZodError } from "zod-validation-error";
3028

3129
import type { Coords2D, Level, Lvl, StairList } from "./data/data-types.ts";
@@ -40,10 +38,13 @@ import {
4038
roomSchema,
4139
} from "./data/schemas.ts";
4240
import { btmStairs, stairs } from "./data/stairs.ts";
43-
44-
const storage: Storage = createStorage({
45-
driver: indexedDbDriver({ base: "map:" }),
46-
});
41+
import {
42+
clearAll,
43+
getProfiles,
44+
getShade,
45+
setProfiles,
46+
setShade,
47+
} from "./data/storage.ts";
4748

4849
declare global {
4950
function startApp(): Promise<void>;
@@ -96,6 +97,8 @@ library.add(
9697
);
9798
dom.watch();
9899

100+
globalThis.clearAll = clearAll;
101+
99102
/**
100103
* Despite the name, this function is purely functional and has no state, though it does perform a side effect.
101104
*
@@ -116,12 +119,6 @@ function toggleNav(isOpen: boolean): void {
116119
}
117120
globalThis.toggleNav = toggleNav;
118121

119-
async function clearAll(): Promise<void> {
120-
await storage.clear();
121-
globalThis.location.reload();
122-
}
123-
globalThis.clearAll = clearAll;
124-
125122
function createProfile(profNum: number): void {
126123
prof = profNum;
127124
const tempElementId = `tempProf${prof}`;
@@ -220,7 +217,7 @@ function createCourse(num: number, profNum: number): void {
220217
const zodErrorElement = document.querySelector("#zod-error");
221218

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

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

276-
await storage.setItem("profiles", profiles);
273+
await setProfiles(profiles);
277274
await applySavedProfiles();
278275
}
279276
globalThis.remProf = remProf;
@@ -371,7 +368,7 @@ async function locateCourses(profNum: number): Promise<void> {
371368
];
372369
}
373370

374-
await storage.setItem("profiles", profiles);
371+
await setProfiles(profiles);
375372
}
376373
globalThis.locateCourses = locateCourses;
377374

@@ -599,15 +596,15 @@ async function toggleDarkMode(): Promise<void> {
599596
const darkModeButton = document.querySelector("#darkModeButton")!;
600597

601598
darkModeButton.innerHTML = isDarkMode ? "Light Mode" : "Dark Mode";
602-
await storage.setItem("shade", isDarkMode ? "dark" : "light");
599+
await setShade(isDarkMode ? "dark" : "light");
603600
}
604601
globalThis.toggleDarkMode = toggleDarkMode;
605602

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

610-
if ((await storage.getItem("shade")) === "dark") {
607+
if ((await getShade()) === "dark") {
611608
await toggleDarkMode();
612609
}
613610
}

0 commit comments

Comments
 (0)