Add screens for opting into Sentry error tracking
This commit is contained in:
59
src/utility/ErrorReportingAlert.ts
Normal file
59
src/utility/ErrorReportingAlert.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { useEffect } from 'react';
|
||||
import { Alert } from 'react-native';
|
||||
import { useTypedSelector } from 'store';
|
||||
import { t } from '@localisation';
|
||||
import { setReceivedErrorReportingAlert } from 'store/settings/actions';
|
||||
import { setSentryStatus } from './Sentry';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import { useDispatch } from 'react-redux';
|
||||
|
||||
/**
|
||||
* This will send out an alert message asking the user if they want to enable
|
||||
* error reporting.
|
||||
*/
|
||||
export default function ErrorReportingAlert() {
|
||||
const { hasReceivedErrorReportingAlert } = useTypedSelector(state => state.settings);
|
||||
const navigation = useNavigation();
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
// Only send out alert if we haven't done so ever
|
||||
if (!hasReceivedErrorReportingAlert) {
|
||||
// Generate the alert
|
||||
Alert.alert(
|
||||
t('enable-error-reporting'),
|
||||
t('enable-error-reporting-description'),
|
||||
[
|
||||
{
|
||||
text: t('enable'),
|
||||
style: 'default',
|
||||
onPress: () => {
|
||||
setSentryStatus(true);
|
||||
}
|
||||
},
|
||||
{
|
||||
text: t('disable'),
|
||||
style: 'destructive',
|
||||
onPress: () => {
|
||||
setSentryStatus(false);
|
||||
}
|
||||
},
|
||||
{
|
||||
text: t('more-info'),
|
||||
style: 'cancel',
|
||||
onPress: () => {
|
||||
navigation.navigate('ErrorReporting');
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
// Store the flag that we have sent out the alert, so that we don't
|
||||
// have to do so anymore in the future.
|
||||
dispatch(setReceivedErrorReportingAlert());
|
||||
}
|
||||
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
50
src/utility/Sentry.ts
Normal file
50
src/utility/Sentry.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { SENTRY_DSN } from '@env';
|
||||
import AsyncStorage from '@react-native-community/async-storage';
|
||||
import * as Sentry from '@sentry/react-native';
|
||||
|
||||
const SENTRY_ASYNC__ITEM_STRING = 'sentry_enabled';
|
||||
export let isSentryEnabled = false;
|
||||
|
||||
/**
|
||||
* Setup Sentry based on what value is stored in AsyncStorage.
|
||||
*/
|
||||
export async function setupSentry(): Promise<void> {
|
||||
// First, we'll retrieve the user settings. This delays Sentry being active
|
||||
// slightly, at the bonus of acutally being able to send off reports for
|
||||
// start-up stuff.
|
||||
isSentryEnabled = (await AsyncStorage.getItem(SENTRY_ASYNC__ITEM_STRING)) === 'true';
|
||||
|
||||
// Make sure the DSN is actually set, in order to prevent weird erros.
|
||||
if (SENTRY_DSN) {
|
||||
Sentry.init({
|
||||
dsn: SENTRY_DSN,
|
||||
// Before we send any event, check whether the Sentry SDK should be
|
||||
// enabled based on user settings.
|
||||
beforeSend(event) {
|
||||
// If so, pass off the event to the back-end
|
||||
if (isSentryEnabled) {
|
||||
return event;
|
||||
}
|
||||
|
||||
// If not, don't sent a thing
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to enable or disable the Sentry SDK for this app.
|
||||
*/
|
||||
export async function setSentryStatus(isEnabled: boolean): Promise<void> {
|
||||
// GUARD: If nothing's changed, change nothing
|
||||
if (isEnabled === isSentryEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// First, store the value in Async Storage
|
||||
await AsyncStorage.setItem(SENTRY_ASYNC__ITEM_STRING, isEnabled.toString());
|
||||
|
||||
// Then, assign it to the variable
|
||||
isSentryEnabled = isEnabled;
|
||||
}
|
||||
Reference in New Issue
Block a user