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([]) const [isVisible, setIsVisible] = useState(false) const scrollViewRef = useRef(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) => { 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 (