-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[menu-bar] Add logs to debug menu (#211)
- Loading branch information
1 parent
b4958b7
commit 5e10753
Showing
7 changed files
with
165 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
import { useState } from 'react'; | ||
import { TouchableOpacity, StyleProp, ViewStyle } from 'react-native'; | ||
|
||
import { ObjectInspector } from './ObjectInspector'; | ||
import type { Log } from '../../../modules/menu-bar/index'; | ||
import { Text } from '../Text'; | ||
import { View, Row } from '../View'; | ||
|
||
interface Props { | ||
log: Log; | ||
style?: StyleProp<ViewStyle>; | ||
} | ||
|
||
const DebugLogRow = ({ log, style }: Props) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<View style={style}> | ||
<TouchableOpacity onPress={() => setIsOpen((prev) => !prev)} disabled={!log.info}> | ||
<Row | ||
flex="1" | ||
style={{ | ||
paddingVertical: 5, | ||
}}> | ||
<Text style={{ marginRight: 40 }}>{log.command}</Text> | ||
<Text style={{ flex: 1 }} numberOfLines={1}> | ||
{log.info} | ||
</Text> | ||
</Row> | ||
{isOpen ? <ExtraInfo log={log} /> : null} | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
}; | ||
|
||
const ExtraInfo = ({ log }: Props) => { | ||
let extraInfo = log.info; | ||
const looksLikeJSON = extraInfo?.startsWith('{') || extraInfo?.startsWith('['); | ||
|
||
if (looksLikeJSON) { | ||
try { | ||
extraInfo = JSON.parse(log.info); | ||
} catch {} | ||
} | ||
|
||
return <ObjectInspector obj={extraInfo} />; | ||
}; | ||
|
||
export default DebugLogRow; |
45 changes: 45 additions & 0 deletions
45
apps/menu-bar/src/components/DebugLogs/ObjectInspector.tsx
This file contains 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,45 @@ | ||
import { useState } from 'react'; | ||
import { TouchableOpacity } from 'react-native'; | ||
|
||
import { Text } from '../Text'; | ||
import { View } from '../View'; | ||
|
||
export const ObjectInspector = ({ obj, name }: { obj: any; name?: string }) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const isObject = typeof obj === 'object'; | ||
const isArray = Array.isArray(obj); | ||
|
||
return ( | ||
<TouchableOpacity onPress={() => setIsOpen((prev) => !prev)}> | ||
<View | ||
style={{ | ||
flexDirection: 'row', | ||
paddingVertical: 5, | ||
flex: 1, | ||
}}> | ||
{isObject ? ( | ||
<Text> | ||
{isOpen ? '▼' : '▶'} {name ? `${name}: ` : ''} | ||
</Text> | ||
) : ( | ||
<Text> | ||
{name ? `${name}: ` : ''} | ||
{String(obj)} | ||
</Text> | ||
)} | ||
{isObject && !isOpen ? ( | ||
<Text style={{ opacity: 0.6, marginLeft: 6 }} numberOfLines={1}> | ||
{JSON.stringify(obj)} | ||
</Text> | ||
) : null} | ||
</View> | ||
{isObject && isOpen ? ( | ||
<View style={{ marginLeft: 10 }}> | ||
{Object.keys(obj).map((key) => ( | ||
<ObjectInspector obj={obj[key]} name={isArray ? `[${key}]` : key} /> | ||
))} | ||
</View> | ||
) : null} | ||
</TouchableOpacity> | ||
); | ||
}; |
This file contains 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,41 @@ | ||
import { ScrollView } from 'react-native'; | ||
|
||
import DebugLogRow from './DebugLogRow'; | ||
import MenuBarModule from '../../modules/MenuBarModule'; | ||
import { useExpoPalette } from '../../utils/useExpoTheme'; | ||
import { Text } from '../Text'; | ||
import { Row } from '../View'; | ||
|
||
export const DebugLogs = () => { | ||
const palette = useExpoPalette(); | ||
|
||
return ( | ||
<ScrollView | ||
contentContainerStyle={{ | ||
backgroundColor: palette['gray']['200'], | ||
borderRadius: 4, | ||
}}> | ||
<Row | ||
flex="1" | ||
style={{ | ||
paddingHorizontal: 10, | ||
paddingVertical: 5, | ||
}}> | ||
<Text style={{ marginRight: 40 }}>Command: </Text> | ||
<Text style={{ flex: 1 }}>Extra info:</Text> | ||
</Row> | ||
{MenuBarModule.logs.map((log, index) => { | ||
return ( | ||
<DebugLogRow | ||
log={log} | ||
key={log.command + index} | ||
style={{ | ||
paddingHorizontal: 10, | ||
backgroundColor: index % 2 === 1 ? palette['gray']['400'] : palette['gray']['200'], | ||
}} | ||
/> | ||
); | ||
})} | ||
</ScrollView> | ||
); | ||
}; |
This file contains 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 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 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