import React, { useState, useEffect } from 'react';
import {
View,
Text,
StyleSheet,
ScrollView,
Share,
TextInput,
Platform,
} from 'react-native';
import NetworkRequestInfo from '../NetworkRequestInfo';
import { useThemedStyles, Theme } from '../theme';
import { backHandlerSet } from '../backHandler';
import ResultItem from './ResultItem';
import Header from './Header';
import Button from './Button';
interface Props {
request: NetworkRequestInfo;
onClose(): void;
}
const Headers = ({
title = 'Headers',
headers,
}: {
title: string;
headers?: object;
}) => {
const styles = useThemedStyles(themedStyles);
return (
{Object.entries(headers || {}).map(([name, value]) => (
{name}:
{value}
))}
);
};
const LargeText: React.FC<{ children: string }> = ({ children }) => {
const styles = useThemedStyles(themedStyles);
if (Platform.OS === 'ios') {
/**
* A readonly TextInput is used because large Text blocks sometimes don't render on iOS
* See this issue https://github.com/facebook/react-native/issues/19453
* Note: Even with the fix mentioned in the comments, text with ~10,000 lines still fails to render
*/
return (
);
}
return (
{children}
);
};
const RequestDetails: React.FC = ({ request, onClose }) => {
const [responseBody, setResponseBody] = useState('Loading...');
const styles = useThemedStyles(themedStyles);
useEffect(() => {
(async () => {
const body = await request.getResponseBody();
setResponseBody(body);
})();
}, [request]);
const requestBody = request.getRequestBody(!!request.gqlOperation);
const getFullRequest = () => {
let response;
if (responseBody) {
try {
response = JSON.parse(responseBody);
} catch {
response = `${responseBody}`;
}
}
const processedRequest = {
...request,
response,
duration: request.duration,
};
return JSON.stringify(processedRequest, null, 2);
};
return (
{requestBody}
{responseBody}
{!backHandlerSet() && (
)}
);
};
const themedStyles = (theme: Theme) =>
StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.colors.background,
justifyContent: 'center',
alignItems: 'center',
paddingBottom: 10,
},
info: {
margin: 0,
},
close: {
position: 'absolute',
right: 10,
top: 0,
},
scrollView: {
width: '100%',
},
headerContainer: { flexDirection: 'row', flexWrap: 'wrap' },
headerKey: { fontWeight: 'bold', color: theme.colors.text },
headerValue: { color: theme.colors.text },
text: {
fontSize: 16,
color: theme.colors.text,
},
content: {
backgroundColor: theme.colors.card,
padding: 10,
color: theme.colors.text,
},
largeContent: {
maxHeight: 300,
},
});
export default RequestDetails;