Skip to content

Commit

Permalink
Tidy colorMap in ChatScreen, add settings TODO.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Jan 30, 2022
1 parent 3b695c5 commit 4965b0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/helpers/useJsonAsyncStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const useMemoisedValue = <T>(value: T) => {
}

// This does not support deep merge, do not use it for deep merging.
// This also requires JSON compatible objects to be passed.
// LOW-TODO: This does not support removing existing keys.
// This function requires JSON compatible objects to be passed.
const useJsonAsyncStorage = <T extends {}>(
name: string,
passedDefaultValue: T
Expand Down
23 changes: 6 additions & 17 deletions src/screens/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ const ChatMessageList = (props: { messages: Message[]; darkMode: boolean }) => {
/>
)
}
const ChatMessageListMemo = React.memo(
ChatMessageList,
(prevProps, nextProps) => prevProps.messages === nextProps.messages
)
const ChatMessageListMemo = React.memo(ChatMessageList) // Shallow prop compare.

const createErrorHandler = (
color: string,
const errorHandler = (
addMessage: (text: MinecraftChat) => void,
translated: string
) => (error: unknown) => {
Expand All @@ -89,7 +85,6 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
connection && connection.connection.options.protocolVersion >= 306 // 16w38a
? 256
: 100
const colorMap = darkMode ? mojangColorMap : lightColorMap
const addMessage = (text: MinecraftChat) =>
setMessages(m => {
const trunc = m.length > 500 ? m.slice(0, 499) : m
Expand All @@ -103,23 +98,18 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
if (!loggedInRef.current && connection.connection.loggedIn) {
setLoggedIn(true)
loggedInRef.current = true
const errorHandler = createErrorHandler(
colorMap.dark_red,
addMessage,
sendMessageErr
)
if (settings.sendJoinMessage) {
connection.connection
.writePacket(
0x03,
concatPacketData([settings.joinMessage.substring(charLimit)])
)
.catch(errorHandler)
.catch(errorHandler(addMessage, sendMessageErr))
}
if (settings.sendSpawnCommand) {
connection.connection
.writePacket(0x03, concatPacketData(['/spawn']))
.catch(errorHandler)
.catch(errorHandler(addMessage, sendMessageErr))
}
} else if (packet.id === 0x0f) {
try {
Expand All @@ -133,15 +123,14 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
addMessage(parseValidJson(chatJson))
}
} catch (e) {
createErrorHandler(colorMap.dark_red, addMessage, parseMessageErr)(e)
errorHandler(addMessage, parseMessageErr)(e)
}
}
})
return () => {
connection.connection.removeAllListeners('packet')
}
}, [
colorMap,
charLimit,
connection,
settings.joinMessage,
Expand Down Expand Up @@ -169,7 +158,7 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
if (trim.startsWith('/')) setCommandHistory(ch => ch.concat([trim]))
connection.connection
.writePacket(0x03, concatPacketData([trim]))
.catch(createErrorHandler(colorMap.dark_red, addMessage, sendMessageErr))
.catch(errorHandler(addMessage, sendMessageErr))
}

if (!connection) return <></> // This should never be hit hopefully.
Expand Down

0 comments on commit 4965b0b

Please sign in to comment.