aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/services/content/WebviewCrashHandler.tsx
blob: 91c9cf92717035b3987a2c23f0b7179601198c1e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Component, ReactElement } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
import ms from 'ms';
import Button from '../../ui/button';
import { H1 } from '../../ui/headline';

const messages = defineMessages({
  headline: {
    id: 'service.crashHandler.headline',
    defaultMessage: 'Oh no!',
  },
  text: {
    id: 'service.crashHandler.text',
    defaultMessage: '{name} has caused an error.',
  },
  action: {
    id: 'service.crashHandler.action',
    defaultMessage: 'Reload {name}',
  },
  autoReload: {
    id: 'service.crashHandler.autoReload',
    defaultMessage:
      'Trying to automatically restore {name} in {seconds} seconds',
  },
});

interface IProps extends WrappedComponentProps {
  name: string;
  reload: () => void;
}

interface IState {
  countdown: number;
}

@observer
class WebviewCrashHandler extends Component<IProps, IState> {
  countdownInterval: NodeJS.Timeout | undefined;

  countdownIntervalTimeout = ms('1s');

  constructor(props: IProps) {
    super(props);

    this.state = {
      countdown: ms('10s'),
    };
  }

  componentDidMount(): void {
    const { reload } = this.props;

    this.countdownInterval = setInterval(() => {
      this.setState(prevState => ({
        countdown: prevState.countdown - this.countdownIntervalTimeout,
      }));

      if (this.state.countdown <= 0) {
        reload();
        clearInterval(this.countdownInterval);
      }
    }, this.countdownIntervalTimeout);
  }

  render(): ReactElement {
    const { name, reload, intl } = this.props;

    return (
      <div className="services__info-layer">
        <H1>{intl.formatMessage(messages.headline)}</H1>
        <p>{intl.formatMessage(messages.text, { name })}</p>
        <Button
          // label={`Reload ${name}`}
          label={intl.formatMessage(messages.action, { name })}
          buttonType="inverted"
          onClick={() => reload()}
        />
        <p className="footnote">
          {intl.formatMessage(messages.autoReload, {
            name,
            seconds: this.state.countdown / ms('1s'),
          })}
        </p>
      </div>
    );
  }
}

export default injectIntl(WebviewCrashHandler);