2023-08-09 00:53:06 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2024-03-13 20:44:13 +00:00
|
|
|
import React, { memo, useEffect, useRef } from 'react';
|
2023-08-09 00:53:06 +00:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { ChatsTab } from '../../components/ChatsTab';
|
|
|
|
import { SmartConversationView } from './ConversationView';
|
|
|
|
import { SmartMiniPlayer } from './MiniPlayer';
|
|
|
|
import { SmartLeftPane } from './LeftPane';
|
|
|
|
import type { NavTabPanelProps } from '../../components/NavTabs';
|
|
|
|
import { useGlobalModalActions } from '../ducks/globalModals';
|
|
|
|
import { getIntl } from '../selectors/user';
|
|
|
|
import { usePrevious } from '../../hooks/usePrevious';
|
|
|
|
import { TargetedMessageSource } from '../ducks/conversationsEnums';
|
|
|
|
import { useConversationsActions } from '../ducks/conversations';
|
2024-01-29 20:09:54 +00:00
|
|
|
import { useToastActions } from '../ducks/toast';
|
2023-08-09 00:53:06 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
2024-01-29 20:09:54 +00:00
|
|
|
import { ToastType } from '../../types/Toast';
|
2023-08-09 00:53:06 +00:00
|
|
|
import { getNavTabsCollapsed } from '../selectors/items';
|
|
|
|
import { useItemsActions } from '../ducks/items';
|
2023-08-14 23:28:47 +00:00
|
|
|
import { getHasAnyFailedStorySends } from '../selectors/stories';
|
|
|
|
import { getHasPendingUpdate } from '../selectors/updates';
|
2023-08-21 20:12:27 +00:00
|
|
|
import { getOtherTabsUnreadStats } from '../selectors/nav';
|
2024-03-15 14:38:26 +00:00
|
|
|
import {
|
|
|
|
getSelectedConversationId,
|
|
|
|
getTargetedMessage,
|
|
|
|
getTargetedMessageSource,
|
|
|
|
} from '../selectors/conversations';
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
function renderConversationView() {
|
|
|
|
return <SmartConversationView />;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderLeftPane(props: NavTabPanelProps) {
|
|
|
|
return <SmartLeftPane {...props} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderMiniPlayer(options: { shouldFlow: boolean }) {
|
|
|
|
return <SmartMiniPlayer {...options} />;
|
|
|
|
}
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartChatsTab = memo(function SmartChatsTab() {
|
2023-08-09 00:53:06 +00:00
|
|
|
const i18n = useSelector(getIntl);
|
|
|
|
const navTabsCollapsed = useSelector(getNavTabsCollapsed);
|
2023-08-14 23:28:47 +00:00
|
|
|
const hasFailedStorySends = useSelector(getHasAnyFailedStorySends);
|
|
|
|
const hasPendingUpdate = useSelector(getHasPendingUpdate);
|
2023-08-21 20:12:27 +00:00
|
|
|
const otherTabsUnreadStats = useSelector(getOtherTabsUnreadStats);
|
2024-03-15 14:38:26 +00:00
|
|
|
const selectedConversationId = useSelector(getSelectedConversationId);
|
2024-03-18 16:22:06 +00:00
|
|
|
const targetedMessageId = useSelector(getTargetedMessage)?.id;
|
2024-03-15 14:38:26 +00:00
|
|
|
const targetedMessageSource = useSelector(getTargetedMessageSource);
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
onConversationClosed,
|
|
|
|
onConversationOpened,
|
|
|
|
scrollToMessage,
|
|
|
|
showConversation,
|
|
|
|
} = useConversationsActions();
|
|
|
|
const { showWhatsNewModal } = useGlobalModalActions();
|
|
|
|
const { toggleNavTabsCollapse } = useItemsActions();
|
2024-01-29 20:09:54 +00:00
|
|
|
const { showToast } = useToastActions();
|
2023-08-09 00:53:06 +00:00
|
|
|
|
2023-08-30 00:09:59 +00:00
|
|
|
const lastOpenedConversationId = useRef<string | undefined>();
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-30 00:09:59 +00:00
|
|
|
if (selectedConversationId !== lastOpenedConversationId.current) {
|
|
|
|
lastOpenedConversationId.current = selectedConversationId;
|
2023-08-09 00:53:06 +00:00
|
|
|
if (selectedConversationId) {
|
2024-03-18 16:22:06 +00:00
|
|
|
onConversationOpened(selectedConversationId, targetedMessageId);
|
2023-08-09 00:53:06 +00:00
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
selectedConversationId &&
|
2024-03-18 16:22:06 +00:00
|
|
|
targetedMessageId &&
|
2023-08-09 00:53:06 +00:00
|
|
|
targetedMessageSource !== TargetedMessageSource.Focus
|
|
|
|
) {
|
2024-03-18 16:22:06 +00:00
|
|
|
scrollToMessage(selectedConversationId, targetedMessageId);
|
2023-08-09 00:53:06 +00:00
|
|
|
}
|
2024-03-18 16:22:06 +00:00
|
|
|
}, [onConversationOpened, selectedConversationId, scrollToMessage, targetedMessageId, targetedMessageSource]);
|
2023-08-09 00:53:06 +00:00
|
|
|
|
2023-08-30 00:09:59 +00:00
|
|
|
const prevConversationId = usePrevious(
|
|
|
|
selectedConversationId,
|
|
|
|
selectedConversationId
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-21 19:51:12 +00:00
|
|
|
if (
|
|
|
|
selectedConversationId != null &&
|
|
|
|
selectedConversationId !== prevConversationId
|
|
|
|
) {
|
|
|
|
const conversation = window.ConversationController.get(
|
|
|
|
selectedConversationId
|
|
|
|
);
|
|
|
|
strictAssert(conversation, 'Conversation must be found');
|
|
|
|
conversation.setMarkedUnread(false);
|
2023-08-09 00:53:06 +00:00
|
|
|
}
|
2023-08-30 00:09:59 +00:00
|
|
|
}, [prevConversationId, selectedConversationId]);
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function refreshConversation({
|
|
|
|
newId,
|
|
|
|
oldId,
|
|
|
|
}: {
|
|
|
|
newId: string;
|
|
|
|
oldId: string;
|
|
|
|
}) {
|
|
|
|
if (prevConversationId === oldId) {
|
|
|
|
showConversation({ conversationId: newId });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close current opened conversation to reload the group information once
|
|
|
|
// linked.
|
|
|
|
function unload() {
|
|
|
|
if (!prevConversationId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onConversationClosed(prevConversationId, 'force unload requested');
|
|
|
|
}
|
|
|
|
|
|
|
|
function packInstallFailed() {
|
2024-01-29 20:09:54 +00:00
|
|
|
showToast({ toastType: ToastType.StickerPackInstallFailed });
|
2023-08-09 00:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.Whisper.events.on('pack-install-failed', packInstallFailed);
|
|
|
|
window.Whisper.events.on('refreshConversation', refreshConversation);
|
|
|
|
window.Whisper.events.on('setupAsNewDevice', unload);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.Whisper.events.off('pack-install-failed', packInstallFailed);
|
|
|
|
window.Whisper.events.off('refreshConversation', refreshConversation);
|
|
|
|
window.Whisper.events.off('setupAsNewDevice', unload);
|
|
|
|
};
|
2024-01-29 20:09:54 +00:00
|
|
|
}, [onConversationClosed, prevConversationId, showConversation, showToast]);
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!selectedConversationId) {
|
|
|
|
window.SignalCI?.handleEvent('empty-inbox:rendered', null);
|
|
|
|
}
|
|
|
|
}, [selectedConversationId]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ChatsTab
|
2023-08-21 20:12:27 +00:00
|
|
|
otherTabsUnreadStats={otherTabsUnreadStats}
|
2023-08-09 00:53:06 +00:00
|
|
|
i18n={i18n}
|
2023-08-14 23:28:47 +00:00
|
|
|
hasFailedStorySends={hasFailedStorySends}
|
|
|
|
hasPendingUpdate={hasPendingUpdate}
|
2023-08-09 00:53:06 +00:00
|
|
|
navTabsCollapsed={navTabsCollapsed}
|
|
|
|
onToggleNavTabsCollapse={toggleNavTabsCollapse}
|
|
|
|
renderConversationView={renderConversationView}
|
|
|
|
renderLeftPane={renderLeftPane}
|
|
|
|
renderMiniPlayer={renderMiniPlayer}
|
|
|
|
selectedConversationId={selectedConversationId}
|
|
|
|
showWhatsNewModal={showWhatsNewModal}
|
|
|
|
/>
|
|
|
|
);
|
2024-03-13 20:44:13 +00:00
|
|
|
});
|