import { Pressable, View } from "react-native" import { SearchKeywordHighlight } from…
- User
import { Pressable, View } from "react-native" import { SearchKeywordHighlight } from "../../../ui-components/search-keyword-highlight/search-keyword-highlight.component" import { useStyles } from "./search-messages.style" import { MessageType, RoomKindEnum } from "@openchat/types" import { useAccountId, useChannel, useCommunity, useGroup, useMember, useRoom, useUserLastSeenAs, useUserUnified, } from "@openchat/ui" import { RoomUtils, UserProfileUtils } from "@openchat/models" import { StyledText } from "../../../ui-components/styled-text/styled-text" import { Icon } from "../../../ui-components/icon/icon.component" import { UserPhoto } from "../../user/user-photo/user-photo.component" import { ChannelIcon } from "../../channel/channel-icon/channel-icon.component" import { NavigationRef } from "../../../navigation/navigation.hooks" import { StackActions } from "@react-navigation/native"
export function SearchMessages({ message, keyword, }: { message: MessageType keyword: string }) { // --------------------------------------------------------------------------- // variables // --------------------------------------------------------------------------- console.log("====================message================") console.log(message) console.log("====================end ================") const styles = useStyles() const { groupId, communityId, channelId, userId } = RoomUtils.extractIds( message.roomId, )
const room = useRoom(message.roomId) const accountId = useAccountId() const group = useGroup(groupId) const channel = useChannel(channelId) const userLastSeen = useUserLastSeenAs(message.senderId) const user = useUserUnified(userLastSeen) const searchMember = useMember(message.senderMemberId) const searchUser = useUserUnified( UserProfileUtils.toPartialFromMember(searchMember), ) const community = useCommunity(communityId)
const isCurrentUserSender = (message.senderId && user?.id === accountId) || (message.senderMemberId && searchUser?.id === accountId)
const displayUser = message.senderId ? user : searchUser
const isDirectMessage = !community && !channel && !!userId
const directMessageText = isDirectMessage ? isCurrentUserSender ? "you in direct message with" : "in direct message with you" : ""
let typeMessage = room && (room.kind === RoomKindEnum.DM || room.kind === RoomKindEnum.CHANNEL_REGULAR) let typeQuestion = room && room.kind === RoomKindEnum.QUESTION let typeIssue = room && room.kind === RoomKindEnum.ISSUE let typeNews = room && room.kind === RoomKindEnum.NEWS let typeComments = room && room.kind === RoomKindEnum.BOARD_COMMENTS let typeReply = room && room.kind === RoomKindEnum.CHANNEL_REGULAR_REPLY
// --------------------------------------------------------------------------- return ( {community && channel && ( <View style={{ flexDirection: "row" }}> <View style={{ flexDirection: "row", alignItems: "center", gap: 5 }} > <View style={{ flexDirection: "row", marginLeft: 5 }}> {channel.name} )}
<View style={styles.userMessagesName}> {isCurrentUserSender ? ( <> {displayUser && ( <View style={{ flexDirection: "row", gap: 10, alignItems: "center", }} > <StyledText style={styles.msgText}> message by {directMessageText} </StyledText> <UserPhoto size={25} user={displayUser} /> <StyledText style={styles.nameUser} ellipsizeMode="tail" numberOfLines={1} > <SearchKeywordHighlight text={displayUser.name} keyword={keyword} /> </StyledText> </View> )} </> ) : ( <View style={{ flexDirection: "row", gap: 10, alignItems: "center" }} > {displayUser && ( <> <StyledText style={styles.msgText}>message by</StyledText> <UserPhoto size={25} user={displayUser} /> <StyledText style={styles.nameUser} ellipsizeMode="tail" numberOfLines={1} > <SearchKeywordHighlight text={displayUser.name} keyword={keyword} /> </StyledText> <StyledText style={styles.msgText}> {directMessageText} </StyledText> </> )} </View> )} </View> </View> <Pressable style={styles.messagesWrap} onPress={() => { NavigationRef.dispatch( StackActions.push("MessageRoom", { roomId: room && room.id }), ) }} > <SearchKeywordHighlight text={message.text} keyword={keyword} style={styles.messages} /> </Pressable> </View>) } please fix this let typeMessage = room && (room.kind === RoomKindEnum.DM || room.kind === RoomKindEnum.CHANNEL_REGULAR) let typeQuestion = room && room.kind === RoomKindEnum.QUESTION let typeIssue = room && room.kind === RoomKindEnum.ISSUE let typeNews = room && room.kind === RoomKindEnum.NEWS let typeComments = room && room.kind === RoomKindEnum.BOARD_COMMENTS let typeReply = room && room.kind === RoomKindEnum.CHANNEL_REGULAR_REPLY part
- Assistant
The issue with the code snippet you've provided is that the variables
typeMessage,typeQuestion,typeIssue,typeNews,typeComments, andtypeReplyare assigned boolean values based on theroom.kindchecks. This usage is not typical for "type" variables, as they are usually expected to hold some kind of type or string description.If the intention is to have these variables hold boolean values indicating the type of the message, that approach is correct, but the variable names might be misleading. They could be renamed to make their boolean nature clearer, for example,
isMessage,isQuestion,isIssue, etc.Here's the refactored code with more appropriate variable naming:
let isMessageType = room && (room.kind === RoomKindEnum.DM || room.kind === RoomKindEnum.CHANNEL_REGULAR); let isQuestionType = room && room.kind === RoomKindEnum.QUESTION; let isIssueType = room && room.kind === RoomKindEnum.ISSUE; let isNewsType = room && room.kind === RoomKindEnum.NEWS; let isCommentsType = room && room.kind === RoomKindEnum.BOARD_COMMENTS; let isReplyType = room && room.kind === RoomKindEnum.CHANNEL_REGULAR_REPLY;However, if your intention was to track which exact type the room kind is, you could consolidate these into a single variable using a string or an enum, but typically in error-prone environments like dynamically typed JavaScript, using booleans for such checks is quite common practice.
In the case where you intended to refactor it into something like a single
roomTypethat holds string values, here’s how you could do it:let roomType = null; if (room) { switch (room.kind) { case RoomKindEnum.DM: case RoomKindEnum.CHANNEL_REGULAR: roomType = "message"; break; case RoomKindEnum.QUESTION: roomType = "question"; break; case RoomKindEnum.ISSUE: roomType = "issue"; break; case RoomKindEnum.NEWS: roomType = "news"; break; case RoomKindEnum.BOARD_COMMENTS: roomType = "comments"; break; case RoomKindEnum.CHANNEL_REGULAR_REPLY: roomType = "reply"; break; default: roomType = "unknown"; break; } }This way, the
roomTypevariable will hold a string describing the room kind, which you can then use in your component's logic as necessary.