2017-12-09 22:18:45 +09:00
|
|
|
export default class Settings {
|
|
|
|
|
|
|
|
constructor(keyBase = null) {
|
|
|
|
this.keyBase = keyBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateKey(id) {
|
|
|
|
return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(id, data) {
|
|
|
|
const key = this.generateKey(id);
|
|
|
|
try {
|
|
|
|
const encodedData = JSON.stringify(data);
|
|
|
|
localStorage.setItem(key, encodedData);
|
|
|
|
return data;
|
2024-09-11 22:59:46 +09:00
|
|
|
} catch {
|
2017-12-09 22:18:45 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(id) {
|
|
|
|
const key = this.generateKey(id);
|
|
|
|
try {
|
|
|
|
const rawData = localStorage.getItem(key);
|
|
|
|
return JSON.parse(rawData);
|
2024-09-11 22:59:46 +09:00
|
|
|
} catch {
|
2017-12-09 22:18:45 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(id) {
|
|
|
|
const data = this.get(id);
|
|
|
|
if (data) {
|
|
|
|
const key = this.generateKey(id);
|
|
|
|
try {
|
|
|
|
localStorage.removeItem(key);
|
2024-09-11 22:59:46 +09:00
|
|
|
} catch {
|
|
|
|
// ignore if the key is not found
|
2017-12-09 22:18:45 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export const pushNotificationsSetting = new Settings('mastodon_push_notification_data');
|
2018-03-05 04:27:25 +09:00
|
|
|
export const tagHistory = new Settings('mastodon_tag_history');
|
2022-10-09 13:08:37 +09:00
|
|
|
export const bannerSettings = new Settings('mastodon_banner_settings');
|
2023-11-08 03:15:38 +09:00
|
|
|
export const searchHistory = new Settings('mastodon_search_history');
|
|
|
|
export const playerSettings = new Settings('mastodon_player');
|