queue/source/utilities/settings.ts

104 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-11-11 17:17:37 +00:00
import {browser} from 'webextension-polyfill-ts';
import {log} from '.';
export type QItem = {
added: Date;
id: number;
text?: string;
url: string;
};
export type Settings = {
latestVersion: string;
queue: QItem[];
versionGotUpdated: boolean;
2020-11-11 17:17:37 +00:00
};
const defaultSettings: Settings = {
latestVersion: '0.0.0',
queue: [],
versionGotUpdated: false
2020-11-11 17:17:37 +00:00
};
/**
* Returns the user's settings.
*/
export async function getSettings(): Promise<Settings> {
const syncSettings: any = await browser.storage.sync.get(defaultSettings);
const queue: QItem[] = syncSettings.queue;
// Initialize all the non-JSON values, as they are stringified when saved.
for (const item of queue) {
item.added = new Date(item.added);
}
2020-11-11 17:17:37 +00:00
const settings: Settings = {
latestVersion: syncSettings.latestVersion,
queue,
versionGotUpdated: syncSettings.versionGotUpdated
2020-11-11 17:17:37 +00:00
};
return settings;
}
/**
* Saves the user's settings to local storage.
* @param settings The settings to save.
*/
export async function saveSettings(settings: Settings): Promise<Settings> {
await browser.storage.sync.set(settings);
2020-11-11 17:17:37 +00:00
return settings;
}
/**
* Returns a new ID to use for a new QItem.
* @param items All the queue items.
*/
export function newQItemID(items: QItem[]): number {
const highestItem = items.sort((a, b) => b.id - a.id)[0];
if (highestItem === undefined) {
return 1;
}
return highestItem.id + 1;
}
/**
* Removes a QItem from the Settings with the specified ID.
* @param id The ID of the QItem to be removed.
* @param settings Optional user settings to use.
*/
export async function removeQItem(
id: number,
settings?: Settings
): Promise<Settings> {
if (settings === undefined) {
settings = await getSettings();
}
const index = settings.queue.findIndex((item) => item.id === id);
if (index === -1) {
log(`No QItem with ID ${id} found.`);
return settings;
}
settings.queue.splice(index, 1);
return saveSettings(settings);
}
/**
* Returns the next QItem.
* @param settings Optional user settings to use.
*/
export async function getNextQItem(
settings?: Settings
): Promise<QItem | undefined> {
if (settings === undefined) {
settings = await getSettings();
}
settings.queue.sort((a, b) => a.added.getTime() - b.added.getTime());
return settings.queue[0];
}