您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FeedbackDialog.web.js 12KB

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