signal-desktop/ts/state/smart/TypingBubble.tsx

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2023-09-27 21:23:52 +00:00
import { last } from 'lodash';
import React from 'react';
import { useSelector } from 'react-redux';
2023-09-27 21:23:52 +00:00
import { TypingBubble } from '../../components/conversation/TypingBubble';
import { useGlobalModalActions } from '../ducks/globalModals';
import { getIntl, getTheme } from '../selectors/user';
2024-02-27 16:01:25 +00:00
import { useTimelineItem } from '../selectors/timeline';
2023-09-27 21:23:52 +00:00
import {
getConversationSelector,
getConversationMessagesSelector,
} from '../selectors/conversations';
import { getPreferredBadgeSelector } from '../selectors/badges';
type ExternalProps = {
conversationId: string;
};
2023-09-27 21:23:52 +00:00
export function SmartTypingBubble({
conversationId,
}: ExternalProps): JSX.Element {
const i18n = useSelector(getIntl);
const theme = useSelector(getTheme);
const getConversation = useSelector(getConversationSelector);
const conversation = getConversation(conversationId);
if (!conversation) {
throw new Error(`Did not find conversation ${conversationId} in state!`);
}
2023-09-27 21:23:52 +00:00
const typingContactIdTimestamps =
conversation.typingContactIdTimestamps ?? {};
const conversationMessages = useSelector(getConversationMessagesSelector)(
conversationId
);
2023-09-27 21:23:52 +00:00
const lastMessageId = last(conversationMessages.items);
2024-02-27 16:01:25 +00:00
const lastItem = useTimelineItem(lastMessageId, conversationId);
2023-09-27 21:23:52 +00:00
let lastItemAuthorId: string | undefined;
let lastItemTimestamp: number | undefined;
if (lastItem?.data) {
if ('author' in lastItem.data) {
lastItemAuthorId = lastItem.data.author?.id;
}
if ('receivedAtMS' in lastItem.data) {
lastItemTimestamp = lastItem.data.receivedAtMS;
2023-09-27 21:23:52 +00:00
}
}
const { showContactModal } = useGlobalModalActions();
2023-09-27 21:23:52 +00:00
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
return (
<TypingBubble
conversationId={conversationId}
conversationType={conversation.type}
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps={typingContactIdTimestamps}
lastItemAuthorId={lastItemAuthorId}
lastItemTimestamp={lastItemTimestamp}
i18n={i18n}
theme={theme}
2023-09-27 21:23:52 +00:00
getConversation={getConversation}
getPreferredBadge={getPreferredBadge}
showContactModal={showContactModal}
/>
);
}