Skip to content

Commit 16e89d8

Browse files
committed
Safe window access for SSR
1 parent 97863a8 commit 16e89d8

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/GithubContributions.jsx

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,55 @@ import { Loader } from "./Loader";
44
import "./GithubContributions.css";
55

66
class GithubContributions extends React.Component {
7-
87
constructor(props) {
98
super(props);
10-
this.state = {
9+
this.state = {
1110
loaded: false,
1211
error: false,
1312
chart: "",
1413
header: "",
1514
tooltip: {
16-
position: null
17-
}
15+
position: null,
16+
},
1817
};
1918
this.init();
2019
}
2120

2221
componentDidMount() {
23-
window.addEventListener("scroll", this.handleMouseOut);
22+
typeof window !== "undefined" &&
23+
window.addEventListener("scroll", this.handleMouseOut);
2424
}
2525

2626
componentWillUnmount() {
27-
window.removeEventListener("scroll", this.handleMouseOut);
27+
typeof window !== "undefined" &&
28+
window.removeEventListener("scroll", this.handleMouseOut);
2829
}
2930

3031
handleMouseOver = (e) => {
3132
if (e.target && e.target.matches && e.target.matches("rect")) {
32-
const rect = e.target.getBoundingClientRect()
33+
const rect = e.target.getBoundingClientRect();
3334
this.setState({
3435
tooltip: {
3536
data: e.target.dataset,
3637
position: {
3738
left: rect.x + rect.width / 2,
38-
top: rect.y - 4
39-
}
40-
}
41-
})
39+
top: rect.y - 4,
40+
},
41+
},
42+
});
4243
}
43-
}
44+
};
4445

4546
handleMouseOut = (e) => {
4647
this.setState({ tooltip: null });
47-
}
48+
};
4849

4950
async init() {
50-
5151
let responseText;
5252

5353
try {
5454
const contributions = await fetch(
55-
`https://cors-anywhere.herokuapp.com/https://github.com/users/${this.props.username}/contributions`,
55+
`https://cors-anywhere.herokuapp.com/https://github.com/users/${this.props.username}/contributions`,
5656
{ mode: "cors" }
5757
);
5858

@@ -66,41 +66,51 @@ class GithubContributions extends React.Component {
6666
doc = parser.parseFromString(responseText, "text/html"),
6767
graph = doc.body.querySelector(".calendar-graph"),
6868
svg = graph.querySelector("svg");
69-
70-
svg.setAttribute("viewBox", `0 0 ${svg.getAttribute("width") || "828"} ${svg.getAttribute("height") || "128"}`);
7169

72-
this.setState({
70+
svg.setAttribute(
71+
"viewBox",
72+
`0 0 ${svg.getAttribute("width") || "828"} ${
73+
svg.getAttribute("height") || "128"
74+
}`
75+
);
76+
77+
this.setState({
7378
loaded: true,
7479
header: doc.body.querySelector(".f4").innerHTML,
75-
chart: graph.innerHTML
80+
chart: graph.innerHTML,
7681
});
7782
}
7883

7984
render() {
8085
return this.state.loaded ? (
8186
<ChartContainer>
8287
<div className="contributions">
83-
84-
{ this.state.error ? <div>Sorry, we couldn't load these contributions right now</div> : "" }
88+
{this.state.error ? (
89+
<div>Sorry, we couldn't load these contributions right now</div>
90+
) : (
91+
""
92+
)}
8593

8694
<h2 className="contributions-header">{this.state.header}</h2>
87-
<div className="contributions-chart"
95+
<div
96+
className="contributions-chart"
8897
onMouseOver={this.handleMouseOver}
8998
onMouseOut={this.handleMouseOut}
9099
dangerouslySetInnerHTML={{ __html: this.state.chart }}></div>
91-
{
92-
this.state.tooltip && this.state.tooltip.data ?
93-
<div className="tooltip" style={this.state.tooltip.position}>
94-
<strong>Date:</strong> {this.state.tooltip.data.date} <br />
95-
<strong>Contributions:</strong> {this.state.tooltip.data.count}
96-
</div>
97-
: ""
98-
}
100+
{this.state.tooltip && this.state.tooltip.data ? (
101+
<div className="tooltip" style={this.state.tooltip.position}>
102+
<strong>Date:</strong> {this.state.tooltip.data.date} <br />
103+
<strong>Contributions:</strong> {this.state.tooltip.data.count}
104+
</div>
105+
) : (
106+
""
107+
)}
99108
</div>
100109
</ChartContainer>
101-
) : <Loader />;
110+
) : (
111+
<Loader />
112+
);
102113
}
103-
104114
}
105115

106-
export default GithubContributions;
116+
export default GithubContributions;

0 commit comments

Comments
 (0)