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.

FeedbackDialog.web.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import { Theme } from '@mui/material';
  2. import { ClassNameMap, withStyles } from '@mui/styles';
  3. import React, { Component } from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { connect } from 'react-redux';
  6. import { createFeedbackOpenEvent } from '../../analytics/AnalyticsEvents';
  7. import { sendAnalytics } from '../../analytics/functions';
  8. import { IReduxState, IStore } from '../../app/types';
  9. import { IJitsiConference } from '../../base/conference/reducer';
  10. import { isMobileBrowser } from '../../base/environment/utils';
  11. import { translate } from '../../base/i18n/functions';
  12. import Icon from '../../base/icons/components/Icon';
  13. import { IconFavorite, IconFavoriteSolid } from '../../base/icons/svg';
  14. import { withPixelLineHeight } from '../../base/styles/functions.web';
  15. import Dialog from '../../base/ui/components/web/Dialog';
  16. import Input from '../../base/ui/components/web/Input';
  17. import { cancelFeedback, submitFeedback } from '../actions';
  18. const styles = (theme: Theme) => {
  19. return {
  20. dialog: {
  21. marginBottom: theme.spacing(1)
  22. },
  23. rating: {
  24. display: 'flex',
  25. flexDirection: 'column' as const,
  26. alignItems: 'center',
  27. justifyContent: 'center',
  28. marginTop: theme.spacing(4),
  29. marginBottom: theme.spacing(3)
  30. },
  31. ratingLabel: {
  32. ...withPixelLineHeight(theme.typography.bodyShortBold),
  33. color: theme.palette.text01,
  34. marginBottom: theme.spacing(2),
  35. height: '20px'
  36. },
  37. stars: {
  38. display: 'flex'
  39. },
  40. starBtn: {
  41. display: 'inline-block',
  42. cursor: 'pointer',
  43. marginRight: theme.spacing(3),
  44. '&:last-of-type': {
  45. marginRight: 0
  46. },
  47. '&.active svg': {
  48. fill: theme.palette.success01
  49. },
  50. '&:focus': {
  51. outline: `1px solid ${theme.palette.action01}`,
  52. borderRadius: '4px'
  53. }
  54. },
  55. details: {
  56. '& textarea': {
  57. minHeight: '122px'
  58. }
  59. }
  60. };
  61. };
  62. const scoreAnimationClass
  63. = interfaceConfig.ENABLE_FEEDBACK_ANIMATION ? 'shake-rotate' : '';
  64. /**
  65. * The scores to display for selecting. The score is the index in the array and
  66. * the value of the index is a translation key used for display in the dialog.
  67. */
  68. const SCORES = [
  69. 'feedback.veryBad',
  70. 'feedback.bad',
  71. 'feedback.average',
  72. 'feedback.good',
  73. 'feedback.veryGood'
  74. ];
  75. const ICON_SIZE = 32;
  76. type Scrollable = {
  77. scroll: Function;
  78. };
  79. /**
  80. * The type of the React {@code Component} props of {@link FeedbackDialog}.
  81. */
  82. interface IProps extends WithTranslation {
  83. /**
  84. * The cached feedback message, if any, that was set when closing a previous
  85. * instance of {@code FeedbackDialog}.
  86. */
  87. _message: string;
  88. /**
  89. * The cached feedback score, if any, that was set when closing a previous
  90. * instance of {@code FeedbackDialog}.
  91. */
  92. _score: number;
  93. /**
  94. * An object containing the CSS classes.
  95. */
  96. classes: ClassNameMap<string>;
  97. /**
  98. * The JitsiConference that is being rated. The conference is passed in
  99. * because feedback can occur after a conference has been left, so
  100. * references to it may no longer exist in redux.
  101. */
  102. conference: IJitsiConference;
  103. /**
  104. * Invoked to signal feedback submission or canceling.
  105. */
  106. dispatch: IStore['dispatch'];
  107. /**
  108. * Callback invoked when {@code FeedbackDialog} is unmounted.
  109. */
  110. onClose: Function;
  111. }
  112. /**
  113. * The type of the React {@code Component} state of {@link FeedbackDialog}.
  114. */
  115. type State = {
  116. /**
  117. * The currently entered feedback message.
  118. */
  119. message: string;
  120. /**
  121. * The score selection index which is currently being hovered. The value -1
  122. * is used as a sentinel value to match store behavior of using -1 for no
  123. * score having been selected.
  124. */
  125. mousedOverScore: number;
  126. /**
  127. * The currently selected score selection index. The score will not be 0
  128. * indexed so subtract one to map with SCORES.
  129. */
  130. score: number;
  131. };
  132. /**
  133. * A React {@code Component} for displaying a dialog to rate the current
  134. * conference quality, write a message describing the experience, and submit
  135. * the feedback.
  136. *
  137. * @augments Component
  138. */
  139. class FeedbackDialog extends Component<IProps, State> {
  140. /**
  141. * An array of objects with click handlers for each of the scores listed in
  142. * the constant SCORES. This pattern is used for binding event handlers only
  143. * once for each score selection icon.
  144. */
  145. _scoreClickConfigurations: Array<{
  146. _onClick: (e: React.MouseEvent) => void;
  147. _onKeyDown: (e: React.KeyboardEvent) => void;
  148. _onMouseOver: (e: React.MouseEvent) => void;
  149. }>;
  150. _onScrollTop: (node: Scrollable | null) => void;
  151. /**
  152. * Initializes a new {@code FeedbackDialog} instance.
  153. *
  154. * @param {Object} props - The read-only React {@code Component} props with
  155. * which the new instance is to be initialized.
  156. */
  157. constructor(props: IProps) {
  158. super(props);
  159. const { _message, _score } = this.props;
  160. this.state = {
  161. /**
  162. * The currently entered feedback message.
  163. *
  164. * @type {string}
  165. */
  166. message: _message,
  167. /**
  168. * The score selection index which is currently being hovered. The
  169. * value -1 is used as a sentinel value to match store behavior of
  170. * using -1 for no score having been selected.
  171. *
  172. * @type {number}
  173. */
  174. mousedOverScore: -1,
  175. /**
  176. * The currently selected score selection index. The score will not
  177. * be 0 indexed so subtract one to map with SCORES.
  178. *
  179. * @type {number}
  180. */
  181. score: _score > -1 ? _score - 1 : _score
  182. };
  183. this._scoreClickConfigurations = SCORES.map((textKey, index) => {
  184. return {
  185. _onClick: () => this._onScoreSelect(index),
  186. _onKeyDown: (e: React.KeyboardEvent) => {
  187. if (e.key === ' ' || e.key === 'Enter') {
  188. e.stopPropagation();
  189. e.preventDefault();
  190. this._onScoreSelect(index);
  191. }
  192. },
  193. _onMouseOver: () => this._onScoreMouseOver(index)
  194. };
  195. });
  196. // Bind event handlers so they are only bound once for every instance.
  197. this._onCancel = this._onCancel.bind(this);
  198. this._onMessageChange = this._onMessageChange.bind(this);
  199. this._onScoreContainerMouseLeave
  200. = this._onScoreContainerMouseLeave.bind(this);
  201. this._onSubmit = this._onSubmit.bind(this);
  202. // On some mobile browsers opening Feedback dialog scrolls down the whole content because of the keyboard.
  203. // By scrolling to the top we prevent hiding the feedback stars so the user knows those exist.
  204. this._onScrollTop = (node: Scrollable | null) => {
  205. node?.scroll?.(0, 0);
  206. };
  207. }
  208. /**
  209. * Emits an analytics event to notify feedback has been opened.
  210. *
  211. * @inheritdoc
  212. */
  213. componentDidMount() {
  214. sendAnalytics(createFeedbackOpenEvent());
  215. if (typeof APP !== 'undefined') {
  216. APP.API.notifyFeedbackPromptDisplayed();
  217. }
  218. }
  219. /**
  220. * Invokes the onClose callback, if defined, to notify of the close event.
  221. *
  222. * @inheritdoc
  223. */
  224. componentWillUnmount() {
  225. if (this.props.onClose) {
  226. this.props.onClose();
  227. }
  228. }
  229. /**
  230. * Implements React's {@link Component#render()}.
  231. *
  232. * @inheritdoc
  233. * @returns {ReactElement}
  234. */
  235. render() {
  236. const { message, mousedOverScore, score } = this.state;
  237. const scoreToDisplayAsSelected
  238. = mousedOverScore > -1 ? mousedOverScore : score;
  239. const { classes, t } = this.props;
  240. const scoreIcons = this._scoreClickConfigurations.map(
  241. (config, index) => {
  242. const isFilled = index <= scoreToDisplayAsSelected;
  243. const activeClass = isFilled ? 'active' : '';
  244. const className
  245. = `${classes.starBtn} ${scoreAnimationClass} ${activeClass}`;
  246. return (
  247. <span
  248. aria-label = { t(SCORES[index]) }
  249. className = { className }
  250. key = { index }
  251. onClick = { config._onClick }
  252. onKeyDown = { config._onKeyDown }
  253. role = 'button'
  254. tabIndex = { 0 }
  255. { ...(isMobileBrowser() ? {} : {
  256. onMouseOver: config._onMouseOver
  257. }) }>
  258. { isFilled
  259. ? <Icon
  260. size = { ICON_SIZE }
  261. src = { IconFavoriteSolid } />
  262. : <Icon
  263. size = { ICON_SIZE }
  264. src = { IconFavorite } /> }
  265. </span>
  266. );
  267. });
  268. return (
  269. <Dialog
  270. ok = {{
  271. translationKey: 'dialog.Submit'
  272. }}
  273. onCancel = { this._onCancel }
  274. onSubmit = { this._onSubmit }
  275. size = 'large'
  276. titleKey = 'feedback.rateExperience'>
  277. <div className = { classes.dialog }>
  278. <div className = { classes.rating }>
  279. <div
  280. aria-label = { this.props.t('feedback.star') }
  281. className = { classes.ratingLabel } >
  282. <p id = 'starLabel'>
  283. { t(SCORES[scoreToDisplayAsSelected]) }
  284. </p>
  285. </div>
  286. <div
  287. className = { classes.stars }
  288. onMouseLeave = { this._onScoreContainerMouseLeave }>
  289. { scoreIcons }
  290. </div>
  291. </div>
  292. <div className = { classes.details }>
  293. <Input
  294. autoFocus = { true }
  295. id = 'feedbackTextArea'
  296. label = { t('feedback.detailsLabel') }
  297. onChange = { this._onMessageChange }
  298. textarea = { true }
  299. value = { message } />
  300. </div>
  301. </div>
  302. </Dialog>
  303. );
  304. }
  305. /**
  306. * Dispatches an action notifying feedback was not submitted. The submitted
  307. * score will have one added as the rest of the app does not expect 0
  308. * indexing.
  309. *
  310. * @private
  311. * @returns {boolean} Returns true to close the dialog.
  312. */
  313. _onCancel() {
  314. const { message, score } = this.state;
  315. const scoreToSubmit = score > -1 ? score + 1 : score;
  316. this.props.dispatch(cancelFeedback(scoreToSubmit, message));
  317. return true;
  318. }
  319. /**
  320. * Updates the known entered feedback message.
  321. *
  322. * @param {string} newValue - The new value from updating the textfield for the
  323. * feedback message.
  324. * @private
  325. * @returns {void}
  326. */
  327. _onMessageChange(newValue: string) {
  328. this.setState({ message: newValue });
  329. }
  330. /**
  331. * Updates the currently selected score.
  332. *
  333. * @param {number} score - The index of the selected score in SCORES.
  334. * @private
  335. * @returns {void}
  336. */
  337. _onScoreSelect(score: number) {
  338. this.setState({ score });
  339. }
  340. /**
  341. * Sets the currently hovered score to null to indicate no hover is
  342. * occurring.
  343. *
  344. * @private
  345. * @returns {void}
  346. */
  347. _onScoreContainerMouseLeave() {
  348. this.setState({ mousedOverScore: -1 });
  349. }
  350. /**
  351. * Updates the known state of the score icon currently behind hovered over.
  352. *
  353. * @param {number} mousedOverScore - The index of the SCORES value currently
  354. * being moused over.
  355. * @private
  356. * @returns {void}
  357. */
  358. _onScoreMouseOver(mousedOverScore: number) {
  359. this.setState({ mousedOverScore });
  360. }
  361. /**
  362. * Dispatches the entered feedback for submission. The submitted score will
  363. * have one added as the rest of the app does not expect 0 indexing.
  364. *
  365. * @private
  366. * @returns {boolean} Returns true to close the dialog.
  367. */
  368. _onSubmit() {
  369. const { conference, dispatch } = this.props;
  370. const { message, score } = this.state;
  371. const scoreToSubmit = score > -1 ? score + 1 : score;
  372. dispatch(submitFeedback(scoreToSubmit, message, conference));
  373. return true;
  374. }
  375. }
  376. /**
  377. * Maps (parts of) the Redux state to the associated {@code FeedbackDialog}'s
  378. * props.
  379. *
  380. * @param {Object} state - The Redux state.
  381. * @private
  382. * @returns {{
  383. * }}
  384. */
  385. function _mapStateToProps(state: IReduxState) {
  386. const { message, score } = state['features/feedback'];
  387. return {
  388. /**
  389. * The cached feedback message, if any, that was set when closing a
  390. * previous instance of {@code FeedbackDialog}.
  391. *
  392. * @type {string}
  393. */
  394. _message: message,
  395. /**
  396. * The currently selected score selection index.
  397. *
  398. * @type {number}
  399. */
  400. _score: score
  401. };
  402. }
  403. export default withStyles(styles)(translate(connect(_mapStateToProps)(FeedbackDialog)));