You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChromeExtensionBanner.web.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import React, { PureComponent } from 'react';
  4. import {
  5. createChromeExtensionBannerEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { getCurrentConference } from '../../base/conference/functions';
  9. import checkChromeExtensionsInstalled from '../../base/environment/checkChromeExtensionsInstalled';
  10. import {
  11. isMobileBrowser
  12. } from '../../base/environment/utils';
  13. import { translate } from '../../base/i18n';
  14. import { Icon, IconCloseLarge } from '../../base/icons';
  15. import { browser } from '../../base/lib-jitsi-meet';
  16. import { connect } from '../../base/redux';
  17. import { isVpaasMeeting } from '../../jaas/functions';
  18. import logger from '../logger';
  19. declare var interfaceConfig: Object;
  20. const emptyObject = {};
  21. /**
  22. * Local storage key name for flag telling if user checked 'Don't show again' checkbox on the banner
  23. * If the user checks this before closing the banner, next time he will access a jitsi domain
  24. * the banner will not be shown regardless of extensions being installed or not.
  25. */
  26. const DONT_SHOW_AGAIN_CHECKED = 'hide_chrome_extension_banner';
  27. /**
  28. * The type of the React {@code PureComponent} props of {@link ChromeExtensionBanner}.
  29. */
  30. type Props = {
  31. /**
  32. * Contains info about installed/to be installed chrome extension(s).
  33. */
  34. bannerCfg: Object,
  35. /**
  36. * Conference data, if any.
  37. */
  38. conference: Object,
  39. /**
  40. * Whether I am the current recorder.
  41. */
  42. iAmRecorder: boolean,
  43. /**
  44. * Whether it's a vpaas meeting or not.
  45. */
  46. isVpaas: boolean,
  47. /**
  48. * Invoked to obtain translated strings.
  49. */
  50. t: Function,
  51. };
  52. /**
  53. * The type of the React {@link PureComponent} state of {@link ChromeExtensionBanner}.
  54. */
  55. type State = {
  56. /**
  57. * Keeps the current value of dont show again checkbox.
  58. */
  59. dontShowAgainChecked: boolean,
  60. /**
  61. * Tells whether user pressed install extension or close button.
  62. */
  63. closePressed: boolean,
  64. /**
  65. * Tells whether should show the banner or not based on extension being installed or not.
  66. */
  67. shouldShow: boolean,
  68. };
  69. /**
  70. * Implements a React {@link PureComponent} which displays a banner having a link to the chrome extension.
  71. *
  72. * @class ChromeExtensionBanner
  73. * @augments PureComponent
  74. */
  75. class ChromeExtensionBanner extends PureComponent<Props, State> {
  76. /**
  77. * Initializes a new {@code ChromeExtensionBanner} instance.
  78. *
  79. * @param {Object} props - The read-only React {@code PureComponent} props with
  80. * which the new instance is to be initialized.
  81. */
  82. constructor(props: Props) {
  83. super(props);
  84. this.state = {
  85. dontShowAgainChecked: false,
  86. closePressed: false,
  87. shouldShow: false
  88. };
  89. this.isEdge = /Edg(e)?/.test(navigator.userAgent);
  90. this._onClosePressed = this._onClosePressed.bind(this);
  91. this._onInstallExtensionClick = this._onInstallExtensionClick.bind(this);
  92. this._shouldNotRender = this._shouldNotRender.bind(this);
  93. this._onDontShowAgainChange = this._onDontShowAgainChange.bind(this);
  94. this._onCloseKeyPress = this._onCloseKeyPress.bind(this);
  95. this._onInstallExtensionKeyPress = this._onInstallExtensionKeyPress.bind(this);
  96. }
  97. /**
  98. * Executed on component update.
  99. * Checks whether any chrome extension from the config is installed.
  100. *
  101. * @inheritdoc
  102. */
  103. async componentDidUpdate(prevProps) {
  104. if (!this._isSupportedEnvironment()) {
  105. return;
  106. }
  107. const { bannerCfg } = this.props;
  108. const prevBannerCfg = prevProps.bannerCfg;
  109. if (bannerCfg.url && !prevBannerCfg.url) {
  110. logger.info('Chrome extension URL found.');
  111. }
  112. if ((bannerCfg.chromeExtensionsInfo || []).length && !(prevBannerCfg.chromeExtensionsInfo || []).length) {
  113. logger.info('Chrome extension(s) info found.');
  114. }
  115. const hasExtensions = await checkChromeExtensionsInstalled(this.props.bannerCfg);
  116. if (
  117. hasExtensions
  118. && hasExtensions.length
  119. && hasExtensions.every(ext => !ext)
  120. && !this.state.shouldShow
  121. ) {
  122. this.setState({ shouldShow: true }); // eslint-disable-line
  123. }
  124. }
  125. /**
  126. * Checks whether the feature is enabled and whether the environment(browser/os)
  127. * supports it.
  128. *
  129. * @returns {boolean}
  130. */
  131. _isSupportedEnvironment() {
  132. return interfaceConfig.SHOW_CHROME_EXTENSION_BANNER
  133. && browser.isChrome()
  134. && !browser.isTwa()
  135. && !isMobileBrowser()
  136. && !this.props.isVpaas;
  137. }
  138. _onClosePressed: () => void;
  139. /**
  140. * Closes the banner for the current session.
  141. *
  142. * @returns {void}
  143. */
  144. _onClosePressed() {
  145. sendAnalytics(createChromeExtensionBannerEvent(false));
  146. this.setState({ closePressed: true });
  147. }
  148. _onCloseKeyPress: (Object) => void;
  149. /**
  150. * KeyPress handler for accessibility.
  151. *
  152. * @param {Object} e - The key event to handle.
  153. *
  154. * @returns {void}
  155. */
  156. _onCloseKeyPress(e) {
  157. if (e.key === ' ' || e.key === 'Enter') {
  158. e.preventDefault();
  159. this._onClosePressed();
  160. }
  161. }
  162. _onInstallExtensionClick: () => void;
  163. /**
  164. * Opens the chrome extension page.
  165. *
  166. * @returns {void}
  167. */
  168. _onInstallExtensionClick() {
  169. const { edgeUrl, url } = this.props.bannerCfg;
  170. sendAnalytics(createChromeExtensionBannerEvent(true));
  171. window.open(this.isEdge && edgeUrl ? edgeUrl : url);
  172. this.setState({ closePressed: true });
  173. }
  174. _onInstallExtensionKeyPress: (Object) => void;
  175. /**
  176. * KeyPress handler for accessibility.
  177. *
  178. * @param {Object} e - The key event to handle.
  179. *
  180. * @returns {void}
  181. */
  182. _onInstallExtensionKeyPress(e) {
  183. if (e.key === ' ' || e.key === 'Enter') {
  184. e.preventDefault();
  185. this._onClosePressed();
  186. }
  187. }
  188. _shouldNotRender: () => boolean;
  189. /**
  190. * Checks whether the banner should not be rendered.
  191. *
  192. * @returns {boolean} Whether to show the banner or not.
  193. */
  194. _shouldNotRender() {
  195. if (!this._isSupportedEnvironment()) {
  196. return true;
  197. }
  198. const dontShowAgain = jitsiLocalStorage.getItem(DONT_SHOW_AGAIN_CHECKED) === 'true';
  199. return !this.props.bannerCfg.url
  200. || dontShowAgain
  201. || this.state.closePressed
  202. || !this.state.shouldShow
  203. || this.props.iAmRecorder;
  204. }
  205. _onDontShowAgainChange: (object: Object) => void;
  206. /**
  207. * Handles the current `don't show again` checkbox state.
  208. *
  209. * @param {Object} event - Input change event.
  210. * @returns {void}
  211. */
  212. _onDontShowAgainChange(event) {
  213. this.setState({ dontShowAgainChecked: event.target.checked });
  214. }
  215. /**
  216. * Implements React's {@link PureComponent#render()}.
  217. *
  218. * @inheritdoc
  219. * @returns {ReactElement}
  220. */
  221. render() {
  222. if (this._shouldNotRender()) {
  223. if (this.state.dontShowAgainChecked) {
  224. jitsiLocalStorage.setItem(DONT_SHOW_AGAIN_CHECKED, 'true');
  225. }
  226. return null;
  227. }
  228. const { bannerCfg, t } = this.props;
  229. const mainClassNames = this.props.conference
  230. ? 'chrome-extension-banner chrome-extension-banner__pos_in_meeting'
  231. : 'chrome-extension-banner';
  232. return (
  233. <div className = { mainClassNames }>
  234. <div
  235. aria-aria-describedby = 'chrome-extension-banner__text-container'
  236. className = 'chrome-extension-banner__container'
  237. role = 'banner'>
  238. <div className = 'chrome-extension-banner__icon-container' />
  239. <div
  240. className = 'chrome-extension-banner__text-container'
  241. id = 'chrome-extension-banner__text-container'>
  242. { t('chromeExtensionBanner.installExtensionText') }
  243. </div>
  244. <div
  245. aria-label = { t('chromeExtensionBanner.close') }
  246. className = 'chrome-extension-banner__close-container'
  247. onClick = { this._onClosePressed }
  248. onKeyPress = { this._onCloseKeyPress }
  249. role = 'button'
  250. tabIndex = { 0 }>
  251. <Icon
  252. className = 'gray'
  253. size = { 12 }
  254. src = { IconCloseLarge } />
  255. </div>
  256. </div>
  257. <div
  258. className = 'chrome-extension-banner__button-container'>
  259. <div
  260. aria-labelledby = 'chrome-extension-banner__button-text'
  261. className = 'chrome-extension-banner__button-open-url'
  262. onClick = { this._onInstallExtensionClick }
  263. onKeyPress = { this._onInstallExtensionKeyPress }
  264. role = 'button'
  265. tabIndex = { 0 }>
  266. <div
  267. className = 'chrome-extension-banner__button-text'
  268. id = 'chrome-extension-banner__button-text'>
  269. { t(this.isEdge && bannerCfg.edgeUrl
  270. ? 'chromeExtensionBanner.buttonTextEdge'
  271. : 'chromeExtensionBanner.buttonText')
  272. }
  273. </div>
  274. </div>
  275. </div>
  276. <div className = 'chrome-extension-banner__checkbox-container'>
  277. <label
  278. className = 'chrome-extension-banner__checkbox-label'
  279. htmlFor = 'chrome-extension-banner__checkbox'
  280. id = 'chrome-extension-banner__checkbox-label'>
  281. <input
  282. aria-labelledby = 'chrome-extension-banner__checkbox-label'
  283. checked = { this.state.dontShowAgainChecked }
  284. id = 'chrome-extension-banner__checkbox'
  285. onChange = { this._onDontShowAgainChange }
  286. type = 'checkbox' />
  287. &nbsp;{ t('chromeExtensionBanner.dontShowAgain') }
  288. </label>
  289. </div>
  290. </div>
  291. );
  292. }
  293. }
  294. /**
  295. * Function that maps parts of Redux state tree into component props.
  296. *
  297. * @param {Object} state - Redux state.
  298. * @returns {Object}
  299. */
  300. const _mapStateToProps = state => {
  301. return {
  302. // Using emptyObject so that we don't change the reference every time when _mapStateToProps is called.
  303. bannerCfg: state['features/base/config'].chromeExtensionBanner || emptyObject,
  304. conference: getCurrentConference(state),
  305. iAmRecorder: state['features/base/config'].iAmRecorder,
  306. isVpaas: isVpaasMeeting(state)
  307. };
  308. };
  309. export default translate(connect(_mapStateToProps)(ChromeExtensionBanner));