2022-09-26 16:24:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-01-25 23:51:08 +00:00
|
|
|
import { pick } from 'lodash';
|
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import type { ListRowProps } from 'react-virtualized';
|
2022-09-26 16:24:52 +00:00
|
|
|
|
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2023-04-10 21:38:34 +00:00
|
|
|
import type { LocalizerType, 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';
|
2023-01-25 23:51:08 +00:00
|
|
|
import type { GroupListItemConversationType } from './conversationList/GroupListItem';
|
|
|
|
import {
|
|
|
|
DisabledReason,
|
|
|
|
GroupListItem,
|
|
|
|
} from './conversationList/GroupListItem';
|
2022-09-26 16:24:52 +00:00
|
|
|
import { Modal } from './Modal';
|
|
|
|
import { SearchInput } from './SearchInput';
|
|
|
|
import { useRestoreFocus } from '../hooks/useRestoreFocus';
|
2023-01-25 23:51:08 +00:00
|
|
|
import { ListView } from './ListView';
|
|
|
|
import { ListTile } from './ListTile';
|
2023-04-10 21:38:34 +00:00
|
|
|
import type { ShowToastAction } from '../state/ducks/toast';
|
2023-07-25 23:56:56 +00:00
|
|
|
import { SizeObserver } from '../hooks/useSizeObserver';
|
2022-09-26 16:24:52 +00:00
|
|
|
|
|
|
|
type OwnProps = {
|
|
|
|
i18n: LocalizerType;
|
|
|
|
theme: ThemeType;
|
2023-08-16 20:54:39 +00:00
|
|
|
contact: Pick<ConversationType, 'id' | 'title' | 'serviceId' | 'pni'>;
|
2022-09-26 16:24:52 +00:00
|
|
|
candidateConversations: ReadonlyArray<ConversationType>;
|
|
|
|
regionCode: string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
type DispatchProps = {
|
|
|
|
toggleAddUserToAnotherGroupModal: (contactId?: string) => void;
|
2022-12-16 03:12:05 +00:00
|
|
|
addMembersToGroup: (
|
2022-09-26 16:24:52 +00:00
|
|
|
conversationId: string,
|
2022-12-16 03:12:05 +00:00
|
|
|
contactIds: Array<string>,
|
|
|
|
opts: {
|
|
|
|
onSuccess?: () => unknown;
|
|
|
|
onFailure?: () => unknown;
|
|
|
|
}
|
2022-09-26 16:24:52 +00:00
|
|
|
) => void;
|
2023-04-10 21:38:34 +00:00
|
|
|
showToast: ShowToastAction;
|
2022-09-26 16:24:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
contact,
|
|
|
|
toggleAddUserToAnotherGroupModal,
|
2022-12-16 03:12:05 +00:00
|
|
|
addMembersToGroup,
|
2022-09-26 16:24:52 +00:00
|
|
|
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(
|
2023-01-25 23:51:08 +00:00
|
|
|
(idx: number): GroupListItemConversationType => {
|
2022-09-26 16:24:52 +00:00
|
|
|
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;
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (memberships.some(c => c.aci === contact.serviceId)) {
|
2022-09-26 16:24:52 +00:00
|
|
|
disabledReason = DisabledReason.AlreadyMember;
|
|
|
|
} else if (
|
2023-08-16 20:54:39 +00:00
|
|
|
pendingApprovalMemberships.some(c => c.aci === contact.serviceId) ||
|
|
|
|
pendingMemberships.some(c => c.serviceId === contact.serviceId) ||
|
|
|
|
pendingMemberships.some(c => c.serviceId === contact.pni)
|
2022-09-26 16:24:52 +00:00
|
|
|
) {
|
|
|
|
disabledReason = DisabledReason.Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-25 23:51:08 +00:00
|
|
|
...pick(convo, 'id', 'avatarPath', 'title', 'unblurredAvatarPath'),
|
|
|
|
memberships,
|
|
|
|
membersCount,
|
|
|
|
disabledReason,
|
2022-09-26 16:24:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[filteredConversations, contact]
|
|
|
|
);
|
|
|
|
|
2023-01-25 23:51:08 +00:00
|
|
|
const renderGroupListItem = useCallback(
|
|
|
|
({ key, index, style }: ListRowProps) => {
|
|
|
|
const group = handleGetRow(index);
|
|
|
|
return (
|
|
|
|
<div key={key} style={style}>
|
|
|
|
<GroupListItem
|
|
|
|
i18n={i18n}
|
|
|
|
group={group}
|
|
|
|
onSelectGroup={setSelectedGroupId}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[i18n, handleGetRow]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleCalculateRowHeight = useCallback(
|
|
|
|
() => ListTile.heightCompact,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2022-09-26 16:24:52 +00:00
|
|
|
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}
|
2023-03-30 00:03:25 +00:00
|
|
|
title={i18n('icu:AddUserToAnotherGroupModal__title')}
|
2022-09-26 16:24:52 +00:00
|
|
|
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}
|
2023-03-30 00:03:25 +00:00
|
|
|
placeholder={i18n('icu:contactSearchPlaceholder')}
|
2022-09-26 16:24:52 +00:00
|
|
|
onChange={handleSearchInputChange}
|
|
|
|
ref={inputRef}
|
|
|
|
value={searchTerm}
|
|
|
|
/>
|
2023-07-25 23:56:56 +00:00
|
|
|
<SizeObserver>
|
|
|
|
{(ref, size) => {
|
2023-01-25 23:51:08 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="AddUserToAnotherGroupModal__list-wrapper"
|
2023-07-25 23:56:56 +00:00
|
|
|
ref={ref}
|
2023-01-25 23:51:08 +00:00
|
|
|
>
|
2023-07-25 23:56:56 +00:00
|
|
|
{size != null && (
|
|
|
|
<ListView
|
|
|
|
width={size.width}
|
|
|
|
height={size.height}
|
|
|
|
rowCount={filteredConversations.length}
|
|
|
|
calculateRowHeight={handleCalculateRowHeight}
|
|
|
|
rowRenderer={renderGroupListItem}
|
|
|
|
/>
|
|
|
|
)}
|
2023-01-25 23:51:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
2023-07-25 23:56:56 +00:00
|
|
|
</SizeObserver>
|
2022-09-26 16:24:52 +00:00
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{selectedGroupId && selectedGroup && (
|
|
|
|
<ConfirmationDialog
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName="AddUserToAnotherGroupModal__confirm"
|
2023-03-30 00:03:25 +00:00
|
|
|
title={i18n('icu:AddUserToAnotherGroupModal__confirm-title')}
|
2022-09-26 16:24:52 +00:00
|
|
|
i18n={i18n}
|
|
|
|
onClose={() => setSelectedGroupId(undefined)}
|
|
|
|
actions={[
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
text: i18n('icu:AddUserToAnotherGroupModal__confirm-add'),
|
2022-09-26 16:24:52 +00:00
|
|
|
style: 'affirmative',
|
|
|
|
action: () => {
|
2023-04-10 21:38:34 +00:00
|
|
|
showToast({
|
|
|
|
toastType: ToastType.AddingUserToGroup,
|
|
|
|
parameters: {
|
|
|
|
contact: contact.title,
|
|
|
|
},
|
2022-09-26 16:24:52 +00:00
|
|
|
});
|
2022-12-16 03:12:05 +00:00
|
|
|
addMembersToGroup(selectedGroupId, [contact.id], {
|
|
|
|
onSuccess: () =>
|
2023-04-10 21:38:34 +00:00
|
|
|
showToast({
|
|
|
|
toastType: ToastType.UserAddedToGroup,
|
|
|
|
parameters: {
|
|
|
|
contact: contact.title,
|
|
|
|
group: selectedGroup.title,
|
|
|
|
},
|
2022-12-16 03:12:05 +00:00
|
|
|
}),
|
|
|
|
});
|
2022-09-26 16:24:52 +00:00
|
|
|
toggleAddUserToAnotherGroupModal(undefined);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:AddUserToAnotherGroupModal__confirm-message', {
|
2022-09-26 16:24:52 +00:00
|
|
|
contact: contact.title,
|
|
|
|
group: selectedGroup.title,
|
|
|
|
})}
|
|
|
|
</ConfirmationDialog>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|