From 4d600e200d6fbdae4ee864b733414f283369e77b Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 15 Jun 2023 14:56:59 +0200 Subject: [PATCH] Add the storage logic. --- source/storage/common.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 source/storage/common.ts diff --git a/source/storage/common.ts b/source/storage/common.ts new file mode 100644 index 0000000..581f122 --- /dev/null +++ b/source/storage/common.ts @@ -0,0 +1,33 @@ +import browser from "webextension-polyfill"; +import {createValue} from "@holllo/webextension-storage"; + +export enum StorageKey { + IntroductionUnderstood = "introduction-understood", + ToursCompleted = "tours-completed", +} + +export async function createIntroductionUnderstood() { + return createValue({ + deserialize: (input) => input === "true", + serialize: (input) => JSON.stringify(input), + key: StorageKey.IntroductionUnderstood, + storage: browser.storage.local, + value: false, + }); +} + +export async function createToursCompleted() { + return createValue>({ + deserialize: (input) => new Set(JSON.parse(input) as TourId[]), + serialize: (input) => JSON.stringify(Array.from(input)), + key: StorageKey.ToursCompleted, + storage: browser.storage.local, + value: new Set([]), + }); +} + +export async function addCompletedTour(tourId: TourId): Promise { + const toursCompleted = await createToursCompleted(); + toursCompleted.value.add(tourId); + await toursCompleted.save(); +}