Skip to content

Commit fd9739a

Browse files
committed
Display action bars 🎉
1 parent bd62c78 commit fd9739a

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

src/components/chat/ActionBar.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react'
2+
import { StyleSheet, View } from 'react-native'
3+
import {
4+
ChatToJsx,
5+
type ClickEvent,
6+
type ColorMap,
7+
type MinecraftChat
8+
} from '../../minecraft/chatToJsx'
9+
import Text from '../Text'
10+
import useDarkMode from '../../context/useDarkMode'
11+
12+
const ActionBar: React.FC<{
13+
content: MinecraftChat
14+
colorMap: ColorMap
15+
onClickEvent: (ce: ClickEvent) => void
16+
}> = props => {
17+
return (
18+
<View
19+
style={[
20+
useDarkMode() ? styles.actionBarDark : styles.actionBarLight,
21+
styles.actionBar
22+
]}
23+
>
24+
<ChatToJsx
25+
chat={props.content}
26+
component={Text}
27+
colorMap={props.colorMap}
28+
onClickEvent={props.onClickEvent}
29+
/>
30+
</View>
31+
)
32+
}
33+
34+
const styles = StyleSheet.create({
35+
actionBar: {
36+
padding: 8,
37+
alignItems: 'center',
38+
justifyContent: 'center'
39+
},
40+
actionBarLight: { backgroundColor: '#fff' },
41+
actionBarDark: { backgroundColor: '#242424' }
42+
})
43+
44+
export default ActionBar

src/components/chat/ChatMessageList.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export interface Message {
1616
const MessageRenderer = (props: {
1717
item: Message
1818
colorMap: ColorMap
19-
clickEventHandler: (event: ClickEvent) => void
19+
onClickEvent: (event: ClickEvent) => void
2020
}): JSX.Element => (
2121
<ChatToJsx
2222
chat={props.item.text}
2323
component={Text}
2424
colorMap={props.colorMap}
25-
clickEventHandler={props.clickEventHandler}
25+
onClickEvent={props.onClickEvent}
2626
/>
2727
)
2828

@@ -31,13 +31,13 @@ const MessageRendererMemo = React.memo(
3131
(prev, next) =>
3232
prev.item.key === next.item.key &&
3333
prev.colorMap === next.colorMap &&
34-
prev.clickEventHandler === next.clickEventHandler
34+
prev.onClickEvent === next.onClickEvent
3535
)
3636

3737
const ChatMessageList = (props: {
3838
messages: Message[]
3939
colorMap: ColorMap
40-
clickEventHandler: (ce: ClickEvent) => void
40+
onClickEvent: (ce: ClickEvent) => void
4141
}): JSX.Element => {
4242
// If colorMap/clickEventHandler changes, this will change and cause a re-render.
4343
// If messages changes, FlatList will execute this function for all messages, and
@@ -47,10 +47,10 @@ const ChatMessageList = (props: {
4747
<MessageRendererMemo
4848
item={item}
4949
colorMap={props.colorMap}
50-
clickEventHandler={props.clickEventHandler}
50+
onClickEvent={props.onClickEvent}
5151
/>
5252
),
53-
[props.colorMap, props.clickEventHandler]
53+
[props.colorMap, props.onClickEvent]
5454
)
5555
return (
5656
<FlatList

src/minecraft/chatToJsx.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ const parseChatToJsx = (
253253
chat: MinecraftChat,
254254
Component: React.ComponentType<TextProps>,
255255
colorMap: ColorMap,
256-
clickEventHandler: (clickEvent: ClickEvent) => void = () => {},
256+
handleClickEvent: (clickEvent: ClickEvent) => void = () => {},
257257
componentProps?: Record<string, unknown>,
258258
trim = false
259259
): JSX.Element => {
@@ -280,7 +280,7 @@ const parseChatToJsx = (
280280
key={i}
281281
style={style}
282282
selectable
283-
onPress={ce ? () => clickEventHandler(ce) : undefined}
283+
onPress={ce ? () => handleClickEvent(ce) : undefined}
284284
onLongPress={() => {}}
285285
>
286286
{c.text ? c.text : ''}
@@ -297,14 +297,14 @@ export const ChatToJsx = (props: {
297297
component: React.ComponentType<TextProps>
298298
colorMap: ColorMap
299299
componentProps?: Record<string, unknown>
300-
clickEventHandler?: (clickEvent: ClickEvent) => void
300+
onClickEvent?: (clickEvent: ClickEvent) => void
301301
trim?: boolean
302302
}): JSX.Element =>
303303
parseChatToJsx(
304304
props.chat ?? { text: '' },
305305
props.component,
306306
props.colorMap,
307-
props.clickEventHandler,
307+
props.onClickEvent,
308308
props.componentProps,
309309
props.trim
310310
)

src/screens/ChatScreen.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { ConnectionState } from '../minecraft/connection'
4545
import { makeChatMessagePacket } from '../minecraft/packets/chat'
4646
import TextField from '../components/TextField'
4747
import Text from '../components/Text'
48+
import ActionBar from '../components/chat/ActionBar'
4849
import ChatMessageList, {
4950
type Message
5051
} from '../components/chat/ChatMessageList'
@@ -71,7 +72,7 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
7172

7273
// TODO: Show command history.
7374
const [, setCommandHistory] = useState<string[]>([])
74-
const [, setActionBar] = useState<MinecraftChat | null>(null)
75+
const [actionBar, setActionBar] = useState<MinecraftChat | null>(null)
7576
const [messages, setMessages] = useState<Message[]>([])
7677
const [loading, setLoading] = useState('Connecting to server...')
7778
const [message, setMessage] = useState('')
@@ -283,10 +284,17 @@ const ChatScreen = ({ navigation, route }: Props): JSX.Element => {
283284
)}
284285
{!loading && connection && (
285286
<>
287+
{actionBar && (
288+
<ActionBar
289+
content={actionBar}
290+
colorMap={darkMode ? mojangColorMap : lightColorMap}
291+
onClickEvent={handleClickEvent}
292+
/>
293+
)}
286294
<ChatMessageList
287295
messages={messages}
288296
colorMap={darkMode ? mojangColorMap : lightColorMap}
289-
clickEventHandler={handleClickEvent}
297+
onClickEvent={handleClickEvent}
290298
/>
291299
<View style={darkMode ? styles.textAreaDark : styles.textArea}>
292300
<TextField

0 commit comments

Comments
 (0)