~cytrogen/masto-fe

ref: d640c0356ebda19e1940c8c100613881243dc4b2 masto-fe/app/javascript/mastodon/features/ui/util/react_router_helpers.jsx -rw-r--r-- 2.9 KiB
d640c035 — Cytrogen Fix merged PR bugs and rewrite reduced-motion to pure CSS a month ago
                                                                                
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import PropTypes from "prop-types";
import { Component, PureComponent, cloneElement, Children } from "react";

import { Switch, Route } from "react-router-dom";

import StackTrace from "stacktrace-js";

import BundleColumnError from "../components/bundle_column_error";
import ColumnLoading from "../components/column_loading";
import BundleContainer from "../containers/bundle_container";

// Small wrapper to pass multiColumn to the route components
export class WrappedSwitch extends PureComponent {
  static contextTypes = {
    router: PropTypes.object,
  };

  render () {
    const { multiColumn, children } = this.props;
    const { location } = this.context.router.route;

    const decklessLocation = multiColumn && location.pathname.startsWith("/deck")
      ? {...location, pathname: location.pathname.slice(5)}
      : location;

    return (
      <Switch location={decklessLocation}>
        {Children.map(children, child => child ? cloneElement(child, { multiColumn }) : null)}
      </Switch>
    );
  }

}

WrappedSwitch.propTypes = {
  multiColumn: PropTypes.bool,
  children: PropTypes.node,
};

// Small Wrapper to extract the params from the route and pass
// them to the rendered component, together with the content to
// be rendered inside (the children)
export class WrappedRoute extends Component {

  static propTypes = {
    component: PropTypes.func.isRequired,
    content: PropTypes.node,
    multiColumn: PropTypes.bool,
    componentParams: PropTypes.object,
  };

  static defaultProps = {
    componentParams: {},
  };

  static getDerivedStateFromError () {
    return {
      hasError: true,
    };
  }

  state = {
    hasError: false,
    stacktrace: "",
  };

  componentDidCatch (error) {
    StackTrace.fromError(error).then(stackframes => {
      this.setState({ stacktrace: error.toString() + "\n" + stackframes.map(frame => frame.toString()).join("\n") });
    }).catch(err => {
      console.error(err);
    });
  }

  renderComponent = ({ match }) => {
    const { component, content, multiColumn, componentParams } = this.props;
    const { hasError, stacktrace } = this.state;

    if (hasError) {
      return (
        <BundleColumnError
          stacktrace={stacktrace}
          multiColumn={multiColumn}
          errorType='error'
        />
      );
    }

    return (
      <BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
        {Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>}
      </BundleContainer>
    );
  };

  renderLoading = () => {
    const { multiColumn } = this.props;

    return <ColumnLoading multiColumn={multiColumn} />;
  };

  renderError = (props) => {
    return <BundleColumnError {...props} errorType='network' />;
  };

  render () {
    const { component: Component, content, ...rest } = this.props;

    return <Route {...rest} render={this.renderComponent} />;
  }

}