All files / src/lib storage.ts

100% Statements 19/19
100% Branches 5/5
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48        10x   10x           10x 669x     10x 185x 185x   6x 6x         10x 237x 237x   2x               10x 247x 247x 247x   6x 6x      
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
/** When the donation prompt was last dismissed, in epoch milliseconds. */
export const DONATION_LAST_SHOWN_KEY = "donation-last-shown";
/** Set for the rest of the session once the splash screen is dismissed. */
export const SPLASH_SHOWN_KEY = "splash-shown";
 
export type StorageArea = "local" | "session";
 
// Touching the storage objects can throw too, when the webview blocks storage,
// so the lookup happens inside each try.
const storageFor = (area: StorageArea): Storage =>
	area === "session" ? window.sessionStorage : window.localStorage;
 
/** The stored value, or null when it is missing or storage can't be read. */
export const readStorage = (key: string, area: StorageArea = "local"): string | null => {
	try {
		return storageFor(area).getItem(key);
	} catch (err) {
		console.warn(`[storage] could not read ${key}:`, err);
		return null;
	}
};
 
/** Removes a stored value. Blocked storage is logged and otherwise ignored. */
export const removeStorage = (key: string, area: StorageArea = "local"): void => {
	try {
		storageFor(area).removeItem(key);
	} catch (err) {
		console.warn(`[storage] could not remove ${key}:`, err);
	}
};
 
/**
 * Stores a value and reports whether it was saved. A full quota or blocked
 * storage returns false instead of throwing.
 */
export const writeStorage = (key: string, value: string, area: StorageArea = "local"): boolean => {
	try {
		storageFor(area).setItem(key, value);
		return true;
	} catch (err) {
		console.warn(`[storage] could not write ${key}:`, err);
		return false;
	}
};