2023-06-15 12:56:59 +00:00
|
|
|
import browser from "webextension-polyfill";
|
|
|
|
import {createValue} from "@holllo/webextension-storage";
|
2023-07-01 10:41:54 +00:00
|
|
|
import {type TourId} from "../tours/exports.js";
|
2023-06-15 12:56:59 +00:00
|
|
|
|
2023-07-02 10:25:48 +00:00
|
|
|
/** All available storage keys. */
|
2023-06-15 12:56:59 +00:00
|
|
|
export enum StorageKey {
|
|
|
|
IntroductionUnderstood = "introduction-understood",
|
|
|
|
ToursCompleted = "tours-completed",
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:25:48 +00:00
|
|
|
/** All values we want to save in storage. */
|
|
|
|
const storageValues = {
|
|
|
|
[StorageKey.IntroductionUnderstood]: createValue<boolean>({
|
2023-06-15 12:56:59 +00:00
|
|
|
deserialize: (input) => input === "true",
|
|
|
|
serialize: (input) => JSON.stringify(input),
|
|
|
|
key: StorageKey.IntroductionUnderstood,
|
|
|
|
storage: browser.storage.local,
|
|
|
|
value: false,
|
2023-07-02 10:25:48 +00:00
|
|
|
}),
|
|
|
|
[StorageKey.ToursCompleted]: createValue<Set<TourId>>({
|
2023-06-15 12:56:59 +00:00
|
|
|
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([]),
|
2023-07-02 10:25:48 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Alias to get the inferred type shape of {@link storageValues}. */
|
|
|
|
export type StorageValues = typeof storageValues;
|
2023-06-15 12:56:59 +00:00
|
|
|
|
2023-07-02 10:25:48 +00:00
|
|
|
/**
|
|
|
|
* Get the stored value for a given key.
|
|
|
|
* @param key The key to get from storage.
|
|
|
|
*/
|
|
|
|
export async function fromStorage<K extends StorageKey>(
|
|
|
|
key: K,
|
|
|
|
): Promise<StorageValues[K]> {
|
|
|
|
return storageValues[key];
|
2023-06-15 12:56:59 +00:00
|
|
|
}
|