import * as React from 'react'; import { useContext, useState } from 'react'; import { View, TouchableOpacity, ScrollView, StyleSheet } from 'react-native'; import { Appbar, Title } from 'react-native-paper'; import FeatherIcon from 'react-native-vector-icons/Feather'; import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'; import { Text } from './Text'; import JSONTree from 'react-native-json-tree'; import { ThemeContext } from '../Theme'; export interface IProps { onPressBack: (showJSONDetails: boolean) => void; data: any; title?: string; } // To access this view, you need to click on the button // 'view more' in the Details screen (after a response or a request body) export const JSONDetails: React.FC = props => { const theme = useContext(ThemeContext); const [viewJSON, setViewJSON] = useState(false); let _content; // Handling crash of JSON tree if the JSON is malformed. try { _content = ( {viewJSON ? ( {`${raw} : `}} valueRenderer={raw => {`${String(raw)} : `}} theme={theme} invertTheme={false} /> ) : ( {typeof props.data !== 'string' ? JSON.stringify(props.data, null, 2) : props.data} )} ); } catch (error) { _content = ( Oups... An error occurs with this JSON. Are you sure it is correctly formated ? {typeof props.data !== 'string' ? JSON.stringify(props.data, null, 2) : props.data} ); } return ( props.onPressBack(false)} testID="buttonBackToDetailsScreen" > setViewJSON(!viewJSON)} > {_content} ); }; export default JSONDetails; const styles = StyleSheet.create({ header: { elevation: 0, shadowOffset: { width: 0, height: 0, }, shadowOpacity: 0, }, container: { position: 'absolute', height: '100%', width: '100%', top: 0, left: 0, }, scrollview: { paddingHorizontal: 16, paddingTop: 20, paddingBottom: 20, }, button: { justifyContent: 'center', alignItems: 'center', height: 56, width: 56, borderLeftWidth: 1, }, });