From 6b69833892037b2b8022a7992f4c930892539fc1 Mon Sep 17 00:00:00 2001 From: Przemek Janowski Date: Tue, 19 Sep 2023 10:19:35 +0200 Subject: [PATCH] feat(samples): mentions screen (#116) --- .../components/list-item/ListItem.tsx | 3 +- .../user-suggestion-box/UserSuggestionBox.tsx | 2 +- .../hooks/useCommonChatRenderers.tsx | 6 +- .../screens/tabs/mentions/MentionsScreen.tsx | 110 +++++++++++++++++- .../ui-components/line/Line.tsx | 9 +- 5 files changed, 116 insertions(+), 14 deletions(-) diff --git a/samples/react-native-group-chat/components/list-item/ListItem.tsx b/samples/react-native-group-chat/components/list-item/ListItem.tsx index 2f7b853e..d608b7a2 100644 --- a/samples/react-native-group-chat/components/list-item/ListItem.tsx +++ b/samples/react-native-group-chat/components/list-item/ListItem.tsx @@ -35,7 +35,7 @@ export function ListItem({ title, avatar, badge, onPress, showCheckbox, checked {badge ? ( - 5 + {badge} ) : null} @@ -59,6 +59,7 @@ const styles = StyleSheet.create({ }, title: { paddingHorizontal: 16, + flexGrow: 1, }, badge: { backgroundColor: colors.badge, diff --git a/samples/react-native-group-chat/components/user-suggestion-box/UserSuggestionBox.tsx b/samples/react-native-group-chat/components/user-suggestion-box/UserSuggestionBox.tsx index fcdfd630..483ac90f 100644 --- a/samples/react-native-group-chat/components/user-suggestion-box/UserSuggestionBox.tsx +++ b/samples/react-native-group-chat/components/user-suggestion-box/UserSuggestionBox.tsx @@ -13,7 +13,7 @@ export function UserSuggestionBox({ users, onUserSelect }: UserSuggestionBoxProp return ( {users.map((user) => ( - onUserSelect(user)} /> + onUserSelect(user)} /> ))} ) diff --git a/samples/react-native-group-chat/hooks/useCommonChatRenderers.tsx b/samples/react-native-group-chat/hooks/useCommonChatRenderers.tsx index 0964d125..0e1cbd1e 100644 --- a/samples/react-native-group-chat/hooks/useCommonChatRenderers.tsx +++ b/samples/react-native-group-chat/hooks/useCommonChatRenderers.tsx @@ -71,7 +71,7 @@ export function useCommonChatRenderers({ ) const renderChatFooter = useCallback(() => { - if (!messageDraft || !messageDraft.quotedMessage) { + if (!messageDraft) { return null } @@ -83,7 +83,7 @@ export function useCommonChatRenderers({ quotedMessageComponent = ( scrollToMessage(quotedMessage)} /> @@ -102,7 +102,7 @@ export function useCommonChatRenderers({ {userSuggestionComponent} ) - }, [messageDraft, showSuggestedUsers, scrollToMessage]) + }, [messageDraft, showSuggestedUsers, scrollToMessage, handleUserToMention, suggestedUsers]) const renderMessagePart = useCallback( (messagePart: MixedTextTypedElement, index: number, userId: string | number) => { diff --git a/samples/react-native-group-chat/screens/tabs/mentions/MentionsScreen.tsx b/samples/react-native-group-chat/screens/tabs/mentions/MentionsScreen.tsx index 2e712adc..45dc1349 100644 --- a/samples/react-native-group-chat/screens/tabs/mentions/MentionsScreen.tsx +++ b/samples/react-native-group-chat/screens/tabs/mentions/MentionsScreen.tsx @@ -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) { + const { chat, setCurrentChannel } = useContext(ChatContext) + const [mentions, setMentions] = useState([]) + 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 ( + + {loading ? ( + + ) : ( + No mentions found. + )} + + ) + } -export function MentionsScreen() { return ( - - Mentions screen - + + {mentions.map((mention, index) => ( + + openChannel(mention.message.channelId)} + underlayColor={colors.neutral50} + > + + + {TimetokenUtils.timetokenToDate(mention.message.timetoken).toLocaleString([], { + weekday: "short", + month: "short", + day: "numeric", + hour: "numeric", + minute: "numeric", + })} + + + + + {mention.message.text} + + + + + {index !== mentions.length - 1 && } + + ))} + ) } + +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, + }, +}) diff --git a/samples/react-native-group-chat/ui-components/line/Line.tsx b/samples/react-native-group-chat/ui-components/line/Line.tsx index be7b393c..a750e2e5 100644 --- a/samples/react-native-group-chat/ui-components/line/Line.tsx +++ b/samples/react-native-group-chat/ui-components/line/Line.tsx @@ -1,6 +1,9 @@ import React from "react" -import { View } from "react-native" +import { View, ViewProps } from "react-native" -export function Line() { - return +export function Line(props: ViewProps) { + const { style, ...rest } = props + return ( + + ) }