2023-07-04 17:09:20 +00:00
|
|
|
import {createValue, type Value} from "@holllo/webextension-storage";
|
2023-06-23 10:52:03 +00:00
|
|
|
import browser from "webextension-polyfill";
|
2023-06-27 11:51:04 +00:00
|
|
|
import {Data, Feature} from "./enums.js";
|
2023-06-30 09:21:39 +00:00
|
|
|
import {collectHideTopicsData} from "./hide-topics.js";
|
2023-06-29 16:51:18 +00:00
|
|
|
import {defaultKnownGroups} from "./known-groups.js";
|
2023-06-28 16:46:27 +00:00
|
|
|
import {collectUsernameColors} from "./username-color.js";
|
|
|
|
import {collectUserLabels} from "./user-label.js";
|
2023-06-23 10:52:03 +00:00
|
|
|
|
2023-06-27 11:51:04 +00:00
|
|
|
export * from "./enums.js";
|
2023-06-30 09:21:39 +00:00
|
|
|
export * from "./hide-topics.js";
|
2023-06-28 16:46:27 +00:00
|
|
|
export * from "./username-color.js";
|
|
|
|
export * from "./user-label.js";
|
2023-06-23 10:52:03 +00:00
|
|
|
|
2023-06-29 16:44:30 +00:00
|
|
|
/**
|
|
|
|
* The data stored for the Hide Votes feature.
|
|
|
|
*/
|
2023-06-23 10:52:03 +00:00
|
|
|
export type HideVotesData = {
|
|
|
|
otherComments: boolean;
|
|
|
|
otherTopics: boolean;
|
|
|
|
ownComments: boolean;
|
|
|
|
ownTopics: boolean;
|
|
|
|
};
|
|
|
|
|
2023-06-29 16:44:30 +00:00
|
|
|
/**
|
|
|
|
* All storage {@link Value}s stored in WebExtension storage.
|
|
|
|
*/
|
2023-06-23 10:52:03 +00:00
|
|
|
export const storageValues = {
|
|
|
|
[Data.EnabledFeatures]: createValue({
|
|
|
|
deserialize: (input) => new Set(JSON.parse(input) as Feature[]),
|
|
|
|
serialize: (input) => JSON.stringify(Array.from(input)),
|
|
|
|
key: Data.EnabledFeatures,
|
2023-06-29 16:36:10 +00:00
|
|
|
value: new Set([
|
|
|
|
Feature.BackToTop,
|
|
|
|
Feature.JumpToNewComment,
|
|
|
|
Feature.MarkdownToolbar,
|
|
|
|
Feature.UserLabels,
|
|
|
|
]),
|
2023-06-23 10:52:03 +00:00
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
|
|
|
[Data.KnownGroups]: createValue({
|
|
|
|
deserialize: (input) => new Set(JSON.parse(input) as string[]),
|
|
|
|
serialize: (input) => JSON.stringify(Array.from(input)),
|
|
|
|
key: Data.KnownGroups,
|
2023-06-29 16:51:18 +00:00
|
|
|
value: new Set(defaultKnownGroups),
|
2023-06-23 10:52:03 +00:00
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
|
|
|
[Data.LatestActiveFeatureTab]: createValue({
|
|
|
|
deserialize: (input) => JSON.parse(input) as Feature,
|
|
|
|
serialize: (input) => JSON.stringify(input),
|
|
|
|
key: Data.LatestActiveFeatureTab,
|
|
|
|
value: Feature.Debug,
|
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
2023-06-24 14:32:47 +00:00
|
|
|
[Data.Version]: createValue({
|
|
|
|
deserialize: (input) => JSON.parse(input) as string,
|
|
|
|
serialize: (input) => JSON.stringify(input),
|
|
|
|
key: Data.Version,
|
|
|
|
value: "2.0.0",
|
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
2023-07-09 15:15:10 +00:00
|
|
|
[Data.RandomizeUsernameColors]: createValue({
|
|
|
|
deserialize: (input) => JSON.parse(input) as boolean,
|
|
|
|
serialize: (input) => JSON.stringify(input),
|
|
|
|
key: Data.RandomizeUsernameColors,
|
|
|
|
value: false,
|
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
2023-06-30 09:21:39 +00:00
|
|
|
[Feature.HideTopics]: collectHideTopicsData(),
|
2023-06-23 10:52:03 +00:00
|
|
|
[Feature.HideVotes]: createValue({
|
|
|
|
deserialize: (input) => JSON.parse(input) as HideVotesData,
|
|
|
|
serialize: (input) => JSON.stringify(input),
|
|
|
|
key: Feature.HideVotes,
|
|
|
|
value: {
|
|
|
|
otherComments: false,
|
|
|
|
otherTopics: false,
|
|
|
|
ownComments: true,
|
|
|
|
ownTopics: true,
|
|
|
|
},
|
|
|
|
storage: browser.storage.sync,
|
|
|
|
}),
|
2023-06-24 12:00:27 +00:00
|
|
|
[Feature.UserLabels]: collectUserLabels(),
|
2023-06-28 16:46:27 +00:00
|
|
|
[Feature.UsernameColors]: collectUsernameColors(),
|
2023-06-23 10:52:03 +00:00
|
|
|
};
|
|
|
|
|
2023-06-29 16:44:30 +00:00
|
|
|
/**
|
|
|
|
* Shorthand for the inferred shape of {@link storageValues}.
|
|
|
|
*/
|
2023-06-23 10:52:03 +00:00
|
|
|
type StorageValues = typeof storageValues;
|
|
|
|
|
2023-06-29 16:44:30 +00:00
|
|
|
/**
|
|
|
|
* Return the {@link Value}-wrapped data associated with a particular key.
|
|
|
|
* @param key The key of the value to get from {@link storageValues}.
|
|
|
|
*/
|
2023-06-23 10:52:03 +00:00
|
|
|
export async function fromStorage<K extends keyof StorageValues>(
|
|
|
|
key: K,
|
|
|
|
): Promise<StorageValues[K]> {
|
|
|
|
return storageValues[key];
|
|
|
|
}
|