mirror of
https://github.com/owncast/owncast.git
synced 2024-11-22 12:49:37 +03:00
remove excess resize event listeners (#3169)
We add a resize handler to the window when the ChatContainer is created. If a second ChatContainer is created due to React redrawing, remove the old handler. Co-authored-by: janWilejan <>
This commit is contained in:
parent
34b531b214
commit
14baef4e36
1 changed files with 12 additions and 2 deletions
|
@ -33,6 +33,8 @@ export type ChatContainerProps = {
|
|||
desktop?: boolean;
|
||||
};
|
||||
|
||||
let resizeWindowCallback: () => void;
|
||||
|
||||
function shouldCollapseMessages(message: ChatMessage, previous: ChatMessage): boolean {
|
||||
if (!message || !message.user) {
|
||||
return false;
|
||||
|
@ -290,13 +292,21 @@ export const ChatContainer: FC<ChatContainerProps> = ({
|
|||
}
|
||||
|
||||
// Re-clamp the chat size whenever the window resizes
|
||||
window.addEventListener('resize', () => {
|
||||
function resize() {
|
||||
const container = desktop && document.getElementById('chat-container');
|
||||
if (container) {
|
||||
const currentWidth = parseFloat(container.style.width) || defaultChatWidth;
|
||||
container.style.width = `${clampChatWidth(currentWidth)}px`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (resizeWindowCallback) window.removeEventListener('resize', resizeWindowCallback);
|
||||
if (desktop) {
|
||||
window.addEventListener('resize', resize);
|
||||
resizeWindowCallback = resize;
|
||||
} else {
|
||||
resizeWindowCallback = null;
|
||||
}
|
||||
|
||||
return (
|
||||
<ErrorBoundary
|
||||
|
|
Loading…
Reference in a new issue