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.ts 2.9KB

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