您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChromeExtensionBanner.web.js 8.4KB

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