import * as React from 'react'; import { View } from 'react-native'; import { gMessageAttributeMentions, useChatContext } from '../../chat'; import { useConfigContext } from '../../config'; import { useColors } from '../../hook'; import { useI18nContext } from '../../i18n'; import { ChatConversationType, ChatMessage } from '../../rename.chat'; import { usePaletteContext } from '../../theme'; import { Icon } from '../../ui/Image'; import { PressableHighlight } from '../../ui/Pressable'; import { SingleLineText } from '../../ui/Text'; import { formatTsForConvList } from '../../utils'; import { Avatar, GroupAvatar } from '../Avatar'; import { Badges } from '../Badges'; import { useMessageSnapshot } from '../hooks'; import type { ConversationListItemProps } from './types'; /** * Conversation list item component. */ export function ConversationListItem(props: ConversationListItemProps) { const { onClicked, onLongPressed, data } = props; const { lastMessage } = data; const { formatTime } = useConfigContext(); const { colors } = usePaletteContext(); const { getColor } = useColors({ pin_bg: { light: colors.neutral[9], dark: colors.neutral[2], }, t2: { light: colors.neutral[5], dark: colors.neutral[6], }, t3: { light: colors.neutral[7], dark: colors.neutral[5], }, mention: { light: colors.primary[5], dark: colors.primary[6], }, }); const im = useChatContext(); const { tr } = useI18nContext(); const { getMessageSnapshot } = useMessageSnapshot(); const getMention = React.useCallback( (msg?: ChatMessage) => { if (msg?.attributes?.[gMessageAttributeMentions]) { const mentions = msg.attributes?.[gMessageAttributeMentions]; if (typeof mentions === 'string') { if (mentions === 'ALL') { return tr('@all'); } } else if (Array.isArray(mentions)) { const ret = (mentions as string[]).find((item) => { if (item === im.userId) { return true; } return false; }); return ret ? tr('@me') : null; } } return null; }, [im.userId, tr] ); const getMessageFormatTime = React.useCallback( (msg?: ChatMessage, timestamp?: number): string => { const cb = formatTime?.conversationListCallback; if (msg === undefined && timestamp) { return cb ? cb(timestamp) : formatTsForConvList(timestamp); } else if (msg) { return cb ? cb(msg.localTime) : formatTsForConvList(msg.localTime); } else { return ''; } }, [formatTime?.conversationListCallback] ); const count = data.doNotDisturb === true ? data.unreadMessageCount === 0 ? 0 : undefined : data.unreadMessageCount; return ( { onClicked?.(data); }} onLongPress={() => { onLongPressed?.(data); }} > {data.convType === ChatConversationType.PeerChat ? ( ) : ( )} {data.convName ?? data.convId} {data.doNotDisturb === true ? ( ) : null} {getMention(lastMessage)} {getMessageSnapshot(data.lastMessage)} {getMessageFormatTime(data.lastMessage, data.pinnedTime)} ); } export const ConversationListItemMemo = React.memo(ConversationListItem);