File size: 8,341 Bytes
68465df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | import {useState, useEffect, useRef} from "react"
import {ScrollView, View, ViewStyle, TextStyle, NativeScrollEvent, NativeSyntheticEvent, Dimensions} from "react-native"
import {Gesture, GestureDetector} from "react-native-gesture-handler"
import Animated, {useSharedValue, useAnimatedStyle, withSpring} from "react-native-reanimated"
import {Button} from "@/components/ignite"
import {Text} from "@/components/ignite/Text"
import {useAppTheme} from "@/contexts/ThemeContext"
import {SETTINGS, useSetting} from "@mentra/engine"
import {ThemedStyle} from "@/theme"
import {useSaferAreaInsets} from "@/contexts/SaferAreaContext"
const TOGGLE_MIN_X = -54
const TOGGLE_MAX_X = 70
const OPEN_MIN_X = 0
const OPEN_PADDING = 80
type ConsoleLogType = "log" | "warn" | "error"
type ConsoleLogEntry = {
type: ConsoleLogType
message: string
timestamp: string
}
export const ConsoleLogger = () => {
const {themed} = useAppTheme()
const [logs, setLogs] = useState<ConsoleLogEntry[]>([])
const [isVisible, setIsVisible] = useState(false)
const scrollViewRef = useRef<ScrollView | null>(null)
const [debugConsole] = useSetting(SETTINGS.debug_console.key)
const consoleOverrideSetup = useRef(false)
const isAtBottom = useRef(true)
const {width, height} = Dimensions.get("window")
const insets = useSaferAreaInsets()
const openRightEdge = width - (insets.right + OPEN_PADDING)
const openBottomEdge = height - (insets.bottom + OPEN_PADDING)
const toggleRightEdge = width - TOGGLE_MAX_X
const toggleBottomEdge = height - (insets.bottom + 80)
// Console window position
const panX = useSharedValue(OPEN_MIN_X)
const panY = useSharedValue(insets.top)
const panStartX = useSharedValue(0)
const panStartY = useSharedValue(0)
// Toggle button position
const toggleX = useSharedValue(toggleRightEdge - 70)
const toggleY = useSharedValue(insets.top)
const toggleStartX = useSharedValue(0)
const toggleStartY = useSharedValue(0)
const panGesture = Gesture.Pan()
.onStart(() => {
panStartX.value = panX.value
panStartY.value = panY.value
})
.onUpdate((event) => {
panX.value = panStartX.value + event.translationX
panY.value = panStartY.value + event.translationY
})
.onEnd(() => {
if (panY.value < insets.top) {
panY.value = withSpring(insets.top)
}
if (panX.value < OPEN_MIN_X) {
panX.value = withSpring(OPEN_MIN_X)
}
if (panX.value > openRightEdge) {
panX.value = withSpring(openRightEdge)
}
if (panY.value > openBottomEdge) {
panY.value = withSpring(openBottomEdge)
}
})
const toggleGesture = Gesture.Pan()
.onStart(() => {
toggleStartX.value = toggleX.value
toggleStartY.value = toggleY.value
})
.onUpdate((event) => {
toggleX.value = toggleStartX.value + event.translationX
toggleY.value = toggleStartY.value + event.translationY
})
.onEnd(() => {
if (toggleY.value < insets.top) {
toggleY.value = withSpring(insets.top)
}
if (toggleX.value < TOGGLE_MIN_X) {
toggleX.value = withSpring(TOGGLE_MIN_X)
}
if (toggleX.value > toggleRightEdge) {
toggleX.value = withSpring(toggleRightEdge)
}
if (toggleY.value > toggleBottomEdge) {
toggleY.value = withSpring(toggleBottomEdge)
}
})
const panAnimatedStyle = useAnimatedStyle(() => ({
transform: [{translateX: panX.value}, {translateY: panY.value}],
}))
const toggleAnimatedStyle = useAnimatedStyle(() => ({
transform: [{translateX: toggleX.value}, {translateY: toggleY.value}],
}))
const handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent
const isBottom = contentOffset.y + layoutMeasurement.height >= contentSize.height - 20
isAtBottom.current = isBottom
}
useEffect(() => {
if (!debugConsole || consoleOverrideSetup.current) {
return
}
const timeoutId = setTimeout(() => {
const originalLog = console.log
const originalWarn = console.warn
const originalError = console.error
const addLog = (type: ConsoleLogType, args: unknown[]) => {
const message = args
.map((arg) => (typeof arg === "object" ? JSON.stringify(arg, null, 2) : String(arg)))
.join(" ")
setTimeout(() => {
setLogs((prev) => {
const newLogs = [
...prev,
{
type,
message,
timestamp: new Date().toLocaleTimeString(),
},
]
return newLogs.slice(-500)
})
}, 1000)
}
console.log = (...args) => {
addLog("log", args)
originalLog(...args)
}
console.warn = (...args) => {
addLog("warn", args)
originalWarn(...args)
}
console.error = (...args) => {
try {
addLog("error", args)
originalError(...args)
} catch (error) {
console.log("Error in console.error", error)
}
}
consoleOverrideSetup.current = true
}, 1000)
return () => {
clearTimeout(timeoutId)
}
}, [debugConsole])
if (!debugConsole) {
return null
}
const handleHide = () => {
setIsVisible(false)
}
if (!isVisible) {
return (
<GestureDetector gesture={toggleGesture}>
<Animated.View className="absolute top-0 left-0 z-1" style={toggleAnimatedStyle}>
<Button text="Show Console" preset="primary" compact onPress={() => setIsVisible(true)} hitSlop={10} />
</Animated.View>
</GestureDetector>
)
}
return (
<Animated.View style={[themed($container), panAnimatedStyle]}>
<GestureDetector gesture={panGesture}>
<View style={themed($header)}>
<Text text={`Console (${logs.length}/500)`} className="text-xs font-bold" />
<View className="flex-row gap-2">
<Button text="Hide" preset="primary" compact onPress={handleHide} />
<Button text="Clear" preset="secondary" compact onPress={() => setLogs([])} />
</View>
</View>
</GestureDetector>
<ScrollView
ref={scrollViewRef}
className="flex"
contentContainerStyle={themed($logContentContainer)}
onScroll={handleScroll}
scrollEventThrottle={16}
onContentSizeChange={() => {
if (isAtBottom.current) {
scrollViewRef.current?.scrollToEnd()
}
}}>
{logs.map((log, index) => (
<View key={index}>
<Text
text={log.message}
className="text-[10px]/3 font-bold color-secondary-foreground font-mono"
style={[log.type === "error" && themed($errorText), log.type === "warn" && themed($warnText)]}
/>
</View>
))}
</ScrollView>
</Animated.View>
)
}
const $container: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
position: "absolute",
zIndex: 1,
left: 0,
right: 0,
height: 300,
width: "90%",
backgroundColor: colors.primary_foreground,
borderWidth: 1,
borderColor: colors.border,
borderRadius: spacing.s6,
})
const $header: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: spacing.s6,
paddingVertical: spacing.s2,
backgroundColor: colors.background,
borderRadius: spacing.s6,
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
borderBottomWidth: 1,
borderBottomColor: colors.border,
borderColor: colors.border,
})
const $logContentContainer: ThemedStyle<ViewStyle> = ({spacing}) => ({
paddingHorizontal: spacing.s2,
paddingBottom: spacing.s3,
paddingTop: spacing.s2,
})
const $errorText: ThemedStyle<TextStyle> = ({colors}) => ({
color: colors.error,
})
const $warnText: ThemedStyle<TextStyle> = ({colors}) => ({
color: colors.warning,
})
|