|
| 1 | +import format from 'date-fns/format'; |
| 2 | +import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; |
| 3 | +import { Animated, ScrollView, Text, TouchableOpacity, View, useWindowDimensions } from 'react-native'; |
| 4 | + |
| 5 | +interface Log { |
| 6 | + type: 'log' | 'warn' | 'error' | 'info'; |
| 7 | + data: string; |
| 8 | + timestamp: number; |
| 9 | +} |
| 10 | + |
| 11 | +const TypeColor: Record<Log['type'], string> = { |
| 12 | + log: '#2d2d2d', |
| 13 | + warn: '#ffc443', |
| 14 | + error: '#ff4332', |
| 15 | + info: '#0773ff', |
| 16 | +}; |
| 17 | + |
| 18 | +const LogView = () => { |
| 19 | + const [logs, setLogs] = useState<Log[]>([]); |
| 20 | + const [isCollapsed, setIsCollapsed] = useState(false); |
| 21 | + |
| 22 | + useLayoutEffect(() => { |
| 23 | + (['log', 'warn', 'error', 'info'] as const).forEach((type) => { |
| 24 | + const origin = console[type]; |
| 25 | + |
| 26 | + console[type] = function (...args: unknown[]) { |
| 27 | + origin(...args); |
| 28 | + setLogs((prev) => [ |
| 29 | + ...prev, |
| 30 | + { |
| 31 | + type, |
| 32 | + data: args.join(','), |
| 33 | + timestamp: Date.now(), |
| 34 | + }, |
| 35 | + ]); |
| 36 | + }; |
| 37 | + }); |
| 38 | + }, []); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + Animated.timing(animated, { toValue: isCollapsed ? 0 : 1, duration: 250, useNativeDriver: false }).start(); |
| 42 | + }, [isCollapsed]); |
| 43 | + |
| 44 | + const animated = useRef(new Animated.Value(0)).current; |
| 45 | + |
| 46 | + const { width } = useWindowDimensions(); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Animated.View |
| 50 | + style={{ |
| 51 | + position: 'absolute', |
| 52 | + zIndex: 999, |
| 53 | + backgroundColor: '#969696', |
| 54 | + width: animated.interpolate({ extrapolate: 'clamp', inputRange: [0, 1], outputRange: [70, width] }), |
| 55 | + height: animated.interpolate({ extrapolate: 'clamp', inputRange: [0, 1], outputRange: [70, width] }), |
| 56 | + bottom: animated.interpolate({ extrapolate: 'clamp', inputRange: [0, 1], outputRange: [150, 0] }), |
| 57 | + right: animated.interpolate({ extrapolate: 'clamp', inputRange: [0, 1], outputRange: [15, 0] }), |
| 58 | + }} |
| 59 | + > |
| 60 | + <TouchableOpacity |
| 61 | + style={{ paddingHorizontal: 8, paddingVertical: 2, backgroundColor: 'orange', alignItems: 'center' }} |
| 62 | + onPress={() => setIsCollapsed((prev) => !prev)} |
| 63 | + > |
| 64 | + <Text>{isCollapsed ? 'Expand' : 'Collapse'}</Text> |
| 65 | + </TouchableOpacity> |
| 66 | + <ScrollView> |
| 67 | + {logs.map(({ type, data, timestamp }, idx) => { |
| 68 | + return ( |
| 69 | + <View |
| 70 | + key={idx} |
| 71 | + style={{ marginBottom: 1, backgroundColor: TypeColor[type], paddingHorizontal: 4, paddingVertical: 2 }} |
| 72 | + > |
| 73 | + <Text style={{ color: 'white' }}> |
| 74 | + {format(timestamp, 'p')} / {data} |
| 75 | + </Text> |
| 76 | + </View> |
| 77 | + ); |
| 78 | + })} |
| 79 | + </ScrollView> |
| 80 | + </Animated.View> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +export default LogView; |
0 commit comments