Skip to content

Commit

Permalink
Pause monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallet committed May 18, 2023
1 parent a2b42ba commit 4fcc24c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class Logger {
private ignoredUrls: Set<string> | undefined;
private ignoredPatterns: RegExp[] | undefined;
public enabled = false;
public paused = false;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
callback = (requests: any[]) => {};
Expand Down Expand Up @@ -47,6 +48,10 @@ export default class Logger {
const xhrIndex = this.requests.length;
this.xhrIdMap[xhr._index] = xhrIndex;

if (this.paused) {
return;
}

if (this.ignoredHosts) {
const host = extractHost(url);
if (host && this.ignoredHosts.has(host)) {
Expand Down
26 changes: 17 additions & 9 deletions src/components/NetworkLogger.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, useCallback } from 'react';
import { Alert, View, StyleSheet, BackHandler, Share } from 'react-native';
import React, { useEffect, useState, useCallback, useMemo } from 'react';
import { View, StyleSheet, BackHandler, Share } from 'react-native';
import logger from '../loggerSingleton';
import NetworkRequestInfo from '../NetworkRequestInfo';
import { Theme, ThemeContext, ThemeName } from '../theme';
Expand Down Expand Up @@ -29,6 +29,7 @@ const NetworkLogger: React.FC<Props> = ({ theme = 'light', sort = 'desc' }) => {
const [request, setRequest] = useState<NetworkRequestInfo>();
const [showDetails, _setShowDetails] = useState(false);
const [mounted, setMounted] = useState(false);
const [paused, setPaused] = useState<boolean>(logger.paused);

const setShowDetails = useCallback((shouldShow: boolean) => {
_setShowDetails(shouldShow);
Expand Down Expand Up @@ -81,20 +82,27 @@ const NetworkLogger: React.FC<Props> = ({ theme = 'light', sort = 'desc' }) => {
});
};

const showMore = () => {
Alert.alert('More Options', undefined, [
const options = useMemo(() => {
return [
{
text: paused ? 'Resume' : 'Pause',
onPress: () => {
setPaused((prev: boolean) => {
logger.paused = !prev;
return !prev;
})
}
},
{
text: 'Clear Logs',
onPress: () => logger.clearRequests(),
style: 'destructive',
},
{
text: 'Export all Logs',
onPress: getHar,
},
{ text: 'Cancel', style: 'cancel' },
]);
};
];
}, [paused, getHar]);

return (
<ThemeContext.Provider value={theme}>
Expand All @@ -113,7 +121,7 @@ const NetworkLogger: React.FC<Props> = ({ theme = 'light', sort = 'desc' }) => {
) : (
<RequestList
requests={requests}
onShowMore={showMore}
options={options}
showDetails={showDetails && !!request}
onPressItem={(item) => {
setRequest(item);
Expand Down
14 changes: 9 additions & 5 deletions src/components/RequestList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import SearchBar from './SearchBar';
interface Props {
requests: NetworkRequestInfo[];
onPressItem: (item: NetworkRequestInfo) => void;
onShowMore: () => void;
options: {text: string; onPress: () => void }[];
showDetails: boolean;
}

const RequestList: React.FC<Props> = ({
requests,
onPressItem,
onShowMore,
options,
showDetails,
}) => {
const styles = useThemedStyles(themedStyles);
Expand Down Expand Up @@ -45,9 +45,13 @@ const RequestList: React.FC<Props> = ({
keyExtractor={(item) => item.id}
// eslint-disable-next-line react/no-unstable-nested-components
ListHeaderComponent={() => (
<Button onPress={onShowMore} style={styles.more}>
More
</Button>
<View style={{ flexDirection: 'row' }}>
{options.map(({ text, onPress }) => (
<Button key={text} onPress={onPress} style={styles.more}>
{text}
</Button>)
)}
</View>
)}
data={filteredRequests}
renderItem={({ item }) => (
Expand Down

0 comments on commit 4fcc24c

Please sign in to comment.