session-desktop/ts/components/session/conversation/SessionMessagesList.tsx

112 lines
3.9 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { useSelector } from 'react-redux';
// tslint:disable-next-line: no-submodule-imports
import useKey from 'react-use/lib/useKey';
import { PropsForDataExtractionNotification, QuoteClickOptions } from '../../../models/messageType';
2021-07-13 09:00:20 +02:00
import {
PropsForCallNotification,
PropsForExpirationTimer,
PropsForGroupInvitation,
PropsForGroupUpdate,
} from '../../../state/ducks/conversations';
import { getSortedMessagesTypesOfSelectedConversation } from '../../../state/selectors/conversations';
import { CallNotification } from '../../conversation/notification-bubble/CallNotification';
import { DataExtractionNotification } from '../../conversation/DataExtractionNotification';
import { GroupInvitation } from '../../conversation/GroupInvitation';
import { GroupNotification } from '../../conversation/GroupNotification';
import { Message } from '../../conversation/Message';
import { MessageDateBreak } from '../../conversation/message/DateBreak';
import { TimerNotification } from '../../conversation/TimerNotification';
import { SessionLastSeenIndicator } from './SessionLastSeenIndicator';
export const SessionMessagesList = (props: {
2021-07-20 08:58:51 +02:00
scrollToQuoteMessage: (options: QuoteClickOptions) => Promise<void>;
onPageUpPressed: () => void;
onPageDownPressed: () => void;
onHomePressed: () => void;
onEndPressed: () => void;
2021-07-20 08:58:51 +02:00
}) => {
const messagesProps = useSelector(getSortedMessagesTypesOfSelectedConversation);
useKey('PageUp', () => {
props.onPageUpPressed();
});
useKey('PageDown', () => {
props.onPageDownPressed();
});
useKey('Home', () => {
props.onHomePressed();
});
useKey('End', () => {
props.onEndPressed();
});
2021-07-16 01:34:32 +02:00
return (
<>
{messagesProps.map(messageProps => {
const messageId = messageProps.message.props.messageId;
const unreadIndicator = messageProps.showUnreadIndicator ? (
<SessionLastSeenIndicator key={`unread-indicator-${messageId}`} />
) : null;
const dateBreak =
messageProps.showDateBreak !== undefined ? (
<MessageDateBreak
key={`date-break-${messageId}`}
timestamp={messageProps.showDateBreak}
messageId={messageId}
/>
) : null;
if (messageProps.message?.messageType === 'group-notification') {
const msgProps = messageProps.message.props as PropsForGroupUpdate;
return [<GroupNotification key={messageId} {...msgProps} />, dateBreak, unreadIndicator];
2021-07-16 01:34:32 +02:00
}
if (messageProps.message?.messageType === 'group-invitation') {
const msgProps = messageProps.message.props as PropsForGroupInvitation;
return [<GroupInvitation key={messageId} {...msgProps} />, dateBreak, unreadIndicator];
2021-07-16 01:34:32 +02:00
}
if (messageProps.message?.messageType === 'data-extraction') {
const msgProps = messageProps.message.props as PropsForDataExtractionNotification;
return [
<DataExtractionNotification key={messageId} {...msgProps} />,
dateBreak,
unreadIndicator,
];
2021-07-16 01:34:32 +02:00
}
if (messageProps.message?.messageType === 'timer-notification') {
const msgProps = messageProps.message.props as PropsForExpirationTimer;
return [<TimerNotification key={messageId} {...msgProps} />, dateBreak, unreadIndicator];
2021-07-16 01:34:32 +02:00
}
if (messageProps.message?.messageType === 'call-notification') {
const msgProps = messageProps.message.props as PropsForCallNotification;
2021-10-18 06:38:49 +02:00
return [<CallNotification key={messageId} {...msgProps} />, dateBreak, unreadIndicator];
2021-10-18 06:38:49 +02:00
}
2021-07-16 01:34:32 +02:00
if (!messageProps) {
return null;
2021-07-16 01:34:32 +02:00
}
return [
<Message
messageId={messageId}
onQuoteClick={props.scrollToQuoteMessage}
key={messageId}
/>,
dateBreak,
unreadIndicator,
];
2021-07-16 01:34:32 +02:00
})}
</>
);
};