soapbox/app/soapbox/settings.ts

59 wiersze
1.3 KiB
TypeScript
Czysty Zwykły widok Historia

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