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.8KB

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