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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* eslint-disable lines-around-comment */
  2. import { Theme } from '@mui/material';
  3. import { withStyles } from '@mui/styles';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import type { Dispatch } from 'redux';
  8. import { IState } from '../../../app/types';
  9. import { translate } from '../../../base/i18n/functions';
  10. import {
  11. getParticipantById,
  12. getParticipantDisplayName
  13. } from '../../../base/participants/functions';
  14. // @ts-ignore
  15. import { updateSettings } from '../../../base/settings';
  16. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  17. // @ts-ignore
  18. import { Tooltip } from '../../../base/tooltip';
  19. // @ts-ignore
  20. import { getIndicatorsTooltipPosition } from '../../../filmstrip/functions.web';
  21. import { appendSuffix } from '../../functions';
  22. /**
  23. * The type of the React {@code Component} props of {@link DisplayName}.
  24. */
  25. interface Props extends WithTranslation {
  26. /**
  27. * The participant's current display name which should be shown when in
  28. * edit mode. Can be different from what is shown when not editing.
  29. */
  30. _configuredDisplayName: string;
  31. /**
  32. * The participant's current display name which should be shown.
  33. */
  34. _nameToDisplay: string;
  35. /**
  36. * Whether or not the display name should be editable on click.
  37. */
  38. allowEditing: boolean;
  39. /**
  40. * An object containing the CSS classes.
  41. */
  42. classes: any;
  43. /**
  44. * Invoked to update the participant's display name.
  45. */
  46. dispatch: Dispatch<any>;
  47. /**
  48. * A string to append to the displayName, if provided.
  49. */
  50. displayNameSuffix: string;
  51. /**
  52. * The ID attribute to add to the component. Useful for global querying for
  53. * the component by legacy components and torture tests.
  54. */
  55. elementID: string;
  56. /**
  57. * The ID of the participant whose name is being displayed.
  58. */
  59. participantID: string;
  60. /**
  61. * The type of thumbnail.
  62. */
  63. thumbnailType: string;
  64. }
  65. /**
  66. * The type of the React {@code Component} state of {@link DisplayName}.
  67. */
  68. type State = {
  69. /**
  70. * The current value of the display name in the edit field.
  71. */
  72. editDisplayNameValue: string;
  73. /**
  74. * Whether or not the component should be displaying an editable input.
  75. */
  76. isEditing: boolean;
  77. };
  78. const styles = (theme: Theme) => {
  79. return {
  80. displayName: {
  81. ...withPixelLineHeight(theme.typography.labelBold),
  82. color: theme.palette.text01,
  83. overflow: 'hidden',
  84. textOverflow: 'ellipsis',
  85. whiteSpace: 'nowrap'
  86. },
  87. editDisplayName: {
  88. outline: 'none',
  89. border: 'none',
  90. background: 'none',
  91. boxShadow: 'none',
  92. padding: 0,
  93. ...withPixelLineHeight(theme.typography.labelBold),
  94. color: theme.palette.text01
  95. }
  96. };
  97. };
  98. /**
  99. * React {@code Component} for displaying and editing a participant's name.
  100. *
  101. * @augments Component
  102. */
  103. class DisplayName extends Component<Props, State> {
  104. _nameInput?: HTMLInputElement;
  105. static defaultProps = {
  106. _configuredDisplayName: ''
  107. };
  108. /**
  109. * Initializes a new {@code DisplayName} instance.
  110. *
  111. * @param {Object} props - The read-only properties with which the new
  112. * instance is to be initialized.
  113. */
  114. constructor(props: Props) {
  115. super(props);
  116. this.state = {
  117. editDisplayNameValue: '',
  118. isEditing: false
  119. };
  120. /**
  121. * The internal reference to the HTML element backing the React
  122. * {@code Component} input with id {@code editDisplayName}. It is
  123. * necessary for automatically selecting the display name input field
  124. * when starting to edit the display name.
  125. *
  126. * @private
  127. * @type {HTMLInputElement}
  128. */
  129. this._nameInput = undefined;
  130. // Bind event handlers so they are only bound once for every instance.
  131. this._onChange = this._onChange.bind(this);
  132. this._onKeyDown = this._onKeyDown.bind(this);
  133. this._onStartEditing = this._onStartEditing.bind(this);
  134. this._onSubmit = this._onSubmit.bind(this);
  135. this._setNameInputRef = this._setNameInputRef.bind(this);
  136. }
  137. /**
  138. * Automatically selects the input field's value after starting to edit the
  139. * display name.
  140. *
  141. * @inheritdoc
  142. * @returns {void}
  143. */
  144. componentDidUpdate(previousProps: Props, previousState: State) {
  145. if (!previousState.isEditing
  146. && this.state.isEditing
  147. && this._nameInput) {
  148. this._nameInput.select();
  149. }
  150. }
  151. /**
  152. * Implements React's {@link Component#render()}.
  153. *
  154. * @inheritdoc
  155. * @returns {ReactElement}
  156. */
  157. render() {
  158. const {
  159. _nameToDisplay,
  160. allowEditing,
  161. displayNameSuffix,
  162. classes,
  163. elementID,
  164. t,
  165. thumbnailType
  166. } = this.props;
  167. if (allowEditing && this.state.isEditing) {
  168. return (
  169. <input
  170. autoFocus = { true }
  171. className = { classes.editDisplayName }
  172. id = 'editDisplayName'
  173. onBlur = { this._onSubmit }
  174. onChange = { this._onChange }
  175. onClick = { this._onClick }
  176. onKeyDown = { this._onKeyDown }
  177. placeholder = { t('defaultNickname') }
  178. ref = { this._setNameInputRef }
  179. spellCheck = { 'false' }
  180. type = 'text'
  181. value = { this.state.editDisplayNameValue } />
  182. );
  183. }
  184. return (
  185. <Tooltip
  186. content = { appendSuffix(_nameToDisplay, displayNameSuffix) }
  187. position = { getIndicatorsTooltipPosition(thumbnailType) }>
  188. <span
  189. className = { `displayname ${classes.displayName}` }
  190. id = { elementID }
  191. onClick = { this._onStartEditing }>
  192. { appendSuffix(_nameToDisplay, displayNameSuffix) }
  193. </span>
  194. </Tooltip>
  195. );
  196. }
  197. /**
  198. * Stop click event propagation.
  199. *
  200. * @param {MouseEvent} e - The click event.
  201. * @private
  202. * @returns {void}
  203. */
  204. _onClick(e: React.MouseEvent) {
  205. e.stopPropagation();
  206. }
  207. /**
  208. * Updates the internal state of the display name entered into the edit
  209. * field.
  210. *
  211. * @param {Object} event - DOM Event for value change.
  212. * @private
  213. * @returns {void}
  214. */
  215. _onChange(event: React.ChangeEvent<HTMLInputElement>) {
  216. this.setState({
  217. editDisplayNameValue: event.target.value
  218. });
  219. }
  220. /**
  221. * Submits the edited display name update if the enter key is pressed.
  222. *
  223. * @param {Event} event - Key down event object.
  224. * @private
  225. * @returns {void}
  226. */
  227. _onKeyDown(event: React.KeyboardEvent) {
  228. if (event.key === 'Enter') {
  229. this._onSubmit();
  230. }
  231. }
  232. /**
  233. * Updates the component to display an editable input field and sets the
  234. * initial value to the current display name.
  235. *
  236. * @param {MouseEvent} e - The click event.
  237. * @private
  238. * @returns {void}
  239. */
  240. _onStartEditing(e: React.MouseEvent) {
  241. if (this.props.allowEditing) {
  242. e.stopPropagation();
  243. this.setState({
  244. isEditing: true,
  245. editDisplayNameValue: this.props._configuredDisplayName
  246. });
  247. }
  248. }
  249. /**
  250. * Dispatches an action to update the display name if any change has
  251. * occurred after editing. Clears any temporary state used to keep track
  252. * of pending display name changes and exits editing mode.
  253. *
  254. * @param {Event} event - Key down event object.
  255. * @private
  256. * @returns {void}
  257. */
  258. _onSubmit() {
  259. const { editDisplayNameValue } = this.state;
  260. const { dispatch } = this.props;
  261. // Store display name in settings
  262. dispatch(updateSettings({
  263. displayName: editDisplayNameValue
  264. }));
  265. this.setState({
  266. isEditing: false,
  267. editDisplayNameValue: ''
  268. });
  269. this._nameInput = undefined;
  270. }
  271. /**
  272. * Sets the internal reference to the HTML element backing the React
  273. * {@code Component} input with id {@code editDisplayName}.
  274. *
  275. * @param {HTMLInputElement} element - The DOM/HTML element for this
  276. * {@code Component}'s input.
  277. * @private
  278. * @returns {void}
  279. */
  280. _setNameInputRef(element: HTMLInputElement) {
  281. this._nameInput = element;
  282. }
  283. }
  284. /**
  285. * Maps (parts of) the redux state to the props of this component.
  286. *
  287. * @param {Object} state - The redux store/state.
  288. * @param {Props} ownProps - The own props of the component.
  289. * @private
  290. * @returns {{
  291. * _configuredDisplayName: string,
  292. * _nameToDisplay: string
  293. * }}
  294. */
  295. function _mapStateToProps(state: IState, ownProps: Partial<Props>) {
  296. const { participantID } = ownProps;
  297. const participant = getParticipantById(state, participantID ?? '');
  298. return {
  299. _configuredDisplayName: participant?.name,
  300. _nameToDisplay: getParticipantDisplayName(state, participantID ?? '')
  301. };
  302. }
  303. export default translate(connect(_mapStateToProps)(withStyles(styles)(DisplayName)));