-
Notifications
You must be signed in to change notification settings - Fork 64
Add json viewer and update content layout #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kaveh-ap
wants to merge
3
commits into
alexbrazier:master
Choose a base branch
from
Kaveh-ap:feat/add-json-viewer-and-update-content-layout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| import React, { useState, useMemo } from 'react'; | ||
| import { | ||
| View, | ||
| Text, | ||
| StyleSheet, | ||
| ScrollView, | ||
| Platform, | ||
| TouchableOpacity, | ||
| TextInput, | ||
| } from 'react-native'; | ||
| import { useThemedStyles, Theme } from '../theme'; | ||
|
|
||
| type JsonPrimitive = string | number | boolean | null; | ||
| type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue }; | ||
|
|
||
| type BodyViewerProps = { | ||
| content?: string; | ||
| data?: JsonValue; | ||
| initiallyExpanded?: boolean; | ||
| }; | ||
|
|
||
| const BodyViewer: React.FC<BodyViewerProps> = ({ | ||
| content, | ||
| data, | ||
| initiallyExpanded = true, | ||
| }) => { | ||
| const parsed = useMemo(() => { | ||
| if (data !== undefined) return { isJson: true, value: data } as const; | ||
| return parseIfJson(content || ''); | ||
| }, [content, data]); | ||
|
|
||
| if (parsed.isJson && parsed.value !== null) { | ||
| return ( | ||
| <CollapsibleJsonView | ||
| data={parsed.value} | ||
| initiallyExpanded={initiallyExpanded} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return <TextViewer>{content || ''}</TextViewer>; | ||
| }; | ||
|
|
||
| export default BodyViewer; | ||
|
|
||
| const isObject = (v: JsonValue): v is { [key: string]: JsonValue } => | ||
| !!v && typeof v === 'object' && !Array.isArray(v); | ||
|
|
||
| const isArray = (v: JsonValue): v is JsonValue[] => Array.isArray(v); | ||
|
|
||
| const stringifyPrimitive = (value: JsonPrimitive) => { | ||
| if (typeof value === 'string') return `"${value}"`; | ||
| if (value === null) return 'null'; | ||
| return String(value); | ||
| }; | ||
|
|
||
| const ExpandRow: React.FC<{ | ||
| name?: string | number; | ||
| level: number; | ||
| open: string; | ||
| close: string; | ||
| expanded: boolean; | ||
| hasChildren: boolean; | ||
| childrenCount: number; | ||
| onToggle: () => void; | ||
| children?: React.ReactNode; | ||
| }> = ({ | ||
| name, | ||
| level, | ||
| open, | ||
| close, | ||
| expanded, | ||
| hasChildren, | ||
| childrenCount, | ||
| onToggle, | ||
| children, | ||
| }) => { | ||
| const styles = useThemedStyles(themedStyles); | ||
| return ( | ||
| <View | ||
| style={[ | ||
| styles.jsonLevelContainerBase, | ||
| level % 2 === 0 ? styles.jsonLevelEven : styles.jsonLevelOdd, | ||
| ]} | ||
| > | ||
| <TouchableOpacity | ||
| onPress={onToggle} | ||
| disabled={!hasChildren} | ||
| accessibilityRole="button" | ||
| accessibilityLabel="Expand or collapse section" | ||
| style={[styles.jsonRow, { paddingLeft: level * 12 }]} | ||
| > | ||
| <Text style={[styles.baseText, styles.jsonText]}> | ||
| {hasChildren ? (expanded ? '▼ ' : '▶ ') : ''} | ||
| {name !== undefined ? `${String(name)}: ` : ''} | ||
| <Text style={[styles.baseText, styles.jsonBracket]}>{open}</Text> | ||
| {!expanded ? `${hasChildren ? childrenCount : ''}` : ''} | ||
| {!expanded ? ( | ||
| <Text style={[styles.baseText, styles.jsonBracket]}>{close}</Text> | ||
| ) : null} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| {expanded && ( | ||
| <> | ||
| {children} | ||
| <Text style={[styles.baseText, styles.jsonText, styles.jsonBracket]}> | ||
| {close} | ||
| </Text> | ||
| </> | ||
| )} | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const JsonNode: React.FC<{ | ||
| name?: string | number; | ||
| value: JsonValue; | ||
| level: number; | ||
| initiallyExpanded?: boolean; | ||
| }> = ({ name, value, level, initiallyExpanded = false }) => { | ||
| const styles = useThemedStyles(themedStyles); | ||
| const [expanded, setExpanded] = useState(initiallyExpanded); | ||
|
|
||
| if (isObject(value)) { | ||
| const entries = Object.entries(value); | ||
| const open = '{'; | ||
| const close = '}'; | ||
| const hasLength = entries.length > 0; | ||
| return ( | ||
| <ExpandRow | ||
| name={name} | ||
| level={level} | ||
| open={open} | ||
| close={close} | ||
| expanded={expanded} | ||
| hasChildren={hasLength} | ||
| childrenCount={entries.length} | ||
| onToggle={() => setExpanded((e) => !e)} | ||
| > | ||
| {entries.map(([k, v]) => ( | ||
| <JsonNode key={k} name={k} value={v} level={level + 1} /> | ||
| ))} | ||
| </ExpandRow> | ||
| ); | ||
| } | ||
|
|
||
| if (isArray(value)) { | ||
| const open = '['; | ||
| const close = ']'; | ||
| const hasLength = value?.length > 0; | ||
| return ( | ||
| <ExpandRow | ||
| name={name} | ||
| level={level} | ||
| open={open} | ||
| close={close} | ||
| expanded={expanded} | ||
| hasChildren={!!hasLength} | ||
| childrenCount={value?.length || 0} | ||
| onToggle={() => setExpanded((e) => !e)} | ||
| > | ||
| {value.map((v, idx) => ( | ||
| <JsonNode key={idx} name={idx} value={v} level={level + 1} /> | ||
| ))} | ||
| </ExpandRow> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <View style={[styles.jsonRow, { paddingLeft: level * 12 }]}> | ||
| <Text style={[styles.baseText, styles.jsonText]}> | ||
| {name !== undefined ? `${String(name)}: ` : ''} | ||
| {stringifyPrimitive(value as JsonPrimitive)} | ||
| </Text> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const CollapsibleJsonView: React.FC<{ | ||
| data: JsonValue; | ||
| initiallyExpanded?: boolean; | ||
| }> = ({ data, initiallyExpanded = true }) => { | ||
| const styles = useThemedStyles(themedStyles); | ||
| return ( | ||
| <View style={[styles.content, styles.textAreaContainer]}> | ||
| <ScrollView nestedScrollEnabled> | ||
| <JsonNode | ||
| level={0} | ||
| value={data} | ||
| initiallyExpanded={initiallyExpanded} | ||
| /> | ||
| </ScrollView> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const parseIfJson = ( | ||
| text: string | ||
| ): { isJson: boolean; value: JsonValue | null } => { | ||
| try { | ||
| const obj = JSON.parse(text); | ||
| return { isJson: true, value: obj }; | ||
| } catch { | ||
| return { isJson: false, value: null }; | ||
| } | ||
| }; | ||
|
|
||
| const TextViewer: 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 ( | ||
| <TextInput | ||
| multiline | ||
| editable={false} | ||
| value={children} | ||
| style={[styles.baseText, styles.content, styles.textAreaContainer]} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <View style={styles.textAreaContainer}> | ||
| <ScrollView nestedScrollEnabled> | ||
| <Text style={[styles.baseText, styles.content]} selectable> | ||
| {children} | ||
| </Text> | ||
| </ScrollView> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const themedStyles = (theme: Theme) => | ||
| StyleSheet.create({ | ||
| baseText: { | ||
| fontFamily: Platform.select({ | ||
| ios: 'Menlo', | ||
| android: 'monospace', | ||
| }), | ||
| color: theme.colors.text, | ||
| }, | ||
| content: { | ||
| padding: 10, | ||
| color: theme.colors.text, | ||
| backgroundColor: theme.colors.card, | ||
| }, | ||
| textAreaContainer: { | ||
| maxHeight: 350, | ||
| }, | ||
| jsonRow: { | ||
| paddingVertical: 4, | ||
| }, | ||
| jsonText: { | ||
| fontSize: 14, | ||
| }, | ||
| jsonBracket: { | ||
| paddingStart: 4, | ||
| color: theme.colors.text, | ||
| }, | ||
| jsonLevelContainerBase: { | ||
| borderRadius: 4, | ||
| marginVertical: 2, | ||
| paddingBottom: 2, | ||
| }, | ||
| jsonLevelEven: { | ||
| backgroundColor: `${theme.colors.card}ff`, | ||
| }, | ||
| jsonLevelOdd: { | ||
| backgroundColor: `${theme.colors.background}44`, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header title is always rendered inside a
TouchableOpacitywithaccessibilityRole="button", even whencollapsibleis false and noonPressis provided. In those non-collapsible cases, assistive technologies will announce an actionable control that does nothing, which is an accessibility regression for screen-reader users. Make the button role/press behavior conditional so static headers are exposed as plain text headers.Useful? React with 👍 / 👎.