/
MessageText.tsx
174 lines (161 loc) · 5.09 KB
/
MessageText.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { useCallback, useContext, useEffect, useState } from "react"
import { Bubble } from "react-native-gifted-chat"
import { EnhancedIMessage } from "../../utils"
import { Linking, Platform, View } from "react-native"
import { Quote } from "../quote"
import { Text } from "../../ui-components"
import { Message, MixedTextTypedElement } from "@pubnub/chat"
import { ChatContext } from "../../context"
import { useNavigation } from "@react-navigation/native"
type MessageTextProps = {
onGoToMessage: (message: Message) => void
messageProps: Bubble<EnhancedIMessage>["props"]
}
export function MessageText({ onGoToMessage, messageProps }: MessageTextProps) {
const { chat, setCurrentChannel } = useContext(ChatContext)
const navigation = useNavigation()
const [imageSrc, setImageSrc] = useState("")
useEffect(() => {
const files = messageProps.currentMessage?.originalPnMessage.files
if (!files || !files.length || files[0].type !== "image/jpeg" || !chat) {
return
}
chat.sdk
.downloadFile({
channel: messageProps.currentMessage?.originalPnMessage.channelId,
id: files[0].id,
name: files[0].name,
})
.then(async (fileParts) => {
setImageSrc(URL.createObjectURL(await fileParts.toFile()))
})
.catch((err) => {
setImageSrc(files[0].url)
})
}, [])
const openLink = (link: string) => {
Linking.openURL(link)
}
async function openChannel(channelId: string) {
if (!chat) return
const channel = await chat.getChannel(channelId)
if (!channel) {
alert("This channel no longer exists.")
return
}
navigation.pop()
setCurrentChannel(channel)
navigation.navigate("Chat")
}
const renderEmojis = useCallback(() => {
if (!messageProps.currentMessage?.originalPnMessage.reactions) {
return null
}
return (
<View style={{ flexDirection: "row", position: "absolute", right: 0, bottom: -20 }}>
{Object.keys(messageProps.currentMessage?.originalPnMessage.reactions).map((key, index) => (
<Text key={String(index)}>{key}</Text>
))}
</View>
)
}, [messageProps.currentMessage?.originalPnMessage.reactions])
const renderMessagePart = useCallback(
(messagePart: MixedTextTypedElement, index: number, userId: string | number) => {
// TODO make it look nice
if (messagePart.type === "text") {
return (
<Text variant="body" color={chat?.currentUser.id ? undefined : "neutral900"} key={index}>
{messagePart.content.text}
</Text>
)
}
if (messagePart.type === "plainLink") {
return (
<Text
key={index}
variant="body"
onPress={() => openLink(messagePart.content.link)}
color="sky150"
>
{messagePart.content.link}
</Text>
)
}
if (messagePart.type === "textLink") {
return (
<Text
key={index}
variant="body"
onPress={() => openLink(messagePart.content.link)}
color="sky150"
>
{messagePart.content.text}
</Text>
)
}
if (messagePart.type === "mention") {
return (
<Text
key={index}
variant="body"
onPress={() => openLink(`https://pubnub.com/${messagePart.content.id}`)}
color="sky150"
>
@{messagePart.content.name}
</Text>
)
}
if (messagePart.type === "channelReference") {
return (
<Text
key={index}
variant="body"
onPress={() => openChannel(messagePart.content.id)}
color="sky150"
>
#{messagePart.content.name}
</Text>
)
}
return null
},
[chat?.currentUser]
)
const renderImage = () => {
if (!imageSrc || Platform.OS !== "web") {
return
}
// display just one image for now
return <img src={imageSrc} style={{ width: 100, height: 100 }} />
}
const messageElements = messageProps.currentMessage?.originalPnMessage.getMessageElements()
if (messageElements) {
return (
<View>
{messageProps.currentMessage?.originalPnMessage.quotedMessage ? (
<Quote
message={messageProps.currentMessage?.originalPnMessage.quotedMessage}
onGoToMessage={() =>
onGoToMessage(messageProps.currentMessage?.originalPnMessage.quotedMessage)
}
charactersLimit={50}
/>
) : null}
<Text variant="body">
{messageProps.currentMessage?.originalPnMessage.deleted
? "(Message softly deleted)"
: messageElements.map((msgPart, index) =>
renderMessagePart(
msgPart,
index,
messageProps.currentMessage?.originalPnMessage.userId || ""
)
)}
</Text>
{renderImage()}
{renderEmojis()}
</View>
)
}
return <Text variant="body">{messageProps.currentMessage?.text}</Text>
}