-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrollbar-context.js
64 lines (53 loc) · 1.44 KB
/
rollbar-context.js
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
'use client';
import { Component } from 'react';
import PropTypes from 'prop-types';
import { Context, getRollbarFromContext } from './provider';
export class RollbarContext extends Component {
static propTypes = {
context: PropTypes.string.isRequired,
onRender: PropTypes.bool,
children: PropTypes.node,
};
static defaultProps = {
onRender: false,
};
static contextType = Context;
firstRender = true;
constructor(props) {
super(props);
this.state = { previousContext: null };
}
changeContext = (storePrevious = true) => {
const rollbar = getRollbarFromContext(this.context);
const { context } = this.props;
if (storePrevious) {
this.setState({ previousContext: rollbar.options.payload.context });
}
rollbar.configure({ payload: { context } });
};
componentDidMount() {
const { onRender } = this.props;
if (!onRender) {
this.changeContext(true);
}
}
componentDidUpdate() {
const { onRender } = this.props;
if (!onRender) {
this.changeContext(false);
}
}
componentWillUnmount() {
const rollbar = getRollbarFromContext(this.context);
const { previousContext } = this.state;
rollbar.configure({ payload: { context: previousContext } });
}
render() {
const { onRender } = this.props;
if (onRender && this.firstRender) {
this.changeContext(true);
}
this.firstRender = false;
return this.props.children;
}
}