Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ChromeExtensionBanner.web.tsx 11KB

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