summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/notifications/update-available.ts
blob: 54dcc02ca8b824f3e9bde91d2184308ee7210868 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { sprintf } from 'sprintf-js';
import { links } from '../../config.json';
import { messages } from '../../shared/gettext';
import {
  InAppNotification,
  InAppNotificationProvider,
  SystemNotification,
  SystemNotificationProvider,
} from './notification';

interface UpdateAvailableNotificationContext {
  suggestedUpgrade?: string;
  suggestedIsBeta?: boolean;
}

export class UpdateAvailableNotificationProvider
  implements InAppNotificationProvider, SystemNotificationProvider {
  public constructor(private context: UpdateAvailableNotificationContext) {}

  public mayDisplay() {
    return this.context.suggestedUpgrade ? true : false;
  }

  public getInAppNotification(): InAppNotification {
    return {
      indicator: 'warning',
      title: this.context.suggestedIsBeta
        ? messages.pgettext('in-app-notifications', 'BETA UPDATE AVAILABLE')
        : messages.pgettext('in-app-notifications', 'UPDATE AVAILABLE'),
      subtitle: this.inAppMessage(),
      action: {
        type: 'open-url',
        url: this.context.suggestedIsBeta ? links.betaDownload : links.download,
      },
    };
  }

  public getSystemNotification(): SystemNotification {
    return {
      message: this.systemMessage(),
      critical: false,
      action: {
        type: 'open-url',
        url: this.context.suggestedIsBeta ? links.betaDownload : links.download,
        text: messages.pgettext('notifications', 'Upgrade'),
      },
      presentOnce: { value: true, name: this.constructor.name },
      suppressInDevelopment: true,
    };
  }

  private inAppMessage(): string {
    if (this.context.suggestedIsBeta) {
      return sprintf(
        // TRANSLATORS: The in-app banner displayed to the user when the app beta update is
        // TRANSLATORS: available.
        // TRANSLATORS: Available placeholders:
        // TRANSLATORS: %(version)s - The version number of the new beta version.
        messages.pgettext('in-app-notifications', 'Try out the newest beta version (%(version)s).'),
        { version: this.context.suggestedUpgrade },
      );
    } else {
      // TRANSLATORS: The in-app banner displayed to the user when the app update is available.
      return messages.pgettext(
        'in-app-notifications',
        'Install the latest app version to stay up to date.',
      );
    }
  }

  private systemMessage(): string {
    if (this.context.suggestedIsBeta) {
      return sprintf(
        // TRANSLATORS: The system notification that notifies the user when a beta update is
        // TRANSLATORS: available.
        // TRANSLATORS: Available placeholders:
        // TRANSLATORS: %(version)s - The version number of the new beta version.
        messages.pgettext(
          'notifications',
          'Beta update available. Try out the newest beta version (%(version)s).',
        ),
        { version: this.context.suggestedUpgrade },
      );
    } else {
      return messages.pgettext(
        'notifications',
        'Update available. Install the latest app version to stay up to date',
      );
    }
  }
}