Files
ncd-fe/packages/pl-fe/src/settings.ts
nicole mikołajczyk 2c87087c73 pl-fe: tag history is not used
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
2025-10-29 00:13:12 +01:00

55 lines
1.1 KiB
TypeScript

class Settings {
keyBase: string | null = null;
constructor(keyBase: string | null = null) {
this.keyBase = keyBase;
}
generateKey(id: string) {
return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
}
set(id: string, data: any) {
const key = this.generateKey(id);
try {
const encodedData = JSON.stringify(data);
localStorage.setItem(key, encodedData);
return data;
} catch (e) {
return null;
}
}
get(id: string) {
const key = this.generateKey(id);
try {
const rawData = localStorage.getItem(key);
return rawData ? JSON.parse(rawData) : null;
} catch (e) {
return null;
}
}
remove(id: string) {
const data = this.get(id);
if (data) {
const key = this.generateKey(id);
try {
localStorage.removeItem(key);
} catch (e) {
// Do nothing
}
}
return data;
}
}
/** Remember push notification settings. */
const pushNotificationsSetting = new Settings('plfe_push_notification_data');
export {
pushNotificationsSetting,
};