From 3b132969f331a3009688f3010b9a53219cee3e5e Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Thu, 21 Mar 2024 23:20:51 +0700 Subject: [PATCH] Fix listener is overridden --- src/components/posts/ModerationProvider.tsx | 4 +++- src/components/profile-selector/MyAccountMenu.tsx | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/components/posts/ModerationProvider.tsx b/src/components/posts/ModerationProvider.tsx index c02490c69..06da8c8d6 100644 --- a/src/components/posts/ModerationProvider.tsx +++ b/src/components/posts/ModerationProvider.tsx @@ -14,7 +14,7 @@ export default function ModerationProvider({ children }: { children: ReactNode } const iframeRef = useRef(null) useEffect(() => { - window.onmessage = event => { + const listener = (event: MessageEvent) => { const message = parseGrillMessage(event.data + '') if (!message) return @@ -23,6 +23,8 @@ export default function ModerationProvider({ children }: { children: ReactNode } setIsOpenModal(false) } } + window.addEventListener('message', listener) + return () => window.removeEventListener('message', listener) }, []) const value = useMemo(() => { diff --git a/src/components/profile-selector/MyAccountMenu.tsx b/src/components/profile-selector/MyAccountMenu.tsx index e88c551c2..ced29dbef 100644 --- a/src/components/profile-selector/MyAccountMenu.tsx +++ b/src/components/profile-selector/MyAccountMenu.tsx @@ -1,5 +1,6 @@ import { useRouter } from 'next/router' import React, { createContext, FC, useContext, useEffect, useRef, useState } from 'react' +import { isDevMode } from 'src/config/env' import { parseGrillMessage } from 'src/utils/iframe' import { getCurrentUrlOrigin } from 'src/utils/url' import { InfoDetails } from '../profiles/address-views' @@ -73,7 +74,7 @@ export const AccountMenu: React.FunctionComponent = ({ address, ow const router = useRouter() useEffect(() => { - window.onmessage = event => { + function listener(event: MessageEvent) { const message = parseGrillMessage(event.data + '') if (!message) return @@ -89,11 +90,10 @@ export const AccountMenu: React.FunctionComponent = ({ address, ow setIsOpenProfileModal(false) } } + window.addEventListener('message', listener) + return () => window.removeEventListener('message', listener) }, []) - const origin = getCurrentUrlOrigin() - const isDevMode = origin.includes('localhost') - return ( {