This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/pwa-install-banner.js.es6
Jarek Radosz fe588cc7f8
DEV: Fix function prototype deprecations (#8681)
* DEV: Fix the function prototype observers deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.observes('foo') to observer('foo', function() {}). [deprecation id: function-prototype-extensions.observes] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-observes for more details.

* DEV: Fix the function prototype event listeners deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.on('foo') to on('foo', function() {}). [deprecation id: function-prototype-extensions.on] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-on for more details.

* DEV: Simplify `default as` imports

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2020-01-16 18:56:53 +01:00

64 lines
2.0 KiB
JavaScript

import { bind } from "@ember/runloop";
import Component from "@ember/component";
import discourseComputed, { on } from "discourse-common/utils/decorators";
const USER_DISMISSED_PROMPT_KEY = "dismissed-pwa-install-banner";
export default Component.extend({
deferredInstallPromptEvent: null,
_handleInstallPromptEvent(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() {
this._promptEventHandler = bind(this, this._handleInstallPromptEvent);
window.addEventListener("beforeinstallprompt", this._promptEventHandler);
},
@on("willDestroyElement")
_unregisterListener() {
window.removeEventListener("beforeinstallprompt", this._promptEventHandler);
},
@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() {
const launchedFromDiscourseHub =
window.location.search.indexOf("discourse_app=1") !== -1;
return (
this.capabilities.isAndroid &&
this.get("currentUser.trust_level") > 0 &&
this.deferredInstallPromptEvent && // Pass the browser engagement checks
!window.matchMedia("(display-mode: standalone)").matches && // Not be in the installed PWA already
!launchedFromDiscourseHub && // not launched via official app
!this.bannerDismissed // Have not a previously dismissed install banner
);
},
actions: {
turnOn() {
this.set("bannerDismissed", true);
this.deferredInstallPromptEvent.prompt();
},
dismiss() {
this.set("bannerDismissed", true);
}
}
});