aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/badge.ts
blob: 1b66effb62d50bad35b38d42688a80fb9f8a4079 (plain) (blame)
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
import { ipcRenderer } from 'electron';

const debug = require('debug')('Ferdium:Plugin:BadgeHandler');

export class BadgeHandler {
  // TODO: Need to extract this into a utility class and reuse outside of the recipes
  safeParseInt(text: string | number | undefined | null) {
    if (text === undefined || text === null) {
      return 0;
    }

    // Parse number to integer
    // This will correct errors that recipes may introduce, e.g.
    // by sending a String instead of an integer
    const parsedNumber = Number.parseInt(text.toString(), 10);
    const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber;
    return Math.max(adjustedNumber, 0);
  }

  setBadge(
    direct: string | number | undefined | null,
    indirect: string | number | undefined | null,
  ) {
    const count = {
      direct: this.safeParseInt(direct),
      indirect: this.safeParseInt(indirect),
    };

    debug('Sending badge count to host: %j', count);
    ipcRenderer.sendToHost('message-counts', count);
  }
}