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.

100 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 '../../../is_mobile';
  7. import Icon from 'mastodon/components/icon';
  8. import NotificationsCounterIcon from './notifications_counter_icon';
  9. import { treeRoot } from '../../../initial_state';
  10. import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';
  11. export const links = [
  12. <NavLink className='tabs-bar__link' to='/timelines/home' data-preview-title-id='column.home' data-preview-icon='home' ><Icon id='home' fixedWidth /><FormattedMessage id='tabs_bar.home' defaultMessage='Home' /></NavLink>,
  13. treeRoot && (
  14. <NavLink className='tabs-bar__link' to={treeRoot} data-preview-title-id='column.tree' data-preview-icon='tree' ><Icon id='tree' fixedWidth /><FormattedMessage id='tabs_bar.tree' defaultMessage='Tree' /></NavLink>),
  15. <NavLink className='tabs-bar__link' to='/notifications' data-preview-title-id='column.notifications' data-preview-icon='bell' ><NotificationsCounterIcon /><FormattedMessage id='tabs_bar.notifications' defaultMessage='Notifications' /></NavLink>,
  16. <NavLink className='tabs-bar__link' to='/timelines/public/local' data-preview-title-id='column.community' data-preview-icon='users' ><Icon id='users' fixedWidth /><FormattedMessage id='tabs_bar.local_timeline' defaultMessage='Local' /></NavLink>,
  17. <NavLink className='tabs-bar__link' exact to='/timelines/public' data-preview-title-id='column.public' data-preview-icon='globe' ><Icon id='globe' fixedWidth /><FormattedMessage id='tabs_bar.federated_timeline' defaultMessage='Federated' /></NavLink>,
  18. <NavLink className='tabs-bar__link optional' to='/search' data-preview-title-id='tabs_bar.search' data-preview-icon='bell' ><Icon id='search' fixedWidth /><FormattedMessage id='tabs_bar.search' defaultMessage='Search' /></NavLink>,
  19. <NavLink className='tabs-bar__link' style={{ flexGrow: '0', flexBasis: '30px' }} to='/getting-started' data-preview-title-id='getting_started.heading' data-preview-icon='bars' ><Icon id='bars' fixedWidth /></NavLink>,
  20. ].filter(q => !!q);
  21. export function getIndex (path) {
  22. return links.findIndex(link => link.props.to === path);
  23. }
  24. export function getLink (index) {
  25. return links[index].props.to;
  26. }
  27. export default @injectIntl
  28. @withRouter
  29. class TabsBar extends React.PureComponent {
  30. static propTypes = {
  31. intl: PropTypes.object.isRequired,
  32. history: PropTypes.object.isRequired,
  33. }
  34. state = {
  35. showPinned: true,
  36. }
  37. setRef = ref => {
  38. this.node = ref;
  39. }
  40. handleClick = (e) => {
  41. // Only apply optimization for touch devices, which we assume are slower
  42. // We thus avoid the 250ms delay for non-touch devices and the lag for touch devices
  43. if (isUserTouching()) {
  44. e.preventDefault();
  45. e.persist();
  46. requestAnimationFrame(() => {
  47. const tabs = Array(...this.node.querySelectorAll('.tabs-bar__link'));
  48. const currentTab = tabs.find(tab => tab.classList.contains('active'));
  49. const nextTab = tabs.find(tab => tab.contains(e.target));
  50. const { props: { to } } = links[Array(...this.node.childNodes).indexOf(nextTab)];
  51. if (currentTab !== nextTab) {
  52. if (currentTab) {
  53. currentTab.classList.remove('active');
  54. }
  55. const listener = debounce(() => {
  56. nextTab.removeEventListener('transitionend', listener);
  57. this.props.history.push(to);
  58. }, 50);
  59. nextTab.addEventListener('transitionend', listener);
  60. nextTab.classList.add('active');
  61. }
  62. });
  63. }
  64. }
  65. handleClear = () => {
  66. this.setState({ showPinned: false});
  67. }
  68. render () {
  69. const { intl: { formatMessage } } = this.props;
  70. const { showPinned } = this.state;
  71. return (
  72. <div className='tabs-bar__wrapper'>
  73. <nav className='tabs-bar' ref={this.setRef}>
  74. {links.map(link => React.cloneElement(link, { key: link.props.to, onClick: this.handleClick, 'aria-label': formatMessage({ id: link.props['data-preview-title-id'] }) }))}
  75. </nav>
  76. <div id='tabs-bar__portal' />
  77. </div>
  78. );
  79. }
  80. }