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.

CopyMeetingUrl.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { getCurrentConferenceUrl } from '../../../connection';
  4. import { translate } from '../../../i18n';
  5. import { Icon, IconCopy, IconCheck } from '../../../icons';
  6. import { connect } from '../../../redux';
  7. import { copyText, getDecodedURI } from '../../../util';
  8. import logger from '../../logger';
  9. type Props = {
  10. /**
  11. * The meeting url.
  12. */
  13. url: string,
  14. /**
  15. * Used for translation.
  16. */
  17. t: Function,
  18. /**
  19. * Used to determine if invitation link should be automatically copied
  20. * after creating a meeting.
  21. */
  22. _enableAutomaticUrlCopy: boolean,
  23. };
  24. type State = {
  25. /**
  26. * If true it shows the 'copy link' message.
  27. */
  28. showCopyLink: boolean,
  29. /**
  30. * If true it shows the 'link copied' message.
  31. */
  32. showLinkCopied: boolean,
  33. };
  34. const COPY_TIMEOUT = 2000;
  35. /**
  36. * Component used to copy meeting url on prejoin page.
  37. */
  38. class CopyMeetingUrl extends Component<Props, State> {
  39. /**
  40. * Initializes a new {@code Prejoin} instance.
  41. *
  42. * @inheritdoc
  43. */
  44. constructor(props) {
  45. super(props);
  46. this.state = {
  47. showCopyLink: false,
  48. showLinkCopied: false
  49. };
  50. this._copyUrl = this._copyUrl.bind(this);
  51. this._hideCopyLink = this._hideCopyLink.bind(this);
  52. this._hideLinkCopied = this._hideLinkCopied.bind(this);
  53. this._showCopyLink = this._showCopyLink.bind(this);
  54. this._showLinkCopied = this._showLinkCopied.bind(this);
  55. this._copyUrlAutomatically = this._copyUrlAutomatically.bind(this);
  56. }
  57. _copyUrl: () => void;
  58. /**
  59. * Callback invoked to copy the url to clipboard.
  60. *
  61. * @returns {void}
  62. */
  63. _copyUrl() {
  64. const success = copyText(this.props.url);
  65. if (success) {
  66. this._showLinkCopied();
  67. window.setTimeout(this._hideLinkCopied, COPY_TIMEOUT);
  68. }
  69. }
  70. _hideLinkCopied: () => void;
  71. /**
  72. * Hides the 'Link copied' message.
  73. *
  74. * @private
  75. * @returns {void}
  76. */
  77. _hideLinkCopied() {
  78. this.setState({
  79. showLinkCopied: false
  80. });
  81. }
  82. _hideCopyLink: () => void;
  83. /**
  84. * Hides the 'Copy link' text.
  85. *
  86. * @private
  87. * @returns {void}
  88. */
  89. _hideCopyLink() {
  90. this.setState({
  91. showCopyLink: false,
  92. showLinkCopied: false
  93. });
  94. }
  95. _showCopyLink: () => void;
  96. /**
  97. * Shows the dark 'Copy link' text on hover.
  98. *
  99. * @private
  100. * @returns {void}
  101. */
  102. _showCopyLink() {
  103. this.setState({
  104. showCopyLink: true,
  105. showLinkCopied: false
  106. });
  107. }
  108. _showLinkCopied: () => void;
  109. /**
  110. * Shows the green 'Link copied' message.
  111. *
  112. * @private
  113. * @returns {void}
  114. */
  115. _showLinkCopied() {
  116. this.setState({
  117. showLinkCopied: true,
  118. showCopyLink: false
  119. });
  120. }
  121. _copyUrlAutomatically: () => void;
  122. /**
  123. * Attempts to automatically copy invitation URL.
  124. * Document has to be focused in order for this to work.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _copyUrlAutomatically() {
  130. navigator.clipboard.writeText(this.props.url)
  131. .then(() => {
  132. this._showLinkCopied();
  133. window.setTimeout(this._hideLinkCopied, COPY_TIMEOUT);
  134. })
  135. .catch(e => {
  136. logger.error(e);
  137. });
  138. }
  139. /**
  140. * Implements React's {@link Component#componentDidMount()}. Invoked
  141. * immediately before mounting occurs.
  142. *
  143. * @inheritdoc
  144. */
  145. componentDidMount() {
  146. const { _enableAutomaticUrlCopy } = this.props;
  147. if (_enableAutomaticUrlCopy) {
  148. setTimeout(this._copyUrlAutomatically, 2000);
  149. }
  150. }
  151. /**
  152. * Implements React's {@link Component#render()}.
  153. *
  154. * @inheritdoc
  155. * @returns {ReactElement}
  156. */
  157. render() {
  158. const { showCopyLink, showLinkCopied } = this.state;
  159. const { url, t } = this.props;
  160. const { _copyUrl, _showCopyLink, _hideCopyLink } = this;
  161. const src = showLinkCopied ? IconCheck : IconCopy;
  162. return (
  163. <div
  164. className = 'copy-meeting'
  165. onMouseEnter = { _showCopyLink }
  166. onMouseLeave = { _hideCopyLink }>
  167. <div
  168. className = { `url ${showLinkCopied ? 'done' : ''}` }
  169. onClick = { _copyUrl } >
  170. <div className = 'copy-meeting-text'>
  171. { !showCopyLink && !showLinkCopied && getDecodedURI(url) }
  172. { showCopyLink && t('prejoin.copyAndShare') }
  173. { showLinkCopied && t('prejoin.linkCopied') }
  174. </div>
  175. <Icon
  176. onClick = { _copyUrl }
  177. size = { 24 }
  178. src = { src } />
  179. </div>
  180. </div>
  181. );
  182. }
  183. }
  184. /**
  185. * Maps (parts of) the redux state to the React {@code Component} props.
  186. *
  187. * @param {Object} state - The redux state.
  188. * @returns {Object}
  189. */
  190. function mapStateToProps(state) {
  191. const { enableAutomaticUrlCopy } = state['features/base/config'];
  192. const { customizationReady } = state['features/dynamic-branding'];
  193. return {
  194. url: customizationReady ? getCurrentConferenceUrl(state) : '',
  195. _enableAutomaticUrlCopy: enableAutomaticUrlCopy || false
  196. };
  197. }
  198. export default connect(mapStateToProps)(translate(CopyMeetingUrl));