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

108 lines
3.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
2023-03-04 03:03:15 +00:00
import React, { useCallback } from 'react';
2022-12-10 02:02:22 +00:00
import { useSelector } from 'react-redux';
import type { ReadonlyDeep } from 'type-fest';
2022-12-10 02:02:22 +00:00
import type { GetConversationByIdType } from '../selectors/conversations';
import type { LocalizerType } from '../../types/Util';
import type { MediaItemType } from '../../types/MediaItem';
import type { StateType } from '../reducer';
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,
2022-12-10 02:02:22 +00:00
getSelectedIndex,
shouldShowLightbox,
} from '../selectors/lightbox';
export function SmartLightbox(): JSX.Element | null {
const i18n = useSelector<StateType, LocalizerType>(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<StateType, GetConversationByIdType>(
getConversationSelector
);
const isShowingLightbox = useSelector<StateType, boolean>(shouldShowLightbox);
const isViewOnce = useSelector<StateType, boolean>(getIsViewOnce);
const media = useSelector<
StateType,
ReadonlyArray<ReadonlyDeep<MediaItemType>>
>(getMedia);
2023-03-04 03:03:15 +00:00
const hasPrevMessage = useSelector<StateType, boolean>(getHasPrevMessage);
const hasNextMessage = useSelector<StateType, boolean>(getHasNextMessage);
2022-12-10 02:02:22 +00:00
const selectedIndex = useSelector<StateType, number>(getSelectedIndex);
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}
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
/>
);
}