tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ErrorBoundary.jsx (1902B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { A11yLinkButton } from "content-src/components/A11yLinkButton/A11yLinkButton";
      6 import React from "react";
      7 
      8 export class ErrorBoundaryFallback extends React.PureComponent {
      9  constructor(props) {
     10    super(props);
     11    this.windowObj = this.props.windowObj || window;
     12    this.onClick = this.onClick.bind(this);
     13  }
     14 
     15  /**
     16   * Since we only get here if part of the page has crashed, do a
     17   * forced reload to give us the best chance at recovering.
     18   */
     19  onClick() {
     20    this.windowObj.location.reload(true);
     21  }
     22 
     23  render() {
     24    const defaultClass = "as-error-fallback";
     25    let className;
     26    if ("className" in this.props) {
     27      className = `${this.props.className} ${defaultClass}`;
     28    } else {
     29      className = defaultClass;
     30    }
     31 
     32    // "A11yLinkButton" to force normal link styling stuff (eg cursor on hover)
     33    return (
     34      <div className={className}>
     35        <div data-l10n-id="newtab-error-fallback-info" />
     36        <span>
     37          <A11yLinkButton
     38            className="reload-button"
     39            onClick={this.onClick}
     40            data-l10n-id="newtab-error-fallback-refresh-link"
     41          />
     42        </span>
     43      </div>
     44    );
     45  }
     46 }
     47 ErrorBoundaryFallback.defaultProps = { className: "as-error-fallback" };
     48 
     49 export class ErrorBoundary extends React.PureComponent {
     50  constructor(props) {
     51    super(props);
     52    this.state = { hasError: false };
     53  }
     54 
     55  componentDidCatch() {
     56    this.setState({ hasError: true });
     57  }
     58 
     59  render() {
     60    if (!this.state.hasError) {
     61      return this.props.children;
     62    }
     63 
     64    return <this.props.FallbackComponent className={this.props.className} />;
     65  }
     66 }
     67 
     68 ErrorBoundary.defaultProps = { FallbackComponent: ErrorBoundaryFallback };