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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 {
  10. checkChromeExtensionsInstalled,
  11. isMobileBrowser
  12. } from '../../base/environment/utils';
  13. import { translate } from '../../base/i18n';
  14. import { Icon, IconClose } 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._onClosePressed = this._onClosePressed.bind(this);
  90. this._onInstallExtensionClick = this._onInstallExtensionClick.bind(this);
  91. this._shouldNotRender = this._shouldNotRender.bind(this);
  92. this._onDontShowAgainChange = this._onDontShowAgainChange.bind(this);
  93. this._onCloseKeyPress = this._onCloseKeyPress.bind(this);
  94. this._onInstallExtensionKeyPress = this._onInstallExtensionKeyPress.bind(this);
  95. }
  96. /**
  97. * Executed on component update.
  98. * Checks whether any chrome extension from the config is installed.
  99. *
  100. * @inheritdoc
  101. */
  102. async componentDidUpdate(prevProps) {
  103. if (!this._isSupportedEnvironment()) {
  104. return;
  105. }
  106. const { bannerCfg } = this.props;
  107. const prevBannerCfg = prevProps.bannerCfg;
  108. if (bannerCfg.url && !prevBannerCfg.url) {
  109. logger.info('Chrome extension URL found.');
  110. }
  111. if ((bannerCfg.chromeExtensionsInfo || []).length && !(prevBannerCfg.chromeExtensionsInfo || []).length) {
  112. logger.info('Chrome extension(s) info found.');
  113. }
  114. const hasExtensions = await checkChromeExtensionsInstalled(this.props.bannerCfg);
  115. if (
  116. hasExtensions
  117. && hasExtensions.length
  118. && hasExtensions.every(ext => !ext)
  119. && !this.state.shouldShow
  120. ) {
  121. this.setState({ shouldShow: true }); // eslint-disable-line
  122. }
  123. }
  124. /**
  125. * Checks whether the feature is enabled and whether the environment(browser/os)
  126. * supports it.
  127. *
  128. * @returns {boolean}
  129. */
  130. _isSupportedEnvironment() {
  131. return interfaceConfig.SHOW_CHROME_EXTENSION_BANNER
  132. && browser.isChrome()
  133. && !browser.isTwa()
  134. && !isMobileBrowser()
  135. && !this.props.isVpaas;
  136. }
  137. _onClosePressed: () => void;
  138. /**
  139. * Closes the banner for the current session.
  140. *
  141. * @returns {void}
  142. */
  143. _onClosePressed() {
  144. sendAnalytics(createChromeExtensionBannerEvent(false));
  145. this.setState({ closePressed: true });
  146. }
  147. _onCloseKeyPress: (Object) => void;
  148. /**
  149. * KeyPress handler for accessibility.
  150. *
  151. * @param {Object} e - The key event to handle.
  152. *
  153. * @returns {void}
  154. */
  155. _onCloseKeyPress(e) {
  156. if (e.key === ' ' || e.key === 'Enter') {
  157. e.preventDefault();
  158. this._onClosePressed();
  159. }
  160. }
  161. _onInstallExtensionClick: () => void;
  162. /**
  163. * Opens the chrome extension page.
  164. *
  165. * @returns {void}
  166. */
  167. _onInstallExtensionClick() {
  168. sendAnalytics(createChromeExtensionBannerEvent(true));
  169. window.open(this.props.bannerCfg.url);
  170. this.setState({ closePressed: true });
  171. }
  172. _onInstallExtensionKeyPress: (Object) => void;
  173. /**
  174. * KeyPress handler for accessibility.
  175. *
  176. * @param {Object} e - The key event to handle.
  177. *
  178. * @returns {void}
  179. */
  180. _onInstallExtensionKeyPress(e) {
  181. if (e.key === ' ' || e.key === 'Enter') {
  182. e.preventDefault();
  183. this._onClosePressed();
  184. }
  185. }
  186. _shouldNotRender: () => boolean;
  187. /**
  188. * Checks whether the banner should not be rendered.
  189. *
  190. * @returns {boolean} Whether to show the banner or not.
  191. */
  192. _shouldNotRender() {
  193. if (!this._isSupportedEnvironment()) {
  194. return true;
  195. }
  196. const dontShowAgain = jitsiLocalStorage.getItem(DONT_SHOW_AGAIN_CHECKED) === 'true';
  197. return !this.props.bannerCfg.url
  198. || dontShowAgain
  199. || this.state.closePressed
  200. || !this.state.shouldShow
  201. || this.props.iAmRecorder;
  202. }
  203. _onDontShowAgainChange: (object: Object) => void;
  204. /**
  205. * Handles the current `don't show again` checkbox state.
  206. *
  207. * @param {Object} event - Input change event.
  208. * @returns {void}
  209. */
  210. _onDontShowAgainChange(event) {
  211. this.setState({ dontShowAgainChecked: event.target.checked });
  212. }
  213. /**
  214. * Implements React's {@link PureComponent#render()}.
  215. *
  216. * @inheritdoc
  217. * @returns {ReactElement}
  218. */
  219. render() {
  220. if (this._shouldNotRender()) {
  221. if (this.state.dontShowAgainChecked) {
  222. jitsiLocalStorage.setItem(DONT_SHOW_AGAIN_CHECKED, 'true');
  223. }
  224. return null;
  225. }
  226. const { t } = this.props;
  227. const mainClassNames = this.props.conference
  228. ? 'chrome-extension-banner chrome-extension-banner__pos_in_meeting'
  229. : 'chrome-extension-banner';
  230. return (
  231. <div className = { mainClassNames }>
  232. <div
  233. aria-aria-describedby = 'chrome-extension-banner__text-container'
  234. className = 'chrome-extension-banner__container'
  235. role = 'banner'>
  236. <div className = 'chrome-extension-banner__icon-container' />
  237. <div
  238. className = 'chrome-extension-banner__text-container'
  239. id = 'chrome-extension-banner__text-container'>
  240. { t('chromeExtensionBanner.installExtensionText') }
  241. </div>
  242. <div
  243. aria-label = { t('chromeExtensionBanner.close') }
  244. className = 'chrome-extension-banner__close-container'
  245. onClick = { this._onClosePressed }
  246. onKeyPress = { this._onCloseKeyPress }
  247. role = 'button'
  248. tabIndex = { 0 }>
  249. <Icon
  250. className = 'gray'
  251. size = { 12 }
  252. src = { IconClose } />
  253. </div>
  254. </div>
  255. <div
  256. className = 'chrome-extension-banner__button-container'>
  257. <div
  258. aria-labelledby = 'chrome-extension-banner__button-text'
  259. className = 'chrome-extension-banner__button-open-url'
  260. onClick = { this._onInstallExtensionClick }
  261. onKeyPress = { this._onInstallExtensionKeyPress }
  262. role = 'button'
  263. tabIndex = { 0 }>
  264. <div
  265. className = 'chrome-extension-banner__button-text'
  266. id = 'chrome-extension-banner__button-text'>
  267. { t('chromeExtensionBanner.buttonText') }
  268. </div>
  269. </div>
  270. </div>
  271. <div className = 'chrome-extension-banner__checkbox-container'>
  272. <label
  273. className = 'chrome-extension-banner__checkbox-label'
  274. htmlFor = 'chrome-extension-banner__checkbox'
  275. id = 'chrome-extension-banner__checkbox-label'>
  276. <input
  277. aria-labelledby = 'chrome-extension-banner__checkbox-label'
  278. checked = { this.state.dontShowAgainChecked }
  279. id = 'chrome-extension-banner__checkbox'
  280. onChange = { this._onDontShowAgainChange }
  281. type = 'checkbox' />
  282. &nbsp;{ t('chromeExtensionBanner.dontShowAgain') }
  283. </label>
  284. </div>
  285. </div>
  286. );
  287. }
  288. }
  289. /**
  290. * Function that maps parts of Redux state tree into component props.
  291. *
  292. * @param {Object} state - Redux state.
  293. * @returns {Object}
  294. */
  295. const _mapStateToProps = state => {
  296. return {
  297. // Using emptyObject so that we don't change the reference every time when _mapStateToProps is called.
  298. bannerCfg: state['features/base/config'].chromeExtensionBanner || emptyObject,
  299. conference: getCurrentConference(state),
  300. iAmRecorder: state['features/base/config'].iAmRecorder,
  301. isVpaas: isVpaasMeeting(state)
  302. };
  303. };
  304. export default translate(connect(_mapStateToProps)(ChromeExtensionBanner));