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

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