import {type Value, createValue} from "@holllo/webextension-storage"; import browser from "webextension-polyfill"; import {Feature} from "./enums.js"; /** * The data structure for a username color. */ export type UsernameColor = { color: string; id: number; username: string; }; /** * Shorthand for an array of {@link Value}-wrapped {@link UsernameColor}s. */ export type UsernameColorsData = Array>; /** * Create a {@link Value}-wrapped {@link UsernameColor}. */ export async function createValueUsernamecolor( usernameColor: UsernameColor, ): Promise { return createValue({ deserialize: (input) => JSON.parse(input) as UsernameColor, serialize: (input) => JSON.stringify(input), key: `${Feature.UsernameColors}-${usernameColor.id}`, value: usernameColor, storage: browser.storage.sync, }); } /** * Get all username colors from storage and combine them into a single array. */ export async function collectUsernameColors(): Promise { const storage = await browser.storage.sync.get(); const userLabels = []; for (const [key, value] of Object.entries(storage)) { if (!key.startsWith(Feature.UsernameColors)) { continue; } userLabels.push( await createValueUsernamecolor( JSON.parse(value as string) as UsernameColor, ), ); } return userLabels; } /** * Save all username colors to storage under individual keys. */ export async function saveUsernameColors( usernameColors: UsernameColorsData, ): Promise { for (const usernameColor of usernameColors) { await usernameColor.save(); } }