@@ -6,13 +6,11 @@ import React, {
6
6
useState
7
7
} from 'react'
8
8
import {
9
- FlatList ,
10
9
StyleSheet ,
11
10
View ,
12
11
ActivityIndicator ,
13
12
Platform ,
14
- Linking ,
15
- type ListRenderItem
13
+ Linking
16
14
} from 'react-native'
17
15
import Ionicons from 'react-native-vector-icons/Ionicons'
18
16
import Clipboard from '@react-native-clipboard/clipboard'
@@ -22,89 +20,37 @@ import {
22
20
packetHandler ,
23
21
enderChatPrefix ,
24
22
sendMessageError
25
- } from './packetHandler'
26
- import { getSession , createConnection } from './sessionBuilder'
27
- import { type RootStackParamList } from '../../App'
28
- import globalStyle from '../../globalStyle'
29
- import useDarkMode from '../../context/useDarkMode'
30
- import AccountsContext from '../../context/accountsContext'
31
- import ServersContext from '../../context/serversContext'
32
- import useSessionStore from '../../context/sessionStore'
33
- import SettingsContext from '../../context/settingsContext'
23
+ } from '../utilities/connection/packetHandler'
24
+ import {
25
+ getSession ,
26
+ createConnection
27
+ } from '../utilities/connection/connectionBuilder'
28
+ import { type RootStackParamList } from '../App'
29
+ import globalStyle from '../globalStyle'
30
+ import useDarkMode from '../context/useDarkMode'
31
+ import AccountsContext from '../context/accountsContext'
32
+ import ServersContext from '../context/serversContext'
33
+ import useSessionStore from '../context/sessionStore'
34
+ import SettingsContext from '../context/settingsContext'
34
35
import ConnectionContext , {
35
36
type DisconnectReason
36
- } from '../../ context/connectionContext'
37
+ } from '../context/connectionContext'
37
38
import {
38
- ChatToJsx ,
39
39
mojangColorMap ,
40
40
lightColorMap ,
41
41
type MinecraftChat ,
42
- type ClickEvent ,
43
- type ColorMap
44
- } from '../../minecraft/chatToJsx'
45
- import { makeChatMessagePacket } from '../../minecraft/packets/chat'
46
- import TextField from '../../components/TextField'
47
- import Text from '../../components/Text'
42
+ type ClickEvent
43
+ } from '../minecraft/chatToJsx'
44
+ import { makeChatMessagePacket } from '../minecraft/packets/chat'
45
+ import TextField from '../components/TextField'
46
+ import Text from '../components/Text'
47
+ import ChatMessageList , {
48
+ type Message
49
+ } from '../components/chat/ChatMessageList'
48
50
49
51
type Props = NativeStackScreenProps < RootStackParamList , 'Chat' >
50
52
51
- export type Status = 'OPENING' | 'CONNECTING' | 'CONNECTED' | 'CLOSED'
52
-
53
- interface Message {
54
- key : number
55
- text : MinecraftChat
56
- }
57
-
58
- const ItemRenderer = ( props : {
59
- item : Message
60
- colorMap : ColorMap
61
- clickEventHandler : ( event : ClickEvent ) => void
62
- } ) : JSX . Element => (
63
- < ChatToJsx
64
- chat = { props . item . text }
65
- component = { Text }
66
- colorMap = { props . colorMap }
67
- clickEventHandler = { props . clickEventHandler }
68
- />
69
- )
70
-
71
- const ItemRendererMemo = React . memo (
72
- ItemRenderer ,
73
- ( prev , next ) =>
74
- prev . item . key === next . item . key &&
75
- prev . colorMap === next . colorMap &&
76
- prev . clickEventHandler === next . clickEventHandler
77
- )
78
-
79
- const ChatMessageList = ( props : {
80
- messages : Message [ ]
81
- colorMap : ColorMap
82
- clickEventHandler : ( ce : ClickEvent ) => void
83
- } ) : JSX . Element => {
84
- // If colorMap/clickEventHandler changes, this will change and cause a re-render.
85
- // If messages changes, FlatList will execute this function for all messages, and
86
- // ItemRendererMemo will check if props have changed instead of this useCallback.
87
- const renderItem = useCallback < ListRenderItem < Message > > (
88
- ( { item } ) => (
89
- < ItemRendererMemo
90
- item = { item }
91
- colorMap = { props . colorMap }
92
- clickEventHandler = { props . clickEventHandler }
93
- />
94
- ) ,
95
- [ props . colorMap , props . clickEventHandler ]
96
- )
97
- return (
98
- < FlatList
99
- inverted
100
- data = { props . messages }
101
- style = { styles . chatArea }
102
- contentContainerStyle = { styles . chatAreaScrollView }
103
- renderItem = { renderItem }
104
- />
105
- )
106
- }
107
-
53
+ export type Status = 'CONNECTING' | 'CONNECTED' | 'CLOSED'
108
54
const handleError =
109
55
( addMessage : ( text : MinecraftChat ) => void , translated : string ) =>
110
56
( error : unknown ) => {
@@ -130,7 +76,7 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
130
76
131
77
const messagesBufferRef = useRef < Message [ ] > ( [ ] )
132
78
const healthRef = useRef < number | null > ( null )
133
- const statusRef = useRef < Status > ( connection ? 'CONNECTING ' : 'OPENING ' )
79
+ const statusRef = useRef < Status > ( connection ? 'CONNECTED ' : 'CONNECTING ' )
134
80
const idRef = useRef ( 0 )
135
81
136
82
const { version, serverName } = route . params
@@ -173,8 +119,8 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
173
119
174
120
// Create connection useEffect.
175
121
useEffect ( ( ) => {
176
- if ( statusRef . current === 'OPENING ' ) {
177
- statusRef . current = 'CONNECTING '
122
+ if ( statusRef . current === 'CONNECTING ' ) {
123
+ statusRef . current = 'CONNECTED '
178
124
; ( async ( ) => {
179
125
const session = await getSession (
180
126
version ,
@@ -198,7 +144,7 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
198
144
setConnection ,
199
145
closeChatScreen
200
146
)
201
- if ( ( statusRef . current as 'CLOSED' | 'CONNECTING' ) !== 'CLOSED' ) {
147
+ if ( ( statusRef . current as Status ) !== 'CLOSED' ) {
202
148
if ( typeof conn === 'string' ) {
203
149
closeChatScreen ( { server : serverName , reason : conn } )
204
150
} else {
@@ -224,7 +170,6 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
224
170
'packet' ,
225
171
packetHandler (
226
172
healthRef ,
227
- statusRef ,
228
173
setLoading ,
229
174
connection ,
230
175
addMessage ,
@@ -358,8 +303,6 @@ const styles = StyleSheet.create({
358
303
backButton : { marginRight : 8 } ,
359
304
backButtonIcon : { marginRight : 0 } ,
360
305
sendButtonIcon : { marginRight : 0 , marginLeft : 4 } ,
361
- chatArea : { padding : 8 , flex : 1 } ,
362
- chatAreaScrollView : { paddingBottom : 16 } ,
363
306
loadingScreen : { flex : 1 , alignItems : 'center' , justifyContent : 'center' } ,
364
307
loadingScreenText : { paddingTop : 24 , fontSize : 20 } ,
365
308
textAreaDark : {
0 commit comments