Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TileViewButton.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.enterTileView';
  37. toggledAccessibilityLabel = 'toolbar.accessibilityLabel.exitTileView';
  38. icon = IconTileView;
  39. label = 'toolbar.enterTileView';
  40. toggledLabel = 'toolbar.exitTileView';
  41. tooltip = 'toolbar.tileViewToggle';
  42. /**
  43. * Handles clicking / pressing the button.
  44. *
  45. * @override
  46. * @protected
  47. * @returns {void}
  48. */
  49. _handleClick() {
  50. const { _tileViewEnabled, dispatch } = this.props;
  51. const value = !_tileViewEnabled;
  52. sendAnalytics(createToolbarEvent(
  53. 'tileview.button',
  54. {
  55. 'is_enabled': value
  56. }));
  57. logger.debug(`Tile view ${value ? 'enable' : 'disable'}`);
  58. batch(() => {
  59. dispatch(setTileView(value));
  60. navigator.product !== 'ReactNative' && dispatch(setOverflowMenuVisible(false));
  61. });
  62. }
  63. /**
  64. * Indicates whether this button is in toggled state or not.
  65. *
  66. * @override
  67. * @protected
  68. * @returns {boolean}
  69. */
  70. _isToggled() {
  71. return this.props._tileViewEnabled;
  72. }
  73. }
  74. /**
  75. * Maps (parts of) the redux state to the associated props for the
  76. * {@code TileViewButton} component.
  77. *
  78. * @param {Object} state - The Redux state.
  79. * @param {Object} ownProps - The properties explicitly passed to the component instance.
  80. * @returns {Props}
  81. */
  82. function _mapStateToProps(state, ownProps) {
  83. const enabled = getFeatureFlag(state, TILE_VIEW_ENABLED, true);
  84. const { visible = enabled } = ownProps;
  85. return {
  86. _tileViewEnabled: shouldDisplayTileView(state),
  87. visible
  88. };
  89. }
  90. export default translate(connect(_mapStateToProps)(TileViewButton));