Merge pull request #2441 from Feodoric/fix-duplicate-messages

fix #2174 - icarus message will no longer stack infinitely
This commit is contained in:
hydroflame 2022-01-08 12:42:19 -05:00 committed by GitHub
commit 30f5559bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,12 +3,14 @@ import { EventEmitter } from "../../utils/EventEmitter";
import { Modal } from "../../ui/React/Modal"; import { Modal } from "../../ui/React/Modal";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import {sha256} from "js-sha256";
export const AlertEvents = new EventEmitter<[string | JSX.Element]>(); export const AlertEvents = new EventEmitter<[string | JSX.Element]>();
interface Alert { interface Alert {
id: string; id: string;
text: string | JSX.Element; text: string | JSX.Element;
hash: string;
} }
let i = 0; let i = 0;
@ -20,11 +22,17 @@ export function AlertManager(): React.ReactElement {
const id = i + ""; const id = i + "";
i++; i++;
setAlerts((old) => { setAlerts((old) => {
const hash = getMessageHash(text);
if (old.some(a => a.hash === hash)) {
console.log('Duplicate message');
return old;
}
return [ return [
...old, ...old,
{ {
id: id, id: id,
text: text, text: text,
hash: hash,
}, },
]; ];
}); });
@ -42,6 +50,11 @@ export function AlertManager(): React.ReactElement {
return () => document.removeEventListener("keydown", handle); return () => document.removeEventListener("keydown", handle);
}, []); }, []);
function getMessageHash(text: string | JSX.Element): string {
if (typeof text === 'string') return sha256(text);
return sha256(JSON.stringify(text.props));
}
function close(): void { function close(): void {
setAlerts((old) => { setAlerts((old) => {
return old.slice(1, 1e99); return old.slice(1, 1e99);