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.

106 lines
4.2 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { NavLink, withRouter } from 'react-router-dom';
  4. import { FormattedMessage, injectIntl } from 'react-intl';
  5. import { debounce } from 'lodash';
  6. import { isUserTouching } from 'flavours/glitch/util/is_mobile';
  7. import { connect } from 'react-redux';
  8. const mapStateToProps = state => ({
  9. unreadNotifications: state.getIn(['notifications', 'unread']),
  10. showBadge: state.getIn(['local_settings', 'notifications', 'tab_badge']),
  11. });
  12. @connect(mapStateToProps)
  13. class NotificationsIcon extends React.PureComponent {
  14. static propTypes = {
  15. unreadNotifications: PropTypes.number,
  16. showBadge: PropTypes.bool,
  17. };
  18. render() {
  19. const { unreadNotifications, showBadge } = this.props;
  20. return (
  21. <span className='icon-badge-wrapper'>
  22. <i className='fa fa-fw fa-bell' />
  23. { showBadge && unreadNotifications > 0 && <div className='icon-badge' />}
  24. </span>
  25. );
  26. }
  27. }
  28. export const links = [
  29. <NavLink className='tabs-bar__link primary' to='/timelines/home' data-preview-title-id='column.home' data-preview-icon='home' ><i className='fa fa-fw fa-home' /><FormattedMessage id='tabs_bar.home' defaultMessage='Home' /></NavLink>,
  30. <NavLink className='tabs-bar__link primary' to='/notifications' data-preview-title-id='column.notifications' data-preview-icon='bell' ><NotificationsIcon /><FormattedMessage id='tabs_bar.notifications' defaultMessage='Notifications' /></NavLink>,
  31. <NavLink className='tabs-bar__link secondary' to='/timelines/public/local' data-preview-title-id='column.community' data-preview-icon='users' ><i className='fa fa-fw fa-users' /><FormattedMessage id='tabs_bar.local_timeline' defaultMessage='Local' /></NavLink>,
  32. <NavLink className='tabs-bar__link secondary' exact to='/timelines/public' data-preview-title-id='column.public' data-preview-icon='globe' ><i className='fa fa-fw fa-globe' /><FormattedMessage id='tabs_bar.federated_timeline' defaultMessage='Federated' /></NavLink>,
  33. <NavLink className='tabs-bar__link primary' to='/search' data-preview-title-id='tabs_bar.search' data-preview-icon='bell' ><i className='fa fa-fw fa-search' /><FormattedMessage id='tabs_bar.search' defaultMessage='Search' /></NavLink>,
  34. <NavLink className='tabs-bar__link primary' style={{ flexGrow: '0', flexBasis: '30px' }} to='/getting-started' data-preview-title-id='getting_started.heading' data-preview-icon='bars' ><i className='fa fa-fw fa-bars' /></NavLink>,
  35. ];
  36. export function getIndex (path) {
  37. return links.findIndex(link => link.props.to === path);
  38. }
  39. export function getLink (index) {
  40. return links[index].props.to;
  41. }
  42. @injectIntl
  43. @withRouter
  44. export default class TabsBar extends React.PureComponent {
  45. static propTypes = {
  46. intl: PropTypes.object.isRequired,
  47. history: PropTypes.object.isRequired,
  48. }
  49. setRef = ref => {
  50. this.node = ref;
  51. }
  52. handleClick = (e) => {
  53. // Only apply optimization for touch devices, which we assume are slower
  54. // We thus avoid the 250ms delay for non-touch devices and the lag for touch devices
  55. if (isUserTouching()) {
  56. e.preventDefault();
  57. e.persist();
  58. requestAnimationFrame(() => {
  59. const tabs = Array(...this.node.querySelectorAll('.tabs-bar__link'));
  60. const currentTab = tabs.find(tab => tab.classList.contains('active'));
  61. const nextTab = tabs.find(tab => tab.contains(e.target));
  62. const { props: { to } } = links[Array(...this.node.childNodes).indexOf(nextTab)];
  63. if (currentTab !== nextTab) {
  64. if (currentTab) {
  65. currentTab.classList.remove('active');
  66. }
  67. const listener = debounce(() => {
  68. nextTab.removeEventListener('transitionend', listener);
  69. this.props.history.push(to);
  70. }, 50);
  71. nextTab.addEventListener('transitionend', listener);
  72. nextTab.classList.add('active');
  73. }
  74. });
  75. }
  76. }
  77. render () {
  78. const { intl: { formatMessage } } = this.props;
  79. return (
  80. <nav className='tabs-bar' ref={this.setRef}>
  81. {links.map(link => React.cloneElement(link, { key: link.props.to, onClick: this.handleClick, 'aria-label': formatMessage({ id: link.props['data-preview-title-id'] }) }))}
  82. </nav>
  83. );
  84. }
  85. }