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

100 lines
3 KiB
TypeScript
Raw Normal View History

2022-12-10 02:02:22 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
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,
getPlaybackDisabled,
2022-12-10 02:02:22 +00:00
getSelectedIndex,
shouldShowLightbox,
} from '../selectors/lightbox';
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,
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
const conversationSelector = useSelector(getConversationSelector);
2022-12-10 02:02:22 +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;
}
setSelectedLightboxIndex(selectedIndex - 1);
2023-03-04 03:03:15 +00:00
}, [
showLightboxForPrevMessage,
selectedIndex,
setSelectedLightboxIndex,
2023-03-04 03:03:15 +00:00
hasPrevMessage,
]);
const onNextAttachment = useCallback(() => {
if (selectedIndex >= media.length - 1) {
if (hasNextMessage) {
showLightboxForNextMessage();
}
return;
}
setSelectedLightboxIndex(selectedIndex + 1);
2023-03-04 03:03:15 +00:00
}, [
showLightboxForNextMessage,
media,
selectedIndex,
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}
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}
onSelectAttachment={setSelectedLightboxIndex}
2023-03-04 03:03:15 +00:00
hasNextMessage={hasNextMessage}
hasPrevMessage={hasPrevMessage}
2022-12-10 02:02:22 +00:00
/>
);
});