|
1 | | -import { useMemo } from 'react'; |
| 1 | +import { memo, useMemo } from 'react'; |
2 | 2 | import { StyleSheet, Text, View } from 'react-native'; |
3 | 3 | import { URL } from 'react-native-url-polyfill'; |
| 4 | +import { NETWORK_ITEM_HEIGHT } from '../../../core/constants'; |
4 | 5 | import { |
5 | 6 | formatRequestDuration, |
6 | 7 | formatRequestMethod, |
7 | 8 | formatRequestStatusCode, |
8 | 9 | } from '../../../core/utils'; |
9 | 10 | import colors from '../../../theme/colors'; |
10 | 11 | import type { HttpRequest, NetworkRequest } from '../../../types'; |
| 12 | +import Divider from '../common/Divider'; |
11 | 13 | import Touchable from '../common/Touchable'; |
12 | 14 |
|
13 | 15 | interface NetworkPanelItemProps { |
@@ -39,76 +41,85 @@ const getMethodColor = (method: string) => { |
39 | 41 | } |
40 | 42 | }; |
41 | 43 |
|
42 | | -export default function NetworkPanelItem({ |
43 | | - method, |
44 | | - name, |
45 | | - startTime, |
46 | | - endTime, |
47 | | - status, |
48 | | - onPress, |
49 | | -}: NetworkPanelItemProps) { |
50 | | - const duration = formatRequestDuration(startTime, endTime); |
51 | | - const requestMethod = formatRequestMethod(method); |
52 | | - const requestStatusCode = formatRequestStatusCode(status); |
53 | | - const isRequestFailed = Number.isInteger(status) && status! >= 400 && status! < 600; |
54 | | - const textStyle = [styles.text, isRequestFailed && styles.failedText]; |
| 44 | +const NetworkPanelItem = memo<NetworkPanelItemProps>( |
| 45 | + ({ method, name, startTime, endTime, status, onPress }) => { |
| 46 | + const duration = formatRequestDuration(startTime, endTime); |
| 47 | + const requestMethod = formatRequestMethod(method); |
| 48 | + const requestStatusCode = formatRequestStatusCode(status); |
| 49 | + const isRequestFailed = Number.isInteger(status) && status! >= 400 && status! < 600; |
| 50 | + const textStyle = [styles.text, isRequestFailed && styles.failedText]; |
55 | 51 |
|
56 | | - const requestPath = useMemo(() => { |
57 | | - if (!name) return '[failed]'; |
| 52 | + const requestPath = useMemo(() => { |
| 53 | + if (!name) return '[failed]'; |
58 | 54 |
|
59 | | - try { |
60 | | - const url = new URL(name); |
61 | | - const suffixUrl = url.pathname + url.search; |
| 55 | + try { |
| 56 | + const url = new URL(name); |
| 57 | + const suffixUrl = url.pathname + url.search; |
62 | 58 |
|
63 | | - if (suffixUrl === '/') return url.host; |
64 | | - return suffixUrl; |
65 | | - } catch (error) { |
66 | | - return name; |
67 | | - } |
68 | | - }, [name]); |
| 59 | + if (suffixUrl === '/') return url.host; |
| 60 | + return suffixUrl; |
| 61 | + } catch (error) { |
| 62 | + return name; |
| 63 | + } |
| 64 | + }, [name]); |
69 | 65 |
|
70 | | - return ( |
71 | | - <Touchable onPress={onPress} style={styles.container}> |
72 | | - <View style={styles.column}> |
73 | | - <Text |
74 | | - numberOfLines={1} |
75 | | - style={[ |
76 | | - styles.text, |
77 | | - styles.methodText, |
78 | | - { backgroundColor: getMethodColor(requestMethod) }, |
79 | | - ]} |
80 | | - > |
81 | | - {requestMethod} |
82 | | - </Text> |
83 | | - </View> |
| 66 | + return ( |
| 67 | + <View style={styles.container}> |
| 68 | + <Touchable onPress={onPress} style={styles.wrapper}> |
| 69 | + <View style={styles.column}> |
| 70 | + <Text |
| 71 | + numberOfLines={1} |
| 72 | + style={[ |
| 73 | + styles.text, |
| 74 | + styles.methodText, |
| 75 | + { backgroundColor: getMethodColor(requestMethod) }, |
| 76 | + ]} |
| 77 | + > |
| 78 | + {requestMethod} |
| 79 | + </Text> |
| 80 | + </View> |
84 | 81 |
|
85 | | - <View style={[styles.column, styles.pathColumn]}> |
86 | | - <Text numberOfLines={1} style={textStyle}> |
87 | | - {requestPath} |
88 | | - </Text> |
89 | | - </View> |
| 82 | + <View style={[styles.column, styles.pathColumn]}> |
| 83 | + <Text numberOfLines={1} style={textStyle}> |
| 84 | + {requestPath} |
| 85 | + </Text> |
| 86 | + </View> |
90 | 87 |
|
91 | | - <View style={[styles.column, styles.durationColumn]}> |
92 | | - <Text numberOfLines={1} style={textStyle}> |
93 | | - {duration} |
94 | | - </Text> |
95 | | - </View> |
| 88 | + <View style={[styles.column, styles.durationColumn]}> |
| 89 | + <Text numberOfLines={1} style={textStyle}> |
| 90 | + {duration} |
| 91 | + </Text> |
| 92 | + </View> |
96 | 93 |
|
97 | | - <View style={styles.column}> |
98 | | - <Text numberOfLines={1} style={textStyle}> |
99 | | - {requestStatusCode} |
100 | | - </Text> |
| 94 | + <View style={styles.column}> |
| 95 | + <Text numberOfLines={1} style={textStyle}> |
| 96 | + {requestStatusCode} |
| 97 | + </Text> |
| 98 | + </View> |
| 99 | + </Touchable> |
| 100 | + <Divider type="horizontal" /> |
101 | 101 | </View> |
102 | | - </Touchable> |
103 | | - ); |
104 | | -} |
| 102 | + ); |
| 103 | + }, |
| 104 | + (prevProps, nextProps) => { |
| 105 | + return ( |
| 106 | + prevProps.method === nextProps.method && |
| 107 | + prevProps.name === nextProps.name && |
| 108 | + prevProps.startTime === nextProps.startTime && |
| 109 | + prevProps.endTime === nextProps.endTime && |
| 110 | + prevProps.status === nextProps.status |
| 111 | + ); |
| 112 | + }, |
| 113 | +); |
105 | 114 |
|
106 | 115 | const styles = StyleSheet.create({ |
107 | 116 | container: { |
108 | 117 | flex: 1, |
| 118 | + }, |
| 119 | + wrapper: { |
109 | 120 | flexDirection: 'row', |
110 | 121 | alignItems: 'center', |
111 | | - paddingVertical: 8, |
| 122 | + height: NETWORK_ITEM_HEIGHT, |
112 | 123 | columnGap: 8, |
113 | 124 | }, |
114 | 125 | pathColumn: { |
@@ -138,3 +149,5 @@ const styles = StyleSheet.create({ |
138 | 149 | fontWeight: '600', |
139 | 150 | }, |
140 | 151 | }); |
| 152 | + |
| 153 | +export default NetworkPanelItem; |
0 commit comments