// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import classNames from 'classnames';
import React from 'react';
import { createPortal } from 'react-dom';
import type { LocalizerType } from '../types/Util';
import { SECOND } from '../util/durations';
import { Toast } from './Toast';
import { WidthBreakpoint } from './_util';
import { UsernameMegaphone } from './UsernameMegaphone';
import { assertDev } from '../util/assert';
import { missingCaseError } from '../util/missingCaseError';
import type { AnyToast } from '../types/Toast';
import { ToastType } from '../types/Toast';
import type { AnyActionableMegaphone } from '../types/Megaphone';
import { MegaphoneType } from '../types/Megaphone';
export type PropsType = {
hideToast: () => unknown;
i18n: LocalizerType;
openFileInFolder: (target: string) => unknown;
OS: string;
onShowDebugLog: () => unknown;
onUndoArchive: (conversaetionId: string) => unknown;
toast?: AnyToast;
megaphone?: AnyActionableMegaphone;
centerToast?: boolean;
containerWidthBreakpoint: WidthBreakpoint | null;
isCompositionAreaVisible?: boolean;
isInFullScreenCall: boolean;
};
const SHORT_TIMEOUT = 3 * SECOND;
export function renderToast({
hideToast,
i18n,
openFileInFolder,
onShowDebugLog,
onUndoArchive,
OS,
toast,
}: PropsType): JSX.Element | null {
if (toast === undefined) {
return null;
}
const { toastType } = toast;
if (toastType === ToastType.AddingUserToGroup) {
return (
{i18n('icu:AddUserToAnotherGroupModal__toast--adding-user-to-group', {
contact: toast.parameters.contact,
})}
);
}
if (toastType === ToastType.AddedUsersToCall) {
return (
{i18n('icu:CallingPendingParticipants__Toast--added-users-to-call', {
count: toast.parameters.count,
})}
);
}
if (toastType === ToastType.AlreadyGroupMember) {
return (
{i18n('icu:GroupV2--join--already-in-group')}
);
}
if (toastType === ToastType.AlreadyRequestedToJoin) {
return (
{i18n('icu:GroupV2--join--already-awaiting-approval')}
);
}
if (toastType === ToastType.Blocked) {
return {i18n('icu:unblockToSend')};
}
if (toastType === ToastType.BlockedGroup) {
return {i18n('icu:unblockGroupToSend')};
}
if (toastType === ToastType.CallHistoryCleared) {
return (
{i18n('icu:CallsTab__ToastCallHistoryCleared')}
);
}
if (toastType === ToastType.CannotEditMessage) {
return (
{i18n('icu:ToastManager__CannotEditMessage_24')}
);
}
if (toastType === ToastType.CannotForwardEmptyMessage) {
return (
{i18n('icu:ForwardMessagesModal__toast--CannotForwardEmptyMessage')}
);
}
if (toastType === ToastType.CannotMixMultiAndNonMultiAttachments) {
return (
{i18n('icu:cannotSelectPhotosAndVideosAlongWithFiles')}
);
}
if (toastType === ToastType.CannotOpenGiftBadgeIncoming) {
return (
{i18n('icu:message--donation--unopened--toast--incoming')}
);
}
if (toastType === ToastType.CannotOpenGiftBadgeOutgoing) {
return (
{i18n('icu:message--donation--unopened--toast--outgoing')}
);
}
if (toastType === ToastType.CaptchaFailed) {
return {i18n('icu:verificationFailed')};
}
if (toastType === ToastType.CaptchaSolved) {
return (
{i18n('icu:verificationComplete')}
);
}
if (toastType === ToastType.CannotStartGroupCall) {
return (
{i18n('icu:GroupV2--cannot-start-group-call')}
);
}
if (toastType === ToastType.ConversationArchived) {
return (
{
onUndoArchive(String(toast.parameters.conversationId));
},
}}
>
{i18n('icu:conversationArchived')}
);
}
if (toastType === ToastType.ConversationMarkedUnread) {
return (
{i18n('icu:conversationMarkedUnread')}
);
}
if (toastType === ToastType.ConversationRemoved) {
return (
{i18n('icu:Toast--ConversationRemoved', {
title: toast.parameters.title,
})}
);
}
if (toastType === ToastType.ConversationUnarchived) {
return (
{i18n('icu:conversationReturnedToInbox')}
);
}
if (toastType === ToastType.CopiedCallLink) {
return (
{i18n('icu:calling__call-link-copied')}
);
}
if (toastType === ToastType.CopiedUsername) {
return (
{i18n('icu:ProfileEditor--username--copied-username')}
);
}
if (toastType === ToastType.CopiedUsernameLink) {
return (
{i18n('icu:ProfileEditor--username--copied-username-link')}
);
}
if (toastType === ToastType.DangerousFileType) {
return {i18n('icu:dangerousFileType')};
}
if (toastType === ToastType.DebugLogError) {
return {i18n('icu:debugLogError')};
}
if (toastType === ToastType.DeleteForEveryoneFailed) {
return (
{i18n('icu:deleteForEveryoneFailed')}
);
}
if (toastType === ToastType.Error) {
return (
window.IPC.showDebugLog(),
}}
>
{i18n('icu:Toast--error')}
);
}
if (toastType === ToastType.Expired) {
return {i18n('icu:expiredWarning')};
}
if (toastType === ToastType.FailedToDeleteUsername) {
return (
{i18n('icu:ProfileEditor--username--delete-general-error')}
);
}
if (toastType === ToastType.FailedToFetchPhoneNumber) {
return (
{i18n('icu:Toast--failed-to-fetch-phone-number')}
);
}
if (toastType === ToastType.FailedToFetchUsername) {
return (
{i18n('icu:Toast--failed-to-fetch-username')}
);
}
if (toastType === ToastType.FileSaved) {
return (
{
openFileInFolder(toast.parameters.fullPath);
},
}}
>
{i18n('icu:attachmentSaved')}
);
}
if (toastType === ToastType.FileSize) {
return (
{i18n('icu:fileSizeWarning', {
limit: toast.parameters.limit,
units: toast.parameters.units,
})}
);
}
if (toastType === ToastType.GroupLinkCopied) {
return (
{i18n('icu:GroupLinkManagement--clipboard')}
);
}
if (toastType === ToastType.DecryptionError) {
assertDev(
toast.toastType === ToastType.DecryptionError,
'Pacify typescript'
);
const { parameters } = toast;
const { deviceId, name } = parameters;
return (
{i18n('icu:decryptionErrorToast', {
name,
deviceId: String(deviceId),
})}
);
}
if (toastType === ToastType.InvalidConversation) {
return {i18n('icu:invalidConversation')};
}
if (toastType === ToastType.LeftGroup) {
return {i18n('icu:youLeftTheGroup')};
}
if (toastType === ToastType.LinkCopied) {
return {i18n('icu:debugLogLinkCopied')};
}
if (toastType === ToastType.LoadingFullLogs) {
return {i18n('icu:loading')};
}
if (toastType === ToastType.MaxAttachments) {
return {i18n('icu:maximumAttachments')};
}
if (toastType === ToastType.MessageBodyTooLong) {
return {i18n('icu:messageBodyTooLong')};
}
if (toastType === ToastType.OriginalMessageNotFound) {
return (
{i18n('icu:originalMessageNotFound')}
);
}
if (toastType === ToastType.PinnedConversationsFull) {
return (
{i18n('icu:pinnedConversationsFull')}
);
}
if (toastType === ToastType.ReactionFailed) {
return {i18n('icu:Reactions--error')};
}
if (toastType === ToastType.ReportedSpam) {
return (
{i18n('icu:MessageRequests--report-spam-success-toast')}
);
}
if (toastType === ToastType.ReportedSpamAndBlocked) {
return (
{i18n('icu:MessageRequests--block-and-report-spam-success-toast')}
);
}
if (toastType === ToastType.StickerPackInstallFailed) {
return (
{i18n('icu:stickers--toast--InstallFailed')}
);
}
if (toastType === ToastType.StoryMuted) {
return (
{i18n('icu:Stories__toast--hasNoSound')}
);
}
if (toastType === ToastType.StoryReact) {
return (
{i18n('icu:Stories__toast--sending-reaction')}
);
}
if (toastType === ToastType.StoryReply) {
return (
{i18n('icu:Stories__toast--sending-reply')}
);
}
if (toastType === ToastType.StoryVideoError) {
return (
{i18n('icu:StoryCreator__error--video-error')}
);
}
if (toastType === ToastType.StoryVideoUnsupported) {
return (
{i18n('icu:StoryCreator__error--video-unsupported')}
);
}
if (toastType === ToastType.TapToViewExpiredIncoming) {
return (
{i18n('icu:Message--tap-to-view--incoming--expired-toast')}
);
}
if (toastType === ToastType.TapToViewExpiredOutgoing) {
return (
{i18n('icu:Message--tap-to-view--outgoing--expired-toast')}
);
}
if (toastType === ToastType.TooManyMessagesToDeleteForEveryone) {
return (
{i18n(
'icu:DeleteMessagesModal__toast--TooManyMessagesToDeleteForEveryone',
{ count: toast.parameters.count }
)}
);
}
if (toastType === ToastType.TooManyMessagesToForward) {
return (
{i18n('icu:SelectModeActions__toast--TooManyMessagesToForward')}
);
}
if (toastType === ToastType.TransportError) {
return {i18n('icu:TransportError')};
}
if (toastType === ToastType.UnableToLoadAttachment) {
return (
{i18n('icu:unableToLoadAttachment')}
);
}
if (toastType === ToastType.UnsupportedMultiAttachment) {
return (
{i18n('icu:cannotSelectMultipleFileAttachments')}
);
}
if (toastType === ToastType.UnsupportedOS) {
return (
{i18n('icu:UnsupportedOSErrorToast', { OS })}
);
}
if (toastType === ToastType.UsernameRecovered) {
return (
{i18n('icu:EditUsernameModalBody__username-recovered__text', {
username: toast.parameters.username,
})}
);
}
if (toastType === ToastType.UserAddedToGroup) {
return (
{i18n('icu:AddUserToAnotherGroupModal__toast--user-added-to-group', {
contact: toast.parameters.contact,
group: toast.parameters.group,
})}
);
}
if (toastType === ToastType.VoiceNoteLimit) {
return {i18n('icu:voiceNoteLimit')};
}
if (toastType === ToastType.VoiceNoteMustBeTheOnlyAttachment) {
return (
{i18n('icu:voiceNoteMustBeOnlyAttachment')}
);
}
if (toastType === ToastType.WhoCanFindMeReadOnly) {
return (
{i18n('icu:WhoCanFindMeReadOnlyToast')}
);
}
throw missingCaseError(toastType);
}
export function renderMegaphone({
i18n,
megaphone,
}: PropsType): JSX.Element | null {
if (!megaphone) {
return null;
}
if (megaphone.type === MegaphoneType.UsernameOnboarding) {
return ;
}
throw missingCaseError(megaphone.type);
}
export function ToastManager(props: PropsType): JSX.Element {
const {
centerToast,
containerWidthBreakpoint,
isCompositionAreaVisible,
isInFullScreenCall,
} = props;
const toast = renderToast(props);
return (
{centerToast
? createPortal(
{toast}
,
document.body
)
: toast}
{renderMegaphone(props)}
);
}