summaryrefslogtreecommitdiff
path: root/frontend/src/notification-service.tsx
blob: 188cebd3f60210b6b3f1e890c922e54e2ef569e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { DeckyState } from './components/DeckyState';
import { getSetting, setSetting } from './utils/settings';

export interface NotificationSettings {
  deckyUpdates: boolean;
  pluginUpdates: boolean;
}

export const DEFAULT_NOTIFICATION_SETTINGS: NotificationSettings = {
  deckyUpdates: true,
  pluginUpdates: true,
};

/**
 * A Service class for managing the notification settings
 *
 * It's mostly responsible for sending setting updates to the server and keeping the local state in sync.
 */
export class NotificationService {
  constructor(private deckyState: DeckyState) {}

  async init() {
    const notificationSettings = await getSetting<Partial<NotificationSettings>>('notificationSettings', {});

    // Adding a fallback to the default settings to be backwards compatible if we ever add new notification settings
    this.deckyState.setNotificationSettings({
      ...DEFAULT_NOTIFICATION_SETTINGS,
      ...notificationSettings,
    });
  }

  /**
   * Sends the new notification settings to the server and persists it locally in the decky state
   *
   * @param notificationSettings The new notification settings
   */
  async update(notificationSettings: NotificationSettings) {
    await setSetting('notificationSettings', notificationSettings);
    this.deckyState.setNotificationSettings(notificationSettings);
  }

  /**
   * For a specific event, returns true if a notification should be shown
   *
   * @param event The notification event that should be checked
   * @returns true if the notification should be shown
   */
  shouldNotify(event: keyof NotificationSettings) {
    return this.deckyState.publicState().notificationSettings[event];
  }
}