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

257 lines
8.9 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2021-08-11 16:23:21 +00:00
import React from 'react';
2019-01-14 21:49:58 +00:00
import { connect } from 'react-redux';
import { get } from 'lodash';
2019-01-14 21:49:58 +00:00
import { mapDispatchToProps } from '../actions';
import type { PropsType as LeftPanePropsType } from '../../components/LeftPane';
import { LeftPane, LeftPaneMode } from '../../components/LeftPane';
2023-01-18 23:31:10 +00:00
import { DialogExpiredBuild } from '../../components/DialogExpiredBuild';
import type { PropsType as DialogExpiredBuildPropsType } from '../../components/DialogExpiredBuild';
import type { StateType } from '../reducer';
2021-03-03 20:09:58 +00:00
import { missingCaseError } from '../../util/missingCaseError';
import { lookupConversationWithoutServiceId } from '../../util/lookupConversationWithoutServiceId';
2023-01-18 23:31:10 +00:00
import { isDone as isRegistrationDone } from '../../util/registration';
2019-01-14 21:49:58 +00:00
import { ComposerStep, OneTimeModalState } from '../ducks/conversationsEnums';
import {
getIsSearching,
2021-11-01 18:43:02 +00:00
getQuery,
getSearchConversation,
getSearchResults,
getStartSearchCounter,
isSearching,
} from '../selectors/search';
2023-01-18 23:31:10 +00:00
import {
getIntl,
getRegionCode,
getTheme,
getIsMacOS,
} from '../selectors/user';
import { hasExpired } from '../selectors/expiration';
import {
isUpdateDialogVisible,
isUpdateDownloaded,
isOSUnsupported,
} from '../selectors/updates';
2021-11-17 21:11:21 +00:00
import { getPreferredBadgeSelector } from '../selectors/badges';
2023-01-18 23:31:10 +00:00
import { hasNetworkDialog } from '../selectors/network';
2021-11-12 01:17:29 +00:00
import {
getPreferredLeftPaneWidth,
getUsernamesEnabled,
2023-04-05 20:48:00 +00:00
getContactManagementEnabled,
2023-08-09 00:53:06 +00:00
getNavTabsCollapsed,
2021-11-12 01:17:29 +00:00
} from '../selectors/items';
2019-11-07 21:36:16 +00:00
import {
2021-08-06 00:17:05 +00:00
getComposeAvatarData,
2021-03-03 20:09:58 +00:00
getComposeGroupAvatar,
getComposeGroupExpireTimer,
2021-08-06 00:17:05 +00:00
getComposeGroupName,
getComposerConversationSearchTerm,
2021-03-03 20:09:58 +00:00
getComposerStep,
getComposerUUIDFetchState,
2021-11-12 01:17:29 +00:00
getComposeSelectedContacts,
2021-08-06 00:17:05 +00:00
getFilteredCandidateContactsForNewGroup,
getFilteredComposeContacts,
getFilteredComposeGroups,
2019-11-07 21:36:16 +00:00
getLeftPaneLists,
2021-03-03 20:09:58 +00:00
getMaximumGroupSizeModalState,
getRecommendedGroupSizeModalState,
getSelectedConversationId,
2023-03-20 22:23:53 +00:00
getTargetedMessage,
2019-11-07 21:36:16 +00:00
getShowArchived,
2021-03-03 20:09:58 +00:00
hasGroupCreationError,
isCreatingGroup,
2021-08-06 00:17:05 +00:00
isEditingAvatar,
2019-11-07 21:36:16 +00:00
} from '../selectors/conversations';
import type { WidthBreakpoint } from '../../components/_util';
import {
getGroupSizeRecommendedLimit,
getGroupSizeHardLimit,
} from '../../groups/limits';
2019-01-14 21:49:58 +00:00
import { SmartMessageSearchResult } from './MessageSearchResult';
import { SmartNetworkStatus } from './NetworkStatus';
import { SmartRelinkDialog } from './RelinkDialog';
2023-01-18 23:31:10 +00:00
import { SmartUnsupportedOSDialog } from './UnsupportedOSDialog';
import type { PropsType as SmartUnsupportedOSDialogPropsType } from './UnsupportedOSDialog';
import { SmartUpdateDialog } from './UpdateDialog';
import { SmartCaptchaDialog } from './CaptchaDialog';
2022-01-11 20:02:46 +00:00
import { SmartCrashReportDialog } from './CrashReportDialog';
2019-01-14 21:49:58 +00:00
2021-08-11 16:23:21 +00:00
function renderMessageSearchResult(id: string): JSX.Element {
return <SmartMessageSearchResult id={id} />;
}
function renderNetworkStatus(
props: Readonly<{ containerWidthBreakpoint: WidthBreakpoint }>
): JSX.Element {
return <SmartNetworkStatus {...props} />;
}
function renderRelinkDialog(
props: Readonly<{ containerWidthBreakpoint: WidthBreakpoint }>
): JSX.Element {
return <SmartRelinkDialog {...props} />;
}
function renderUpdateDialog(
props: Readonly<{ containerWidthBreakpoint: WidthBreakpoint }>
): JSX.Element {
return <SmartUpdateDialog {...props} />;
}
function renderCaptchaDialog({ onSkip }: { onSkip(): void }): JSX.Element {
return <SmartCaptchaDialog onSkip={onSkip} />;
}
2022-01-11 20:02:46 +00:00
function renderCrashReportDialog(): JSX.Element {
return <SmartCrashReportDialog />;
}
2023-01-18 23:31:10 +00:00
function renderExpiredBuildDialog(
props: DialogExpiredBuildPropsType
): JSX.Element {
return <DialogExpiredBuild {...props} />;
}
function renderUnsupportedOSDialog(
props: Readonly<SmartUnsupportedOSDialogPropsType>
): JSX.Element {
return <SmartUnsupportedOSDialog {...props} />;
}
const getModeSpecificProps = (
state: StateType
): LeftPanePropsType['modeSpecificProps'] => {
2021-03-03 20:09:58 +00:00
const composerStep = getComposerStep(state);
switch (composerStep) {
case undefined:
if (getShowArchived(state)) {
const { archivedConversations } = getLeftPaneLists(state);
2021-11-01 18:43:02 +00:00
const searchConversation = getSearchConversation(state);
const searchTerm = getQuery(state);
2021-03-03 20:09:58 +00:00
return {
mode: LeftPaneMode.Archive,
archivedConversations,
2021-11-01 18:43:02 +00:00
searchConversation,
searchTerm,
startSearchCounter: getStartSearchCounter(state),
2021-11-01 18:43:02 +00:00
...(searchConversation && searchTerm ? getSearchResults(state) : {}),
2021-03-03 20:09:58 +00:00
};
}
if (isSearching(state)) {
const primarySendsSms = Boolean(
get(state.items, ['primarySendsSms'], false)
);
2021-03-03 20:09:58 +00:00
return {
mode: LeftPaneMode.Search,
primarySendsSms,
2022-01-27 22:12:26 +00:00
searchConversation: getSearchConversation(state),
searchDisabled: state.network.challengeStatus !== 'idle',
startSearchCounter: getStartSearchCounter(state),
2021-03-03 20:09:58 +00:00
...getSearchResults(state),
};
}
return {
mode: LeftPaneMode.Inbox,
isAboutToSearch: getIsSearching(state),
2022-01-27 22:12:26 +00:00
searchConversation: getSearchConversation(state),
searchDisabled: state.network.challengeStatus !== 'idle',
searchTerm: getQuery(state),
startSearchCounter: getStartSearchCounter(state),
2021-03-03 20:09:58 +00:00
...getLeftPaneLists(state),
};
case ComposerStep.StartDirectConversation:
return {
mode: LeftPaneMode.Compose,
composeContacts: getFilteredComposeContacts(state),
composeGroups: getFilteredComposeGroups(state),
2021-03-03 20:09:58 +00:00
regionCode: getRegionCode(state),
searchTerm: getComposerConversationSearchTerm(state),
2021-11-12 01:17:29 +00:00
isUsernamesEnabled: getUsernamesEnabled(state),
uuidFetchState: getComposerUUIDFetchState(state),
2021-03-03 20:09:58 +00:00
};
case ComposerStep.ChooseGroupMembers:
return {
mode: LeftPaneMode.ChooseGroupMembers,
candidateContacts: getFilteredCandidateContactsForNewGroup(state),
groupSizeRecommendedLimit: getGroupSizeRecommendedLimit(),
groupSizeHardLimit: getGroupSizeHardLimit(),
2021-03-03 20:09:58 +00:00
isShowingRecommendedGroupSizeModal:
getRecommendedGroupSizeModalState(state) ===
OneTimeModalState.Showing,
isShowingMaximumGroupSizeModal:
getMaximumGroupSizeModalState(state) === OneTimeModalState.Showing,
regionCode: getRegionCode(state),
searchTerm: getComposerConversationSearchTerm(state),
2021-03-03 20:09:58 +00:00
selectedContacts: getComposeSelectedContacts(state),
2022-06-17 00:38:28 +00:00
isUsernamesEnabled: getUsernamesEnabled(state),
uuidFetchState: getComposerUUIDFetchState(state),
2021-03-03 20:09:58 +00:00
};
case ComposerStep.SetGroupMetadata:
return {
mode: LeftPaneMode.SetGroupMetadata,
groupAvatar: getComposeGroupAvatar(state),
groupName: getComposeGroupName(state),
groupExpireTimer: getComposeGroupExpireTimer(state),
2021-03-03 20:09:58 +00:00
hasError: hasGroupCreationError(state),
isCreating: isCreatingGroup(state),
2021-08-06 00:17:05 +00:00
isEditingAvatar: isEditingAvatar(state),
2021-03-03 20:09:58 +00:00
selectedContacts: getComposeSelectedContacts(state),
2021-08-06 00:17:05 +00:00
userAvatarData: getComposeAvatarData(state),
2021-03-03 20:09:58 +00:00
};
default:
throw missingCaseError(composerStep);
}
};
2019-03-12 00:20:16 +00:00
const mapStateToProps = (state: StateType) => {
2023-01-18 23:31:10 +00:00
const hasUpdateDialog = isUpdateDialogVisible(state);
const hasUnsupportedOS = isOSUnsupported(state);
let hasExpiredDialog = false;
let unsupportedOSDialogType: 'error' | 'warning' | undefined;
if (hasExpired(state)) {
if (hasUnsupportedOS) {
unsupportedOSDialogType = 'error';
} else {
hasExpiredDialog = true;
}
} else if (hasUnsupportedOS) {
unsupportedOSDialogType = 'warning';
}
2019-01-14 21:49:58 +00:00
return {
2023-01-18 23:31:10 +00:00
hasNetworkDialog: hasNetworkDialog(state),
hasExpiredDialog,
hasRelinkDialog: !isRegistrationDone(),
hasUpdateDialog,
isUpdateDownloaded: isUpdateDownloaded(state),
unsupportedOSDialogType,
modeSpecificProps: getModeSpecificProps(state),
2023-08-09 00:53:06 +00:00
navTabsCollapsed: getNavTabsCollapsed(state),
preferredWidthFromStorage: getPreferredLeftPaneWidth(state),
selectedConversationId: getSelectedConversationId(state),
2023-03-20 22:23:53 +00:00
targetedMessageId: getTargetedMessage(state)?.id,
2019-03-12 00:20:16 +00:00
showArchived: getShowArchived(state),
2021-11-17 21:11:21 +00:00
getPreferredBadge: getPreferredBadgeSelector(state),
2023-04-05 20:48:00 +00:00
isContactManagementEnabled: getContactManagementEnabled(state),
2019-01-14 21:49:58 +00:00
i18n: getIntl(state),
2023-01-18 23:31:10 +00:00
isMacOS: getIsMacOS(state),
regionCode: getRegionCode(state),
challengeStatus: state.network.challengeStatus,
2022-01-11 20:02:46 +00:00
crashReportCount: state.crashReports.count,
renderMessageSearchResult,
renderNetworkStatus,
renderRelinkDialog,
renderUpdateDialog,
renderCaptchaDialog,
2022-01-11 20:02:46 +00:00
renderCrashReportDialog,
2023-01-18 23:31:10 +00:00
renderExpiredBuildDialog,
renderUnsupportedOSDialog,
lookupConversationWithoutServiceId,
2021-11-02 23:01:13 +00:00
theme: getTheme(state),
2019-01-14 21:49:58 +00:00
};
};
const smart = connect(mapStateToProps, mapDispatchToProps);
export const SmartLeftPane = smart(LeftPane);