|
| 1 | +import { |
| 2 | + StyleSheet, |
| 3 | + FlatList, |
| 4 | + TouchableOpacity, |
| 5 | + Text, |
| 6 | + View, |
| 7 | +} from 'react-native'; |
| 8 | + |
| 9 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 10 | +import ReactNativeBrownfield from '@callstack/react-native-brownfield'; |
| 11 | +import type { MessageEvent } from '@callstack/react-native-brownfield'; |
| 12 | + |
| 13 | +type Message = { |
| 14 | + id: string; |
| 15 | + text: string; |
| 16 | + from: 'native' | 'rn'; |
| 17 | + timestamp: number; |
| 18 | +}; |
| 19 | + |
| 20 | +export default function PostMessageScreen() { |
| 21 | + const [messages, setMessages] = useState<Message[]>([]); |
| 22 | + const flatListRef = useRef<FlatList<Message>>(null); |
| 23 | + |
| 24 | + const messageCounterRef = useRef(0); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const sub = ReactNativeBrownfield.onMessage((event: MessageEvent) => { |
| 28 | + const data = event.data as { text?: string }; |
| 29 | + setMessages((prev) => [ |
| 30 | + ...prev, |
| 31 | + { |
| 32 | + id: String(++messageCounterRef.current), |
| 33 | + text: data?.text ?? JSON.stringify(event.data), |
| 34 | + from: 'native', |
| 35 | + timestamp: Date.now(), |
| 36 | + }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + return () => sub.remove(); |
| 40 | + }, []); |
| 41 | + |
| 42 | + const sendMessage = useCallback(() => { |
| 43 | + const msg = { |
| 44 | + text: `Hello from TesterIntegrated! (#${++messageCounterRef.current})`, |
| 45 | + timestamp: Date.now(), |
| 46 | + }; |
| 47 | + ReactNativeBrownfield.postMessage(msg); |
| 48 | + setMessages((prev) => [ |
| 49 | + ...prev, |
| 50 | + { |
| 51 | + id: String(messageCounterRef.current), |
| 52 | + text: msg.text, |
| 53 | + from: 'rn', |
| 54 | + timestamp: msg.timestamp, |
| 55 | + }, |
| 56 | + ]); |
| 57 | + }, []); |
| 58 | + |
| 59 | + return ( |
| 60 | + <View style={styles.messageSection}> |
| 61 | + <TouchableOpacity |
| 62 | + style={styles.sendButton} |
| 63 | + onPress={sendMessage} |
| 64 | + activeOpacity={0.8} |
| 65 | + > |
| 66 | + <Text style={styles.sendButtonText}>Send message to Native</Text> |
| 67 | + </TouchableOpacity> |
| 68 | + |
| 69 | + <FlatList |
| 70 | + data={messages} |
| 71 | + keyExtractor={(item) => `message-${item.id}`} |
| 72 | + renderItem={({ item }) => <Text>{item.text}</Text>} |
| 73 | + style={styles.messageList} |
| 74 | + contentContainerStyle={styles.messageListContent} |
| 75 | + inverted={true} // ensure newest messages are at the top |
| 76 | + onContentSizeChange={() => { |
| 77 | + flatListRef.current?.scrollToEnd({ animated: true }); |
| 78 | + }} |
| 79 | + ref={flatListRef} |
| 80 | + /> |
| 81 | + </View> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +const styles = StyleSheet.create({ |
| 86 | + messageSection: { |
| 87 | + flex: 1, |
| 88 | + width: '100%', |
| 89 | + padding: 12, |
| 90 | + paddingHorizontal: 20, |
| 91 | + }, |
| 92 | + sendButton: { |
| 93 | + paddingVertical: 12, |
| 94 | + paddingHorizontal: 20, |
| 95 | + borderRadius: 10, |
| 96 | + alignItems: 'center', |
| 97 | + marginBottom: 10, |
| 98 | + backgroundColor: '#4F8EF7', |
| 99 | + }, |
| 100 | + sendButtonText: { |
| 101 | + fontWeight: '700', |
| 102 | + fontSize: 15, |
| 103 | + color: '#fff', |
| 104 | + }, |
| 105 | + messageList: { |
| 106 | + flex: 1, |
| 107 | + }, |
| 108 | + messageListContent: { |
| 109 | + paddingBottom: 8, |
| 110 | + }, |
| 111 | +}); |
0 commit comments