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.5KB

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