2024-01-19 10:32:26 +01:00
|
|
|
type Expirable<T> = {
|
|
|
|
value: T | undefined;
|
|
|
|
expiry: number | null;
|
|
|
|
};
|
|
|
|
|
2022-02-08 12:06:25 +01:00
|
|
|
// Get an item from localStorage.
|
|
|
|
// Returns undefined if the browser denies access.
|
|
|
|
export function getLocalStorageItem<T>(key: string): T | undefined {
|
|
|
|
try {
|
2024-01-19 10:32:26 +01:00
|
|
|
const itemStr = window.localStorage.getItem(key);
|
|
|
|
if (!itemStr) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item: Expirable<T> | undefined = parseStoredItem(itemStr);
|
|
|
|
if (item?.expiry && new Date().getTime() > item.expiry) {
|
|
|
|
window.localStorage.removeItem(key);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return item?.value;
|
2022-02-08 12:06:25 +01:00
|
|
|
} catch (err: unknown) {
|
|
|
|
console.warn(err);
|
2024-01-19 10:32:26 +01:00
|
|
|
return undefined;
|
2022-02-08 12:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store an item in localStorage.
|
|
|
|
// Does nothing if the browser denies access.
|
2024-01-19 10:32:26 +01:00
|
|
|
export function setLocalStorageItem<T>(
|
|
|
|
key: string,
|
|
|
|
value: T | undefined = undefined,
|
|
|
|
timeToLive?: number,
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const item: Expirable<T> = {
|
|
|
|
value,
|
|
|
|
expiry:
|
|
|
|
timeToLive !== undefined
|
|
|
|
? new Date().getTime() + timeToLive
|
|
|
|
: null,
|
|
|
|
};
|
2024-01-29 12:33:01 +01:00
|
|
|
window.localStorage.setItem(
|
|
|
|
key,
|
|
|
|
JSON.stringify(item, (_key, value) =>
|
|
|
|
value instanceof Set ? [...value] : value,
|
|
|
|
),
|
|
|
|
);
|
2024-01-19 10:32:26 +01:00
|
|
|
} catch (err: unknown) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store an item in sessionStorage with optional TTL
|
|
|
|
export function setSessionStorageItem<T>(
|
|
|
|
key: string,
|
|
|
|
value: T | undefined = undefined,
|
|
|
|
timeToLive?: number,
|
|
|
|
) {
|
2022-02-08 12:06:25 +01:00
|
|
|
try {
|
2024-01-19 10:32:26 +01:00
|
|
|
const item: Expirable<T> = {
|
|
|
|
value,
|
|
|
|
expiry:
|
|
|
|
timeToLive !== undefined
|
|
|
|
? new Date().getTime() + timeToLive
|
|
|
|
: null,
|
|
|
|
};
|
2024-01-29 12:33:01 +01:00
|
|
|
window.sessionStorage.setItem(
|
|
|
|
key,
|
|
|
|
JSON.stringify(item, (_key, value) =>
|
|
|
|
value instanceof Set ? [...value] : value,
|
|
|
|
),
|
|
|
|
);
|
2024-01-19 10:32:26 +01:00
|
|
|
} catch (err: unknown) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an item from sessionStorage, checking for TTL
|
|
|
|
export function getSessionStorageItem<T>(key: string): T | undefined {
|
|
|
|
try {
|
|
|
|
const itemStr = window.sessionStorage.getItem(key);
|
|
|
|
if (!itemStr) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item: Expirable<T> | undefined = parseStoredItem(itemStr);
|
|
|
|
if (item?.expiry && new Date().getTime() > item.expiry) {
|
|
|
|
window.sessionStorage.removeItem(key);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return item?.value;
|
2022-02-08 12:06:25 +01:00
|
|
|
} catch (err: unknown) {
|
|
|
|
console.warn(err);
|
2024-01-19 10:32:26 +01:00
|
|
|
return undefined;
|
2022-02-08 12:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse an item from localStorage.
|
|
|
|
// Returns undefined if the item could not be parsed.
|
|
|
|
function parseStoredItem<T>(data: string | null): T | undefined {
|
|
|
|
try {
|
|
|
|
return data ? JSON.parse(data) : undefined;
|
|
|
|
} catch (err: unknown) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
}
|