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

TileViewButton.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import {
  4. createToolbarEvent,
  5. sendAnalytics
  6. } from '../../analytics';
  7. import { TILE_VIEW_ENABLED, getFeatureFlag } from '../../base/flags';
  8. import { translate } from '../../base/i18n';
  9. import { IconTileView } from '../../base/icons';
  10. import { connect } from '../../base/redux';
  11. import { AbstractButton, type AbstractButtonProps } from '../../base/toolbox/components';
  12. import { setTileView } from '../actions';
  13. import { shouldDisplayTileView } from '../functions';
  14. import logger from '../logger';
  15. /**
  16. * The type of the React {@code Component} props of {@link TileViewButton}.
  17. */
  18. type Props = AbstractButtonProps & {
  19. /**
  20. * Whether or not tile view layout has been enabled as the user preference.
  21. */
  22. _tileViewEnabled: boolean,
  23. /**
  24. * Used to dispatch actions from the buttons.
  25. */
  26. dispatch: Dispatch<any>
  27. };
  28. /**
  29. * Component that renders a toolbar button for toggling the tile layout view.
  30. *
  31. * @extends AbstractButton
  32. */
  33. class TileViewButton<P: Props> extends AbstractButton<P, *> {
  34. accessibilityLabel = 'toolbar.accessibilityLabel.tileView';
  35. icon = IconTileView;
  36. label = 'toolbar.enterTileView';
  37. toggledLabel = 'toolbar.exitTileView';
  38. tooltip = 'toolbar.tileViewToggle';
  39. /**
  40. * Handles clicking / pressing the button.
  41. *
  42. * @override
  43. * @protected
  44. * @returns {void}
  45. */
  46. _handleClick() {
  47. const { _tileViewEnabled, dispatch, handleClick } = this.props;
  48. if (handleClick) {
  49. handleClick();
  50. return;
  51. }
  52. const value = !_tileViewEnabled;
  53. sendAnalytics(createToolbarEvent(
  54. 'tileview.button',
  55. {
  56. 'is_enabled': value
  57. }));
  58. logger.debug(`Tile view ${value ? 'enable' : 'disable'}`);
  59. dispatch(setTileView(value));
  60. }
  61. /**
  62. * Indicates whether this button is in toggled state or not.
  63. *
  64. * @override
  65. * @protected
  66. * @returns {boolean}
  67. */
  68. _isToggled() {
  69. return this.props._tileViewEnabled;
  70. }
  71. }
  72. /**
  73. * Maps (parts of) the redux state to the associated props for the
  74. * {@code TileViewButton} component.
  75. *
  76. * @param {Object} state - The Redux state.
  77. * @param {Object} ownProps - The properties explicitly passed to the component instance.
  78. * @returns {Props}
  79. */
  80. function _mapStateToProps(state, ownProps) {
  81. const enabled = getFeatureFlag(state, TILE_VIEW_ENABLED, true);
  82. const { visible = enabled } = ownProps;
  83. return {
  84. _tileViewEnabled: shouldDisplayTileView(state),
  85. visible
  86. };
  87. }
  88. export default translate(connect(_mapStateToProps)(TileViewButton));