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 8.9KB

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