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

133 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import { connect } from 'react-redux';
import { sortBy } from 'lodash';
import type { StateType } from '../reducer';
2021-08-06 00:17:05 +00:00
import { mapDispatchToProps } from '../actions';
import type { StateProps } from '../../components/conversation/conversation-details/ConversationDetails';
import { ConversationDetails } from '../../components/conversation/conversation-details/ConversationDetails';
2021-03-11 21:29:31 +00:00
import {
getConversationByIdSelector,
2023-08-16 20:54:39 +00:00
getConversationByServiceIdSelector,
getAllComposableConversations,
2021-03-11 21:29:31 +00:00
} from '../selectors/conversations';
import { getGroupMemberships } from '../../util/getGroupMemberships';
import { getActiveCallState } from '../selectors/calling';
import { getAreWeASubscriber } from '../selectors/items';
2021-11-02 23:01:13 +00:00
import { getIntl, getTheme } from '../selectors/user';
import {
getBadgesSelector,
getPreferredBadgeSelector,
} from '../selectors/badges';
import { assertDev } from '../../util/assert';
import { SignalService as Proto } from '../../protobuf';
import { getConversationColorAttributes } from '../../util/getConversationColorAttributes';
import type { SmartChooseGroupMembersModalPropsType } from './ChooseGroupMembersModal';
import { SmartChooseGroupMembersModal } from './ChooseGroupMembersModal';
import type { SmartConfirmAdditionsModalPropsType } from './ConfirmAdditionsModal';
import { SmartConfirmAdditionsModal } from './ConfirmAdditionsModal';
import {
getGroupSizeRecommendedLimit,
getGroupSizeHardLimit,
} from '../../groups/limits';
2023-08-09 00:53:06 +00:00
import type { CallHistoryGroup } from '../../types/CallDisposition';
import { getSelectedNavTab } from '../selectors/nav';
export type SmartConversationDetailsProps = {
conversationId: string;
2023-08-09 00:53:06 +00:00
callHistoryGroup?: CallHistoryGroup | null;
};
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
const renderChooseGroupMembersModal = (
props: SmartChooseGroupMembersModalPropsType
) => {
return <SmartChooseGroupMembersModal {...props} />;
};
const renderConfirmAdditionsModal = (
props: SmartConfirmAdditionsModalPropsType
) => {
return <SmartConfirmAdditionsModal {...props} />;
};
const mapStateToProps = (
state: StateType,
props: SmartConversationDetailsProps
): StateProps => {
const conversationSelector = getConversationByIdSelector(state);
const conversation = conversationSelector(props.conversationId);
assertDev(
conversation,
'<SmartConversationDetails> expected a conversation to be found'
);
const canEditGroupInfo = Boolean(conversation.canEditGroupInfo);
const canAddNewMembers = Boolean(conversation.canAddNewMembers);
const isAdmin = Boolean(conversation.areWeAdmin);
const hasGroupLink =
Boolean(conversation.groupLink) &&
conversation.accessControlAddFromInviteLink !== ACCESS_ENUM.UNSATISFIABLE;
2023-08-16 20:54:39 +00:00
const conversationByServiceIdSelector =
getConversationByServiceIdSelector(state);
2021-11-02 23:01:13 +00:00
const groupMemberships = getGroupMemberships(
conversation,
2023-08-16 20:54:39 +00:00
conversationByServiceIdSelector
2021-11-02 23:01:13 +00:00
);
const badges = getBadgesSelector(state)(conversation.badges);
const groupsInCommon =
conversation.type === 'direct'
? getAllComposableConversations(state).filter(
c =>
c.type === 'group' &&
(c.memberships ?? []).some(
2023-08-16 20:54:39 +00:00
member => member.aci === conversation.serviceId
)
)
: [];
const groupsInCommonSorted = sortBy(groupsInCommon, 'title');
const maxGroupSize = getGroupSizeHardLimit(1001);
const maxRecommendedGroupSize = getGroupSizeRecommendedLimit(151);
return {
...props,
2023-08-09 00:53:06 +00:00
areWeASubscriber: getAreWeASubscriber(state),
2021-11-02 23:01:13 +00:00
badges,
canEditGroupInfo,
canAddNewMembers,
conversation: {
...conversation,
...getConversationColorAttributes(conversation),
},
2021-11-17 21:11:21 +00:00
getPreferredBadge: getPreferredBadgeSelector(state),
hasActiveCall: Boolean(getActiveCallState(state)),
i18n: getIntl(state),
isAdmin,
2021-11-02 23:01:13 +00:00
...groupMemberships,
maxGroupSize,
maxRecommendedGroupSize,
2021-08-06 00:17:05 +00:00
userAvatarData: conversation.avatars || [],
hasGroupLink,
groupsInCommon: groupsInCommonSorted,
isGroup: conversation.type === 'group',
2023-08-09 00:53:06 +00:00
selectedNavTab: getSelectedNavTab(state),
2021-11-02 23:01:13 +00:00
theme: getTheme(state),
renderChooseGroupMembersModal,
renderConfirmAdditionsModal,
};
};
2021-08-06 00:17:05 +00:00
const smart = connect(mapStateToProps, mapDispatchToProps);
export const SmartConversationDetails = smart(ConversationDetails);