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

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