2022-12-10 02:02:22 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
import React, { memo, useCallback } from 'react';
|
2022-12-10 02:02:22 +00:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { Lightbox } from '../../components/Lightbox';
|
|
|
|
import { getConversationSelector } from '../selectors/conversations';
|
|
|
|
import { getIntl } from '../selectors/user';
|
2022-12-14 18:12:04 +00:00
|
|
|
import { useConversationsActions } from '../ducks/conversations';
|
2022-12-10 02:02:22 +00:00
|
|
|
import { useGlobalModalActions } from '../ducks/globalModals';
|
|
|
|
import { useLightboxActions } from '../ducks/lightbox';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { useAudioPlayerActions } from '../ducks/audioPlayer';
|
2022-12-10 02:02:22 +00:00
|
|
|
import {
|
|
|
|
getIsViewOnce,
|
|
|
|
getMedia,
|
2023-03-04 03:03:15 +00:00
|
|
|
getHasPrevMessage,
|
|
|
|
getHasNextMessage,
|
2024-02-02 23:39:32 +00:00
|
|
|
getPlaybackDisabled,
|
2022-12-10 02:02:22 +00:00
|
|
|
getSelectedIndex,
|
|
|
|
shouldShowLightbox,
|
|
|
|
} from '../selectors/lightbox';
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartLightbox = memo(function SmartLightbox() {
|
|
|
|
const i18n = useSelector(getIntl);
|
2022-12-14 18:12:04 +00:00
|
|
|
const { saveAttachment } = useConversationsActions();
|
2023-03-04 03:03:15 +00:00
|
|
|
const {
|
|
|
|
closeLightbox,
|
|
|
|
showLightboxForNextMessage,
|
|
|
|
showLightboxForPrevMessage,
|
2023-09-26 15:38:21 +00:00
|
|
|
setSelectedLightboxIndex,
|
2023-03-04 03:03:15 +00:00
|
|
|
} = useLightboxActions();
|
2023-03-20 22:23:53 +00:00
|
|
|
const { toggleForwardMessagesModal } = useGlobalModalActions();
|
2023-02-24 23:18:57 +00:00
|
|
|
const { pauseVoiceNotePlayer } = useAudioPlayerActions();
|
2022-12-10 02:02:22 +00:00
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
const conversationSelector = useSelector(getConversationSelector);
|
2022-12-10 02:02:22 +00:00
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
const isShowingLightbox = useSelector(shouldShowLightbox);
|
|
|
|
const isViewOnce = useSelector(getIsViewOnce);
|
|
|
|
const media = useSelector(getMedia);
|
|
|
|
const hasPrevMessage = useSelector(getHasPrevMessage);
|
|
|
|
const hasNextMessage = useSelector(getHasNextMessage);
|
|
|
|
const selectedIndex = useSelector(getSelectedIndex);
|
|
|
|
const playbackDisabled = useSelector(getPlaybackDisabled);
|
2022-12-10 02:02:22 +00:00
|
|
|
|
2023-03-04 03:03:15 +00:00
|
|
|
const onPrevAttachment = useCallback(() => {
|
|
|
|
if (selectedIndex <= 0) {
|
|
|
|
if (hasPrevMessage) {
|
|
|
|
showLightboxForPrevMessage();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-26 15:38:21 +00:00
|
|
|
setSelectedLightboxIndex(selectedIndex - 1);
|
2023-03-04 03:03:15 +00:00
|
|
|
}, [
|
|
|
|
showLightboxForPrevMessage,
|
|
|
|
selectedIndex,
|
2023-09-26 15:38:21 +00:00
|
|
|
setSelectedLightboxIndex,
|
2023-03-04 03:03:15 +00:00
|
|
|
hasPrevMessage,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const onNextAttachment = useCallback(() => {
|
|
|
|
if (selectedIndex >= media.length - 1) {
|
|
|
|
if (hasNextMessage) {
|
|
|
|
showLightboxForNextMessage();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-26 15:38:21 +00:00
|
|
|
setSelectedLightboxIndex(selectedIndex + 1);
|
2023-03-04 03:03:15 +00:00
|
|
|
}, [
|
|
|
|
showLightboxForNextMessage,
|
|
|
|
media,
|
|
|
|
selectedIndex,
|
2023-09-26 15:38:21 +00:00
|
|
|
setSelectedLightboxIndex,
|
2023-03-04 03:03:15 +00:00
|
|
|
hasNextMessage,
|
|
|
|
]);
|
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
if (!isShowingLightbox) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Lightbox
|
|
|
|
closeLightbox={closeLightbox}
|
|
|
|
getConversation={conversationSelector}
|
|
|
|
i18n={i18n}
|
|
|
|
isViewOnce={isViewOnce}
|
|
|
|
media={media}
|
2024-02-02 23:39:32 +00:00
|
|
|
playbackDisabled={playbackDisabled}
|
2022-12-14 18:12:04 +00:00
|
|
|
saveAttachment={saveAttachment}
|
2022-12-10 02:02:22 +00:00
|
|
|
selectedIndex={selectedIndex || 0}
|
2023-03-20 22:23:53 +00:00
|
|
|
toggleForwardMessagesModal={toggleForwardMessagesModal}
|
2023-02-24 23:18:57 +00:00
|
|
|
onMediaPlaybackStart={pauseVoiceNotePlayer}
|
2023-03-04 03:03:15 +00:00
|
|
|
onPrevAttachment={onPrevAttachment}
|
|
|
|
onNextAttachment={onNextAttachment}
|
2023-09-26 15:38:21 +00:00
|
|
|
onSelectAttachment={setSelectedLightboxIndex}
|
2023-03-04 03:03:15 +00:00
|
|
|
hasNextMessage={hasNextMessage}
|
|
|
|
hasPrevMessage={hasPrevMessage}
|
2022-12-10 02:02:22 +00:00
|
|
|
/>
|
|
|
|
);
|
2024-03-13 20:44:13 +00:00
|
|
|
});
|