signal-desktop/ts/state/selectors/timeline.ts

92 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
2022-12-23 00:32:03 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2024-02-27 16:01:25 +00:00
import { useSelector } from 'react-redux';
2022-12-23 00:32:03 +00:00
import type { TimelineItemType } from '../../components/conversation/TimelineItem';
import type { StateType } from '../reducer';
import {
getConversationSelector,
2023-03-20 22:23:53 +00:00
getTargetedMessage,
getSelectedMessageIds,
2024-02-27 16:01:25 +00:00
getMessages,
getCachedConversationMemberColorsSelector,
2022-12-23 00:32:03 +00:00
} from './conversations';
import { getAccountSelector } from './accounts';
import {
getRegionCode,
getUserConversationId,
getUserNumber,
getUserACI,
getUserPNI,
} from './user';
import { getDefaultConversationColor } from './items';
2022-12-23 00:32:03 +00:00
import { getActiveCall, getCallSelector } from './calling';
import { getPropsForBubble } from './message';
2023-08-09 00:53:06 +00:00
import { getCallHistorySelector } from './callHistory';
2024-02-27 16:01:25 +00:00
import { useProxySelector } from '../../hooks/useProxySelector';
2022-12-23 00:32:03 +00:00
2024-02-27 16:01:25 +00:00
const getTimelineItem = (
2022-12-23 00:32:03 +00:00
state: StateType,
2024-02-27 16:01:25 +00:00
messageId: string | undefined,
contactNameColors: Map<string, string>
2022-12-23 00:32:03 +00:00
): TimelineItemType | undefined => {
2024-02-27 16:01:25 +00:00
if (messageId === undefined) {
return undefined;
}
2022-12-23 00:32:03 +00:00
const messageLookup = getMessages(state);
2024-02-27 16:01:25 +00:00
const message = messageLookup[messageId];
2022-12-23 00:32:03 +00:00
if (!message) {
return undefined;
}
2023-03-20 22:23:53 +00:00
const targetedMessage = getTargetedMessage(state);
const conversationSelector = getConversationSelector(state);
const regionCode = getRegionCode(state);
const ourNumber = getUserNumber(state);
const ourAci = getUserACI(state);
const ourPni = getUserPNI(state);
const ourConversationId = getUserConversationId(state);
const callSelector = getCallSelector(state);
2023-08-09 00:53:06 +00:00
const callHistorySelector = getCallHistorySelector(state);
const activeCall = getActiveCall(state);
const accountSelector = getAccountSelector(state);
2023-03-20 22:23:53 +00:00
const selectedMessageIds = getSelectedMessageIds(state);
const defaultConversationColor = getDefaultConversationColor(state);
return getPropsForBubble(message, {
conversationSelector,
ourConversationId,
ourNumber,
ourAci,
ourPni,
regionCode,
2023-03-20 22:23:53 +00:00
targetedMessageId: targetedMessage?.id,
targetedMessageCounter: targetedMessage?.counter,
2024-02-27 16:01:25 +00:00
contactNameColors,
callSelector,
2023-08-09 00:53:06 +00:00
callHistorySelector,
activeCall,
accountSelector,
2023-03-20 22:23:53 +00:00
selectedMessageIds,
defaultConversationColor,
});
2022-12-23 00:32:03 +00:00
};
2024-02-27 16:01:25 +00:00
export const useTimelineItem = (
messageId: string | undefined,
conversationId: string
): TimelineItemType | undefined => {
// Generating contact name colors can take a while in large groups. We don't want to do
// this inside of useProxySelector, since the proxied state invalidates the memoization
// from createSelector. So we do the expensive part outside of useProxySelector, taking
// advantage of reselect's global cache.
const contactNameColors = useSelector(
getCachedConversationMemberColorsSelector
)(conversationId);
return useProxySelector(getTimelineItem, messageId, contactNameColors);
};