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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from '../../../base/redux';
  4. import { translate } from '../../../base/i18n';
  5. import { getCurrentConferenceUrl } from '../../../base/connection';
  6. import { Icon, IconCopy, IconCheck } from '../../../base/icons';
  7. import logger from '../../logger';
  8. type Props = {
  9. /**
  10. * The meeting url.
  11. */
  12. url: string,
  13. /**
  14. * Used for translation.
  15. */
  16. t: Function
  17. };
  18. type State = {
  19. /**
  20. * If true it shows the 'copy link' message.
  21. */
  22. showCopyLink: boolean,
  23. /**
  24. * If true it shows the 'link copied' message.
  25. */
  26. showLinkCopied: boolean,
  27. };
  28. const COPY_TIMEOUT = 2000;
  29. /**
  30. * Component used to copy meeting url on prejoin page.
  31. */
  32. class CopyMeetingUrl extends Component<Props, State> {
  33. textarea: Object;
  34. /**
  35. * Initializes a new {@code Prejoin} instance.
  36. *
  37. * @inheritdoc
  38. */
  39. constructor(props) {
  40. super(props);
  41. this.textarea = React.createRef();
  42. this.state = {
  43. showCopyLink: false,
  44. showLinkCopied: false
  45. };
  46. this._copyUrl = this._copyUrl.bind(this);
  47. this._hideCopyLink = this._hideCopyLink.bind(this);
  48. this._hideLinkCopied = this._hideLinkCopied.bind(this);
  49. this._showCopyLink = this._showCopyLink.bind(this);
  50. this._showLinkCopied = this._showLinkCopied.bind(this);
  51. }
  52. _copyUrl: () => void;
  53. /**
  54. * Callback invoked to copy the url to clipboard.
  55. *
  56. * @returns {void}
  57. */
  58. _copyUrl() {
  59. const textarea = this.textarea.current;
  60. try {
  61. textarea.select();
  62. document.execCommand('copy');
  63. textarea.blur();
  64. this._showLinkCopied();
  65. window.setTimeout(this._hideLinkCopied, COPY_TIMEOUT);
  66. } catch (err) {
  67. logger.error('error when copying the meeting url');
  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. });
  93. }
  94. _showCopyLink: () => void;
  95. /**
  96. * Shows the dark 'Copy link' text on hover.
  97. *
  98. * @private
  99. * @returns {void}
  100. */
  101. _showCopyLink() {
  102. this.setState({
  103. showCopyLink: true
  104. });
  105. }
  106. _showLinkCopied: () => void;
  107. /**
  108. * Shows the green 'Link copied' message.
  109. *
  110. * @private
  111. * @returns {void}
  112. */
  113. _showLinkCopied() {
  114. this.setState({
  115. showLinkCopied: true,
  116. showCopyLink: false
  117. });
  118. }
  119. /**
  120. * Implements React's {@link Component#render()}.
  121. *
  122. * @inheritdoc
  123. * @returns {ReactElement}
  124. */
  125. render() {
  126. const { showCopyLink, showLinkCopied } = this.state;
  127. const { url, t } = this.props;
  128. const { _copyUrl, _showCopyLink, _hideCopyLink } = this;
  129. const src = showLinkCopied ? IconCheck : IconCopy;
  130. const iconCls = showCopyLink || showCopyLink ? 'prejoin-copy-icon--white' : 'prejoin-copy-icon--light';
  131. return (
  132. <div
  133. className = 'prejoin-copy-meeting'
  134. onMouseEnter = { _showCopyLink }
  135. onMouseLeave = { _hideCopyLink }>
  136. <div className = 'prejoin-copy-url'>{url}</div>
  137. {showCopyLink && <div
  138. className = 'prejoin-copy-badge prejoin-copy-badge--hover'
  139. onClick = { _copyUrl }>
  140. {t('prejoin.copyAndShare')}
  141. </div>}
  142. {showLinkCopied && <div
  143. className = 'prejoin-copy-badge prejoin-copy-badge--done'>
  144. {t('prejoin.linkCopied')}
  145. </div>}
  146. <Icon
  147. className = { `prejoin-copy-icon ${iconCls}` }
  148. onClick = { _copyUrl }
  149. size = { 24 }
  150. src = { src } />
  151. <textarea
  152. className = 'prejoin-copy-textarea'
  153. readOnly = { true }
  154. ref = { this.textarea }
  155. tabIndex = '-1'
  156. value = { url } />
  157. </div>);
  158. }
  159. }
  160. /**
  161. * Maps (parts of) the redux state to the React {@code Component} props.
  162. *
  163. * @param {Object} state - The redux state.
  164. * @returns {Object}
  165. */
  166. function mapStateToProps(state) {
  167. return {
  168. url: getCurrentConferenceUrl(state)
  169. };
  170. }
  171. export default connect(mapStateToProps)(translate(CopyMeetingUrl));