about summary refs log tree commit diff
path: root/app/javascript/mastodon/features/ui/components/columns_area.js
blob: 3c3e9425d36b5a7a387d5cb571820dd575b8809b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import ReactSwipeable from 'react-swipeable';
import HomeTimeline from '../../home_timeline';
import Notifications from '../../notifications';
import PublicTimeline from '../../public_timeline';
import CommunityTimeline from '../../community_timeline';
import HashtagTimeline from '../../hashtag_timeline';
import Compose from '../../compose';
import { getPreviousLink, getNextLink } from './tabs_bar';

const componentMap = {
  'COMPOSE': Compose,
  'HOME': HomeTimeline,
  'NOTIFICATIONS': Notifications,
  'PUBLIC': PublicTimeline,
  'COMMUNITY': CommunityTimeline,
  'HASHTAG': HashtagTimeline,
};

export default class ColumnsArea extends ImmutablePureComponent {

  static contextTypes = {
    router: PropTypes.object.isRequired,
  };

  static propTypes = {
    columns: ImmutablePropTypes.list.isRequired,
    singleColumn: PropTypes.bool,
    children: PropTypes.node,
  };

  handleRightSwipe = () => {
    const previousLink = getPreviousLink(this.context.router.history.location.pathname);

    if (previousLink) {
      this.context.router.history.push(previousLink);
    }
  }

  handleLeftSwipe = () => {
    const previousLink = getNextLink(this.context.router.history.location.pathname);

    if (previousLink) {
      this.context.router.history.push(previousLink);
    }
  };

  render () {
    const { columns, children, singleColumn } = this.props;

    if (singleColumn) {
      return (
        <ReactSwipeable onSwipedLeft={this.handleLeftSwipe} onSwipedRight={this.handleRightSwipe} className='columns-area'>
          {children}
        </ReactSwipeable>
      );
    }

    return (
      <div className='columns-area'>
        {columns.map(column => {
          const SpecificComponent = componentMap[column.get('id')];
          const params = column.get('params', null) === null ? null : column.get('params').toJS();
          return <SpecificComponent key={column.get('uuid')} columnId={column.get('uuid')} params={params} multiColumn />;
        })}

        {React.Children.map(children, child => React.cloneElement(child, { multiColumn: true }))}
      </div>
    );
  }

}