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.

TileViewButton.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 } = this.props;
  48. const value = !_tileViewEnabled;
  49. sendAnalytics(createToolbarEvent(
  50. 'tileview.button',
  51. {
  52. 'is_enabled': value
  53. }));
  54. logger.debug(`Tile view ${value ? 'enable' : 'disable'}`);
  55. dispatch(setTileView(value));
  56. }
  57. /**
  58. * Indicates whether this button is in toggled state or not.
  59. *
  60. * @override
  61. * @protected
  62. * @returns {boolean}
  63. */
  64. _isToggled() {
  65. return this.props._tileViewEnabled;
  66. }
  67. }
  68. /**
  69. * Maps (parts of) the redux state to the associated props for the
  70. * {@code TileViewButton} component.
  71. *
  72. * @param {Object} state - The Redux state.
  73. * @param {Object} ownProps - The properties explicitly passed to the component instance.
  74. * @returns {Props}
  75. */
  76. function _mapStateToProps(state, ownProps) {
  77. const enabled = getFeatureFlag(state, TILE_VIEW_ENABLED, true);
  78. const { visible = enabled } = ownProps;
  79. return {
  80. _tileViewEnabled: shouldDisplayTileView(state),
  81. visible
  82. };
  83. }
  84. export default translate(connect(_mapStateToProps)(TileViewButton));