2023-04-10 21:38:34 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-03-15 14:38:26 +00:00
|
|
|
import React, { memo, useCallback } from 'react';
|
2023-04-10 21:38:34 +00:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import type { StateType } from '../reducer';
|
|
|
|
import { getIntl } from '../selectors/user';
|
|
|
|
import { useGlobalModalActions } from '../ducks/globalModals';
|
|
|
|
import DeleteMessagesModal from '../../components/DeleteMessagesModal';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
|
|
|
import { canDeleteMessagesForEveryone } from '../selectors/message';
|
|
|
|
import { useConversationsActions } from '../ducks/conversations';
|
|
|
|
import { useToastActions } from '../ducks/toast';
|
2024-03-13 20:44:13 +00:00
|
|
|
import {
|
|
|
|
getConversationSelector,
|
|
|
|
getLastSelectedMessage,
|
|
|
|
} from '../selectors/conversations';
|
|
|
|
import { getDeleteMessagesProps } from '../selectors/globalModals';
|
2024-05-28 15:56:00 +00:00
|
|
|
import { useItemsActions } from '../ducks/items';
|
|
|
|
import {
|
|
|
|
getLocalDeleteWarningShown,
|
|
|
|
getDeleteSyncSendEnabled,
|
|
|
|
} from '../selectors/items';
|
|
|
|
import { LocalDeleteWarningModal } from '../../components/LocalDeleteWarningModal';
|
2023-04-10 21:38:34 +00:00
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartDeleteMessagesModal = memo(
|
|
|
|
function SmartDeleteMessagesModal() {
|
|
|
|
const deleteMessagesProps = useSelector(getDeleteMessagesProps);
|
|
|
|
strictAssert(
|
|
|
|
deleteMessagesProps != null,
|
|
|
|
'Cannot render delete messages modal without messages'
|
|
|
|
);
|
|
|
|
const { conversationId, messageIds, onDelete } = deleteMessagesProps;
|
2024-03-15 14:38:26 +00:00
|
|
|
const conversationSelector = useSelector(getConversationSelector);
|
|
|
|
const conversation = conversationSelector(conversationId);
|
|
|
|
const { isMe } = conversation;
|
2023-04-13 20:38:35 +00:00
|
|
|
|
2024-03-15 14:38:26 +00:00
|
|
|
const getCanDeleteForEveryone = useCallback(
|
|
|
|
(state: StateType) => {
|
|
|
|
return canDeleteMessagesForEveryone(state, { messageIds, isMe });
|
|
|
|
},
|
|
|
|
[messageIds, isMe]
|
|
|
|
);
|
|
|
|
const canDeleteForEveryone = useSelector(getCanDeleteForEveryone);
|
2024-05-28 15:56:00 +00:00
|
|
|
const isDeleteSyncSendEnabled = useSelector(getDeleteSyncSendEnabled);
|
2024-03-13 20:44:13 +00:00
|
|
|
const lastSelectedMessage = useSelector(getLastSelectedMessage);
|
|
|
|
const i18n = useSelector(getIntl);
|
|
|
|
const { toggleDeleteMessagesModal } = useGlobalModalActions();
|
|
|
|
const { deleteMessages, deleteMessagesForEveryone } =
|
|
|
|
useConversationsActions();
|
|
|
|
const { showToast } = useToastActions();
|
2023-04-10 21:38:34 +00:00
|
|
|
|
2024-03-19 17:12:32 +00:00
|
|
|
const messageCount = deleteMessagesProps.messageIds.length;
|
|
|
|
|
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
toggleDeleteMessagesModal(undefined);
|
|
|
|
}, [toggleDeleteMessagesModal]);
|
|
|
|
|
|
|
|
const handleDeleteForMe = useCallback(() => {
|
|
|
|
deleteMessages({
|
|
|
|
conversationId,
|
|
|
|
messageIds,
|
|
|
|
lastSelectedMessage,
|
|
|
|
});
|
|
|
|
onDelete?.();
|
|
|
|
}, [
|
|
|
|
conversationId,
|
|
|
|
deleteMessages,
|
|
|
|
lastSelectedMessage,
|
|
|
|
messageIds,
|
|
|
|
onDelete,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const handleDeleteForEveryone = useCallback(() => {
|
|
|
|
deleteMessagesForEveryone(messageIds);
|
|
|
|
onDelete?.();
|
|
|
|
}, [deleteMessagesForEveryone, messageIds, onDelete]);
|
|
|
|
|
2024-05-28 15:56:00 +00:00
|
|
|
const localDeleteWarningShown = useSelector(getLocalDeleteWarningShown);
|
|
|
|
const { putItem } = useItemsActions();
|
|
|
|
if (!localDeleteWarningShown && isDeleteSyncSendEnabled) {
|
|
|
|
return (
|
|
|
|
<LocalDeleteWarningModal
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={() => {
|
|
|
|
putItem('localDeleteWarningShown', true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
return (
|
|
|
|
<DeleteMessagesModal
|
|
|
|
isMe={isMe}
|
|
|
|
canDeleteForEveryone={canDeleteForEveryone}
|
|
|
|
i18n={i18n}
|
2024-05-28 15:56:00 +00:00
|
|
|
isDeleteSyncSendEnabled={isDeleteSyncSendEnabled}
|
2024-03-19 17:12:32 +00:00
|
|
|
messageCount={messageCount}
|
|
|
|
onClose={handleClose}
|
|
|
|
onDeleteForMe={handleDeleteForMe}
|
|
|
|
onDeleteForEveryone={handleDeleteForEveryone}
|
2024-03-13 20:44:13 +00:00
|
|
|
showToast={showToast}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|