-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 104 additions & 6 deletions
110
samples/react-native-group-chat/screens/tabs/mentions/MentionsScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,108 @@ | ||
import React from "react"; | ||
import { View, Text } from "react-native"; | ||
import React, { useContext, useState } from "react" | ||
import { View, StyleSheet, ScrollView, TouchableHighlight, ActivityIndicator } from "react-native" | ||
import { useFocusEffect } from "@react-navigation/native" | ||
import { UserMentionData, TimetokenUtils } from "@pubnub/chat" | ||
import { BottomTabScreenProps } from "@react-navigation/bottom-tabs" | ||
|
||
import { colorPalette as colors, Line, Text } from "../../../ui-components" | ||
import { ChatContext } from "../../../context" | ||
import { Avatar } from "../../../components" | ||
import { BottomTabsParamList } from "../../../types" | ||
|
||
export function MentionsScreen({ | ||
navigation, | ||
}: BottomTabScreenProps<BottomTabsParamList, "Mentions">) { | ||
const { chat, setCurrentChannel } = useContext(ChatContext) | ||
const [mentions, setMentions] = useState<UserMentionData[]>([]) | ||
const [loading, setLoading] = useState(true) | ||
|
||
async function openChannel(channelId: string) { | ||
if (!chat) return | ||
const channel = await chat.getChannel(channelId) | ||
if (!channel) { | ||
alert("This channel no longer exists.") | ||
return | ||
} | ||
setCurrentChannel(channel) | ||
navigation.navigate("Chat") | ||
} | ||
|
||
useFocusEffect(() => { | ||
const init = async () => { | ||
if (!chat) return | ||
const { enhancedMentionsData } = await chat.getCurrentUserMentions() | ||
setMentions(enhancedMentionsData.reverse()) | ||
setLoading(false) | ||
} | ||
if (chat) init() | ||
}) | ||
|
||
if (loading || !mentions.length) { | ||
return ( | ||
<View style={styles.loading}> | ||
{loading ? ( | ||
<ActivityIndicator size="large" color={colors.navy700} /> | ||
) : ( | ||
<Text textAlign="center">No mentions found.</Text> | ||
)} | ||
</View> | ||
) | ||
} | ||
|
||
export function MentionsScreen() { | ||
return ( | ||
<View> | ||
<Text>Mentions screen</Text> | ||
</View> | ||
<ScrollView style={styles.container}> | ||
{mentions.map((mention, index) => ( | ||
<View key={mention.event.timetoken}> | ||
<TouchableHighlight | ||
onPress={() => openChannel(mention.message.channelId)} | ||
underlayColor={colors.neutral50} | ||
> | ||
<View> | ||
<Text variant="smallBody" color="neutral600"> | ||
{TimetokenUtils.timetokenToDate(mention.message.timetoken).toLocaleString([], { | ||
weekday: "short", | ||
month: "short", | ||
day: "numeric", | ||
hour: "numeric", | ||
minute: "numeric", | ||
})} | ||
</Text> | ||
<View style={styles.message}> | ||
<Avatar source={mention.user} style={{ marginTop: 10 }} /> | ||
<View style={styles.bubble}> | ||
<Text>{mention.message.text}</Text> | ||
</View> | ||
</View> | ||
</View> | ||
</TouchableHighlight> | ||
{index !== mentions.length - 1 && <Line style={{ marginVertical: 16 }} />} | ||
</View> | ||
))} | ||
</ScrollView> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
loading: { | ||
backgroundColor: colors.neutral0, | ||
flex: 1, | ||
justifyContent: "center", | ||
}, | ||
container: { | ||
backgroundColor: colors.neutral0, | ||
flex: 1, | ||
padding: 16, | ||
}, | ||
message: { | ||
flexDirection: "row", | ||
marginTop: 10, | ||
marginLeft: 10, | ||
}, | ||
bubble: { | ||
backgroundColor: colors.neutral50, | ||
borderRadius: 6, | ||
borderTopLeftRadius: 0, | ||
marginLeft: 10, | ||
padding: 10, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import React from "react" | ||
import { View } from "react-native" | ||
import { View, ViewProps } from "react-native" | ||
|
||
export function Line() { | ||
return <View style={{ width: "100%", height: 1, backgroundColor: "#E2E8F0" }} /> | ||
export function Line(props: ViewProps) { | ||
const { style, ...rest } = props | ||
return ( | ||
<View style={[{ width: "100%", height: 1, backgroundColor: "#E2E8F0" }, style]} {...rest} /> | ||
) | ||
} |