bigbuffet-rw/app/soapbox/settings.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
'use strict';
export default class Settings {
2022-04-24 12:28:07 -07:00
keyBase: string | null = null;
constructor(keyBase: string | null = null) {
2020-03-27 13:59:38 -07:00
this.keyBase = keyBase;
}
2022-04-24 12:28:07 -07:00
generateKey(id: string) {
2020-03-27 13:59:38 -07:00
return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
}
2022-04-24 12:28:07 -07:00
set(id: string, data: any) {
2020-03-27 13:59:38 -07:00
const key = this.generateKey(id);
try {
const encodedData = JSON.stringify(data);
localStorage.setItem(key, encodedData);
return data;
} catch (e) {
return null;
}
}
2022-04-24 12:28:07 -07:00
get(id: string) {
2020-03-27 13:59:38 -07:00
const key = this.generateKey(id);
try {
const rawData = localStorage.getItem(key);
2022-04-24 12:28:07 -07:00
return rawData ? JSON.parse(rawData) : null;
2020-03-27 13:59:38 -07:00
} catch (e) {
return null;
}
}
2022-04-24 12:28:07 -07:00
remove(id: string) {
2020-03-27 13:59:38 -07:00
const data = this.get(id);
if (data) {
const key = this.generateKey(id);
try {
localStorage.removeItem(key);
} catch (e) {
2021-08-03 12:29:36 -07:00
// Do nothing
2020-03-27 13:59:38 -07:00
}
}
return data;
}
}
2022-04-24 12:28:07 -07:00
/** Remember push notification settings. */
2020-05-28 15:52:07 -07:00
export const pushNotificationsSetting = new Settings('soapbox_push_notification_data');
2022-04-24 12:28:07 -07:00
/** Remember hashtag usage. */
2020-05-28 15:52:07 -07:00
export const tagHistory = new Settings('soapbox_tag_history');
2023-02-28 07:26:27 -08:00
/** Remember group usage. */
export const groupSearchHistory = new Settings('soapbox_group_search_history');