/
pwa-install-banner.js
59 lines (51 loc) · 1.79 KB
/
pwa-install-banner.js
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
import Component from "@ember/component";
import discourseComputed, { bind, on } from "discourse-common/utils/decorators";
const USER_DISMISSED_PROMPT_KEY = "dismissed-pwa-install-banner";
export default Component.extend({
deferredInstallPromptEvent: null,
@bind
_onInstallPrompt(event) {
// Prevent Chrome 76+ from automatically showing the prompt
event.preventDefault();
// Stash the event so it can be triggered later
this.set("deferredInstallPromptEvent", event);
},
@on("didInsertElement")
_registerListener() {
window.addEventListener("beforeinstallprompt", this._onInstallPrompt);
},
@on("willDestroyElement")
_unregisterListener() {
window.removeEventListener("beforeinstallprompt", this._onInstallPrompt);
},
@discourseComputed
bannerDismissed: {
set(value) {
this.keyValueStore.set({ key: USER_DISMISSED_PROMPT_KEY, value });
return this.keyValueStore.get(USER_DISMISSED_PROMPT_KEY);
},
get() {
return this.keyValueStore.get(USER_DISMISSED_PROMPT_KEY);
},
},
@discourseComputed("deferredInstallPromptEvent", "bannerDismissed")
showPWAInstallBanner(deferredInstallPromptEvent, bannerDismissed) {
return (
this.capabilities.isAndroid &&
this.get("currentUser.trust_level") > 0 &&
deferredInstallPromptEvent && // Pass the browser engagement checks
!window.matchMedia("(display-mode: standalone)").matches && // Not be in the installed PWA already
!this.capabilities.isAppWebview && // not launched via official app
!bannerDismissed // Have not a previously dismissed install banner
);
},
actions: {
turnOn() {
this.set("bannerDismissed", true);
this.deferredInstallPromptEvent.prompt();
},
dismiss() {
this.set("bannerDismissed", true);
},
},
});