This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
ErrorBoundary.tsx
136 lines (126 loc) · 3.38 KB
/
ErrorBoundary.tsx
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { Component } from "react"
import { StyleSheet, Text, View, Alert, Clipboard, Linking } from "react-native"
import CrossIcon from "./src/common/components/CrossIcon"
import SpotifyButton from "./src/common/components/SpotifyButton"
import { NativeModules } from "react-native"
import { colors } from "./src/common/theme"
export default class ErrorBoundary extends Component {
private static NO_STACK = "No stack trace."
private static REPO_URL = "https://github.com/osamaq/spotify-lite/"
private static ISSUE_URL = "issues/new?title="
private static ISSUE_BODY =
"&body=%23%23%23 Description%0A%0A%23%23%23 Stack Trace%0A%0A`Paste it here!`"
state: { hasError: boolean; error: Error | null } = {
hasError: false,
error: null,
}
static getDerivedStateFromError(error: Error) {
return { hasError: true, error }
}
showError = () => {
Alert.alert(
this.state.error?.name || "Error",
this.state.error?.stack || ErrorBoundary.NO_STACK,
[
{
text: "Cancel",
onPress: () => {
return
},
},
{
text: "Copy & Report On GitHub",
onPress: () => {
const stackTrace = this.state.error?.stack || ErrorBoundary.NO_STACK
const errName = this.state.error?.message || "Crash"
Clipboard.setString(stackTrace)
Linking.openURL(
ErrorBoundary.REPO_URL +
ErrorBoundary.ISSUE_URL +
errName +
ErrorBoundary.ISSUE_BODY,
)
},
},
],
{
cancelable: false,
},
)
}
reloadApp = () => {
NativeModules.DevSettings.reload()
}
render() {
if (this.state.hasError) {
return (
<View style={styles.container}>
<View style={styles.subContainer}>
<CrossIcon
color={colors.white}
handlePress={() => {
return
}}
iconStyle={styles.icon}
size={80}
/>
<Text style={styles.bigBoldText}>
Spotify couldn{"'"}t keep going...
</Text>
<Text style={styles.text}>
It would be great if you can report this!
</Text>
<View style={styles.btnContainer}>
<SpotifyButton text="SHOW ERROR" handlePress={this.showError} />
</View>
<View style={styles.restartBtn}>
<SpotifyButton
text="RESTART SPOTIFY"
handlePress={this.reloadApp}
color={colors.green}
textColor={colors.white}
/>
</View>
</View>
</View>
)
}
return this.props.children
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: colors.background,
flex: 1,
justifyContent: "flex-start",
},
subContainer: {
marginTop: "50%",
marginHorizontal: 30,
alignItems: "center",
},
icon: {
marginBottom: 18,
},
bigBoldText: {
fontSize: 18,
color: colors.white,
letterSpacing: 0.8,
fontWeight: "bold",
},
text: {
marginTop: 22,
marginHorizontal: 50,
lineHeight: 18,
fontSize: 12,
color: colors.grey,
letterSpacing: 0.4,
textAlign: "center",
},
btnContainer: {
marginTop: 44,
},
restartBtn: {
marginTop: 34,
},
})