Memoize toasts to unstick them in AudioCapture

This commit is contained in:
Josh Perez 2021-10-12 15:09:00 -04:00 committed by GitHub
parent f4b0bade80
commit 7488fa5abc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 77 deletions

View file

@ -1,7 +1,13 @@
// Copyright 2021 Signal Messenger, LLC // Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import React, { KeyboardEvent, MouseEvent, ReactNode, useEffect } from 'react'; import React, {
KeyboardEvent,
MouseEvent,
ReactNode,
memo,
useEffect,
} from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { createPortal } from 'react-dom'; import { createPortal } from 'react-dom';
import { useRestoreFocus } from '../hooks/useRestoreFocus'; import { useRestoreFocus } from '../hooks/useRestoreFocus';
@ -19,7 +25,8 @@ export type PropsType = {
}; };
}; };
export const Toast = ({ export const Toast = memo(
({
autoDismissDisabled = false, autoDismissDisabled = false,
children, children,
className, className,
@ -27,7 +34,7 @@ export const Toast = ({
onClose, onClose,
timeout = 8000, timeout = 8000,
toastAction, toastAction,
}: PropsType): JSX.Element | null => { }: PropsType): JSX.Element | null => {
const [root, setRoot] = React.useState<HTMLElement | null>(null); const [root, setRoot] = React.useState<HTMLElement | null>(null);
const [focusRef] = useRestoreFocus(); const [focusRef] = useRestoreFocus();
@ -103,4 +110,5 @@ export const Toast = ({
root root
) )
: null; : null;
}; }
);

View file

@ -134,9 +134,9 @@ export const AudioCapture = ({
completeRecording(conversationId, onSendAudioRecording); completeRecording(conversationId, onSendAudioRecording);
}, [conversationId, completeRecording, onSendAudioRecording]); }, [conversationId, completeRecording, onSendAudioRecording]);
function closeToast() { const closeToast = useCallback(() => {
setToastType(undefined); setToastType(undefined);
} }, []);
let toastElement: JSX.Element | undefined; let toastElement: JSX.Element | undefined;
if (toastType === ToastType.VoiceNoteLimit) { if (toastType === ToastType.VoiceNoteLimit) {