From da1a049144700adb0a0285ffb77d51ae651008be Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 11 Jul 2024 11:53:39 +0200 Subject: [PATCH] DEV: migrates pwa-install-banner to gjs (#27859) --- .../app/components/pwa-install-banner.gjs | 81 +++++++++++++++++++ .../app/components/pwa-install-banner.hbs | 19 ----- .../app/components/pwa-install-banner.js | 59 -------------- 3 files changed, 81 insertions(+), 78 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/components/pwa-install-banner.gjs delete mode 100644 app/assets/javascripts/discourse/app/components/pwa-install-banner.hbs delete mode 100644 app/assets/javascripts/discourse/app/components/pwa-install-banner.js diff --git a/app/assets/javascripts/discourse/app/components/pwa-install-banner.gjs b/app/assets/javascripts/discourse/app/components/pwa-install-banner.gjs new file mode 100644 index 00000000000..450d3db0828 --- /dev/null +++ b/app/assets/javascripts/discourse/app/components/pwa-install-banner.gjs @@ -0,0 +1,81 @@ +import Component from "@glimmer/component"; +import { tracked } from "@glimmer/tracking"; +import { hash } from "@ember/helper"; +import { action } from "@ember/object"; +import { service } from "@ember/service"; +import { modifier as modifierFn } from "ember-modifier"; +import DButton from "discourse/components/d-button"; +import DiscourseLinkedText from "discourse/components/discourse-linked-text"; + +const USER_DISMISSED_PROMPT_KEY = "dismissed-pwa-install-banner"; + +export default class PwaInstallBanner extends Component { + @service capabilities; + @service currentUser; + @service keyValueStore; + @service siteSettings; + + @tracked + bannerDismissed = + this.keyValueStore.get(USER_DISMISSED_PROMPT_KEY) === "true"; + + @tracked deferredInstallPromptEvent = null; + + registerInstallPromptListener = modifierFn(() => { + const handler = (event) => { + // Prevent Chrome 76+ from automatically showing the prompt + event.preventDefault(); + // Stash the event so it can be triggered later + this.deferredInstallPromptEvent = event; + }; + + window.addEventListener("beforeinstallprompt", handler); + + return () => { + window.removeEventListener("beforeinstallprompt", handler); + }; + }); + + get showPWAInstallBanner() { + return ( + this.capabilities.isAndroid && + this.currentUser?.trust_level > 0 && + this.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 + !this.bannerDismissed // Have not a previously dismissed install banner + ); + } + + @action + turnOn() { + this.dismiss(); + this.deferredInstallPromptEvent.prompt(); + } + + @action + dismiss() { + this.keyValueStore.set({ key: USER_DISMISSED_PROMPT_KEY, value: true }); + this.bannerDismissed = true; + } + + +} diff --git a/app/assets/javascripts/discourse/app/components/pwa-install-banner.hbs b/app/assets/javascripts/discourse/app/components/pwa-install-banner.hbs deleted file mode 100644 index ba5ed5dfb9f..00000000000 --- a/app/assets/javascripts/discourse/app/components/pwa-install-banner.hbs +++ /dev/null @@ -1,19 +0,0 @@ -{{#if this.showPWAInstallBanner}} -
-
- - - - -
-
-{{/if}} \ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/components/pwa-install-banner.js b/app/assets/javascripts/discourse/app/components/pwa-install-banner.js deleted file mode 100644 index be46c3911cf..00000000000 --- a/app/assets/javascripts/discourse/app/components/pwa-install-banner.js +++ /dev/null @@ -1,59 +0,0 @@ -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); - }, - }, -});