@@ -16,15 +16,17 @@ import {
16
16
enderChatPrefix ,
17
17
sendMessageError
18
18
} from './packetHandler'
19
- import { createConnection } from './sessionBuilder'
19
+ import { getSession , createConnection } from './sessionBuilder'
20
20
import { RootStackParamList } from '../../App'
21
21
import globalStyle from '../../globalStyle'
22
22
import useDarkMode from '../../context/useDarkMode'
23
23
import AccountsContext from '../../context/accountsContext'
24
24
import ServersContext from '../../context/serversContext'
25
25
import useSessionStore from '../../context/sessionStore'
26
26
import SettingsContext from '../../context/settingsContext'
27
- import ConnectionContext , { Connection } from '../../context/connectionContext'
27
+ import ConnectionContext , {
28
+ DisconnectReason
29
+ } from '../../context/connectionContext'
28
30
import {
29
31
ChatToJsx ,
30
32
mojangColorMap ,
@@ -85,9 +87,6 @@ const handleError =
85
87
addMessage ( enderChatPrefix + translated )
86
88
}
87
89
88
- const isConnection = ( connection : any ) : connection is Connection =>
89
- ! ! ( connection as Connection ) . connection
90
-
91
90
// TODO: Ability to copy text.
92
91
const ChatScreen = ( { navigation, route } : Props ) => {
93
92
const darkMode = useDarkMode ( )
@@ -97,25 +96,27 @@ const ChatScreen = ({ navigation, route }: Props) => {
97
96
const { connection, setConnection, setDisconnectReason } =
98
97
useContext ( ConnectionContext )
99
98
const { sessions, setSession } = useSessionStore ( )
99
+
100
100
// TODO: Show command history.
101
101
const [ , setCommandHistory ] = useState < string [ ] > ( [ ] )
102
102
const [ messages , setMessages ] = useState < Message [ ] > ( [ ] )
103
- const [ loggedIn , setLoggedIn ] = useState ( false )
103
+ const [ loading , setLoading ] = useState ( 'Connecting to server...' )
104
104
const [ message , setMessage ] = useState ( '' )
105
+
105
106
const messagesBufferRef = useRef < Message [ ] > ( [ ] )
106
107
const healthRef = useRef < number | null > ( null )
107
108
const statusRef = useRef < Status > ( connection ? 'CONNECTING' : 'OPENING' )
108
109
const idRef = useRef ( 0 )
109
110
110
- const charLimit =
111
- connection && connection . connection . options . protocolVersion >= 306 // 16w38a
112
- ? 256
113
- : 100
111
+ const { version, serverName } = route . params
112
+ const charLimit = version >= 306 /* 16w38a */ ? 256 : 100
113
+
114
114
const addMessage = ( text : MinecraftChat ) =>
115
115
messagesBufferRef . current . unshift ( { key : idRef . current ++ , text } )
116
- const closeChatScreen = ( ) => {
117
- if ( navigation . canGoBack ( ) && statusRef . current !== 'CLOSED' ) {
118
- navigation . goBack ( )
116
+ const closeChatScreen = ( reason ?: DisconnectReason ) => {
117
+ if ( statusRef . current !== 'CLOSED' ) {
118
+ if ( navigation . canGoBack ( ) ) navigation . goBack ( )
119
+ if ( reason ) setDisconnectReason ( reason )
119
120
}
120
121
}
121
122
@@ -149,36 +150,45 @@ const ChatScreen = ({ navigation, route }: Props) => {
149
150
useEffect ( ( ) => {
150
151
if ( statusRef . current === 'OPENING' ) {
151
152
statusRef . current = 'CONNECTING'
152
- createConnection (
153
- route . params . serverName ,
154
- route . params . version ,
155
- servers ,
156
- settings ,
157
- accounts ,
158
- sessions ,
159
- setSession ,
160
- setAccounts ,
161
- setConnection ,
162
- setDisconnectReason ,
163
- closeChatScreen
164
- )
165
- . then ( conn => {
166
- if ( statusRef . current !== 'CLOSED' ) {
167
- if ( isConnection ( conn ) ) setConnection ( conn )
168
- else {
169
- closeChatScreen ( )
170
- setDisconnectReason ( conn )
153
+ ; ( async ( ) => {
154
+ const session = await getSession (
155
+ version ,
156
+ accounts ,
157
+ sessions ,
158
+ setSession ,
159
+ setLoading ,
160
+ setAccounts
161
+ )
162
+ if ( typeof session === 'string' ) {
163
+ closeChatScreen ( { server : serverName , reason : session } )
164
+ } else if ( statusRef . current !== 'CLOSED' ) {
165
+ setLoading ( 'Connecting to server...' )
166
+ const conn = await createConnection (
167
+ serverName ,
168
+ version ,
169
+ servers ,
170
+ session ,
171
+ settings ,
172
+ accounts ,
173
+ setConnection ,
174
+ closeChatScreen
175
+ )
176
+ if ( ( statusRef . current as 'CLOSED' | 'CONNECTING' ) !== 'CLOSED' ) {
177
+ if ( typeof conn === 'string' ) {
178
+ closeChatScreen ( { server : serverName , reason : conn } )
179
+ } else {
180
+ setConnection ( conn )
181
+ setLoading ( 'Logging in...' )
171
182
}
172
- } else if ( isConnection ( conn ) ) conn . connection . close ( ) // No memory leaky
173
- } )
174
- . catch ( e => {
175
- console . error ( e )
176
- closeChatScreen ( )
177
- setDisconnectReason ( {
178
- server : route . params . serverName ,
179
- reason : 'An error occurred resolving the server hostname!'
180
- } )
181
- } )
183
+ } else if ( typeof conn !== 'string' ) conn . connection . close ( )
184
+ }
185
+ } ) ( ) . catch ( err => {
186
+ console . error ( err )
187
+ if ( statusRef . current !== 'CLOSED' ) {
188
+ const reason = 'An unknown error occurred!\n' + err
189
+ closeChatScreen ( { server : serverName , reason } )
190
+ }
191
+ } )
182
192
}
183
193
} )
184
194
@@ -190,7 +200,7 @@ const ChatScreen = ({ navigation, route }: Props) => {
190
200
packetHandler (
191
201
healthRef ,
192
202
statusRef ,
193
- setLoggedIn ,
203
+ setLoading ,
194
204
connection . connection ,
195
205
addMessage ,
196
206
settings . joinMessage ,
@@ -276,7 +286,7 @@ const ChatScreen = ({ navigation, route }: Props) => {
276
286
onPress = { ( ) => navigation . push ( 'Settings' ) }
277
287
/>
278
288
</ View >
279
- { ( ! loggedIn || ! connection ) && (
289
+ { ( loading || ! connection ) && (
280
290
< View style = { styles . loadingScreen } >
281
291
< ActivityIndicator
282
292
color = '#00aaff'
@@ -285,10 +295,12 @@ const ChatScreen = ({ navigation, route }: Props) => {
285
295
default : 'large'
286
296
} ) }
287
297
/>
288
- < Text style = { styles . loadingScreenText } > Connecting...</ Text >
298
+ < Text style = { styles . loadingScreenText } >
299
+ { loading || 'Connecting to server...' }
300
+ </ Text >
289
301
</ View >
290
302
) }
291
- { loggedIn && connection && (
303
+ { ! loading && connection && (
292
304
< >
293
305
< ChatMessageListMemo
294
306
messages = { messages }
0 commit comments