2022-09-26 16:24:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { noop, pick } from 'lodash';
|
|
|
|
import React from 'react';
|
|
|
|
import type { MeasuredComponentProps } from 'react-measure';
|
|
|
|
import Measure from 'react-measure';
|
|
|
|
|
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
|
|
|
import type {
|
|
|
|
LocalizerType,
|
|
|
|
ReplacementValuesType,
|
|
|
|
ThemeType,
|
|
|
|
} from '../types/Util';
|
2022-12-15 00:48:36 +00:00
|
|
|
import { ToastType } from '../types/Toast';
|
2022-09-26 16:24:52 +00:00
|
|
|
import { filterAndSortConversationsByRecent } from '../util/filterAndSortConversations';
|
|
|
|
import { ConfirmationDialog } from './ConfirmationDialog';
|
|
|
|
import type { Row } from './ConversationList';
|
|
|
|
import { ConversationList, RowType } from './ConversationList';
|
|
|
|
import { DisabledReason } from './conversationList/GroupListItem';
|
|
|
|
import { Modal } from './Modal';
|
|
|
|
import { SearchInput } from './SearchInput';
|
|
|
|
import { useRestoreFocus } from '../hooks/useRestoreFocus';
|
|
|
|
|
|
|
|
type OwnProps = {
|
|
|
|
i18n: LocalizerType;
|
|
|
|
theme: ThemeType;
|
|
|
|
contact: Pick<ConversationType, 'id' | 'title' | 'uuid'>;
|
|
|
|
candidateConversations: ReadonlyArray<ConversationType>;
|
|
|
|
regionCode: string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
type DispatchProps = {
|
|
|
|
toggleAddUserToAnotherGroupModal: (contactId?: string) => void;
|
|
|
|
addMemberToGroup: (
|
|
|
|
conversationId: string,
|
|
|
|
contactId: string,
|
|
|
|
onComplete: () => void
|
|
|
|
) => void;
|
|
|
|
showToast: (toastType: ToastType, parameters?: ReplacementValuesType) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Props = OwnProps & DispatchProps;
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function AddUserToAnotherGroupModal({
|
2022-09-26 16:24:52 +00:00
|
|
|
i18n,
|
|
|
|
theme,
|
|
|
|
contact,
|
|
|
|
toggleAddUserToAnotherGroupModal,
|
|
|
|
addMemberToGroup,
|
|
|
|
showToast,
|
|
|
|
candidateConversations,
|
|
|
|
regionCode,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: Props): JSX.Element | null {
|
2022-09-26 16:24:52 +00:00
|
|
|
const [searchTerm, setSearchTerm] = React.useState('');
|
|
|
|
const [filteredConversations, setFilteredConversations] = React.useState(
|
|
|
|
filterAndSortConversationsByRecent(candidateConversations, '', undefined)
|
|
|
|
);
|
|
|
|
|
|
|
|
const [selectedGroupId, setSelectedGroupId] = React.useState<
|
|
|
|
undefined | string
|
|
|
|
>(undefined);
|
|
|
|
|
|
|
|
const groupLookup: Map<string, ConversationType> = React.useMemo(() => {
|
|
|
|
const map = new Map();
|
|
|
|
candidateConversations.forEach(conversation => {
|
|
|
|
map.set(conversation.id, conversation);
|
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}, [candidateConversations]);
|
|
|
|
|
|
|
|
const [inputRef] = useRestoreFocus();
|
|
|
|
|
|
|
|
const normalizedSearchTerm = searchTerm.trim();
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
setFilteredConversations(
|
|
|
|
filterAndSortConversationsByRecent(
|
|
|
|
candidateConversations,
|
|
|
|
normalizedSearchTerm,
|
|
|
|
regionCode
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, 200);
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
candidateConversations,
|
|
|
|
normalizedSearchTerm,
|
|
|
|
setFilteredConversations,
|
|
|
|
regionCode,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const selectedGroup = selectedGroupId
|
|
|
|
? groupLookup.get(selectedGroupId)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const handleSearchInputChange = React.useCallback(
|
|
|
|
(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSearchTerm(event.target.value);
|
|
|
|
},
|
|
|
|
[setSearchTerm]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleGetRow = React.useCallback(
|
|
|
|
(idx: number): Row | undefined => {
|
|
|
|
const convo = filteredConversations[idx];
|
|
|
|
|
|
|
|
// these are always populated in the case of a group
|
|
|
|
const memberships = convo.memberships ?? [];
|
|
|
|
const pendingApprovalMemberships = convo.pendingApprovalMemberships ?? [];
|
|
|
|
const pendingMemberships = convo.pendingMemberships ?? [];
|
|
|
|
const membersCount = convo.membersCount ?? 0;
|
|
|
|
|
|
|
|
let disabledReason;
|
|
|
|
|
|
|
|
if (memberships.some(c => c.uuid === contact.uuid)) {
|
|
|
|
disabledReason = DisabledReason.AlreadyMember;
|
|
|
|
} else if (
|
|
|
|
pendingApprovalMemberships.some(c => c.uuid === contact.uuid) ||
|
|
|
|
pendingMemberships.some(c => c.uuid === contact.uuid)
|
|
|
|
) {
|
|
|
|
disabledReason = DisabledReason.Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: RowType.SelectSingleGroup,
|
|
|
|
group: {
|
|
|
|
...pick(convo, 'id', 'avatarPath', 'title', 'unblurredAvatarPath'),
|
|
|
|
memberships,
|
|
|
|
membersCount,
|
|
|
|
disabledReason,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[filteredConversations, contact]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!selectedGroup && (
|
|
|
|
<Modal
|
2022-09-27 20:24:21 +00:00
|
|
|
modalName="AddUserToAnotherGroupModal"
|
2022-09-26 16:24:52 +00:00
|
|
|
hasXButton
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={toggleAddUserToAnotherGroupModal}
|
|
|
|
title={i18n('AddUserToAnotherGroupModal__title')}
|
|
|
|
moduleClassName="AddUserToAnotherGroupModal"
|
2022-09-29 22:40:09 +00:00
|
|
|
padded={false}
|
2022-09-26 16:24:52 +00:00
|
|
|
>
|
|
|
|
<div className="AddUserToAnotherGroupModal__main-body">
|
|
|
|
<SearchInput
|
|
|
|
i18n={i18n}
|
|
|
|
placeholder={i18n('contactSearchPlaceholder')}
|
|
|
|
onChange={handleSearchInputChange}
|
|
|
|
ref={inputRef}
|
|
|
|
value={searchTerm}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Measure bounds>
|
|
|
|
{({ contentRect, measureRef }: MeasuredComponentProps) => (
|
|
|
|
<div
|
|
|
|
className="AddUserToAnotherGroupModal__list-wrapper"
|
|
|
|
ref={measureRef}
|
|
|
|
>
|
|
|
|
<ConversationList
|
|
|
|
dimensions={contentRect.bounds}
|
|
|
|
getPreferredBadge={() => undefined}
|
2022-12-14 19:05:32 +00:00
|
|
|
getRow={handleGetRow}
|
2022-09-26 16:24:52 +00:00
|
|
|
i18n={i18n}
|
2022-12-14 19:05:32 +00:00
|
|
|
lookupConversationWithoutUuid={async _ => undefined}
|
2022-09-26 16:24:52 +00:00
|
|
|
onClickArchiveButton={noop}
|
|
|
|
onClickContactCheckbox={noop}
|
|
|
|
onSelectConversation={setSelectedGroupId}
|
2022-12-14 19:05:32 +00:00
|
|
|
rowCount={filteredConversations.length}
|
|
|
|
setIsFetchingUUID={noop}
|
|
|
|
shouldRecomputeRowHeights={false}
|
2022-09-26 16:24:52 +00:00
|
|
|
showChooseGroupMembers={noop}
|
2022-12-14 19:05:32 +00:00
|
|
|
showConversation={noop}
|
2022-09-26 16:24:52 +00:00
|
|
|
showUserNotFoundModal={noop}
|
2022-12-14 19:05:32 +00:00
|
|
|
theme={theme}
|
2022-09-26 16:24:52 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Measure>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{selectedGroupId && selectedGroup && (
|
|
|
|
<ConfirmationDialog
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName="AddUserToAnotherGroupModal__confirm"
|
2022-09-26 16:24:52 +00:00
|
|
|
title={i18n('AddUserToAnotherGroupModal__confirm-title')}
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={() => setSelectedGroupId(undefined)}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
text: i18n('AddUserToAnotherGroupModal__confirm-add'),
|
|
|
|
style: 'affirmative',
|
|
|
|
action: () => {
|
|
|
|
showToast(ToastType.AddingUserToGroup, {
|
|
|
|
contact: contact.title,
|
|
|
|
});
|
|
|
|
addMemberToGroup(selectedGroupId, contact.id, () =>
|
|
|
|
showToast(ToastType.UserAddedToGroup, {
|
|
|
|
contact: contact.title,
|
|
|
|
group: selectedGroup.title,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
toggleAddUserToAnotherGroupModal(undefined);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{i18n('AddUserToAnotherGroupModal__confirm-message', {
|
|
|
|
contact: contact.title,
|
|
|
|
group: selectedGroup.title,
|
|
|
|
})}
|
|
|
|
</ConfirmationDialog>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|