github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-server/web/src/components/Link.js (about)

     1  import { Link as RouterLink } from 'react-router-dom';
     2  import { withRouter } from 'react-router';
     3  import React from 'react';
     4  import Url from 'url';
     5  import classnames from 'classnames';
     6  
     7  class Link extends React.Component {
     8    constructor() {
     9      super();
    10      this.state = {
    11        isLocal: false,
    12      };
    13    }
    14  
    15    /* eslint-disable react/no-did-mount-set-state */
    16    componentDidMount() {
    17      const { to } = this.props;
    18      if (to && typeof window !== 'undefined') {
    19        const url = Url.parse(to);
    20        if (window.location.hostname === url.hostname || !url.hostname || !url.hostname.length) {
    21          this.setState({
    22            isLocal: true,
    23            localTo: to.replace('www.', '').replace(window.location.origin, ''),
    24          });
    25        }
    26      }
    27    }
    28    /* eslint-enable */
    29  
    30    render() {
    31      const {
    32        to, className, children, location, activeClass, onClick,
    33      } = this.props;
    34      const { isLocal, localTo } = this.state;
    35      const active = localTo && activeClass && location.pathname === localTo ? activeClass : null;
    36  
    37      return (
    38        isLocal ?
    39          <RouterLink
    40            to={localTo}
    41            className={classnames([className, active])}
    42            onClick={onClick}
    43          >
    44            {children}
    45          </RouterLink>
    46          :
    47          <a
    48            href={to}
    49            className={className}
    50            onClick={onClick}
    51          >
    52            {children}
    53          </a>
    54      );
    55    }
    56  }
    57  
    58  export default withRouter(Link);