From 89d7f21918ced24a86abccea2b6169bf5dfa6b4c Mon Sep 17 00:00:00 2001 From: Carson Full Date: Sat, 12 Oct 2024 15:07:22 -0500 Subject: [PATCH] Broadcast impersonation changes --- src/api/client/ImpersonationContext.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/api/client/ImpersonationContext.tsx b/src/api/client/ImpersonationContext.tsx index 3663ebca20..00ebec3a8f 100644 --- a/src/api/client/ImpersonationContext.tsx +++ b/src/api/client/ImpersonationContext.tsx @@ -28,6 +28,7 @@ export const ImpersonationProvider = ({ children, initial, }: ChildrenProp & { initial?: Impersonation | null }) => { + const [broadcast] = useState(() => new BroadcastChannel('impersonation')); const [impersonation, setImpersonation] = useState(() => { if (initial !== undefined) { return initial ?? undefined; @@ -41,6 +42,7 @@ export const ImpersonationProvider = ({ : undefined; next = next && Object.keys(next).length === 0 ? undefined : next; setImpersonation(next); + broadcast.postMessage(next); if (!next) { Cookies.remove('impersonation'); return; @@ -50,7 +52,7 @@ export const ImpersonationProvider = ({ sameSite: 'strict', }); }, - [setImpersonation] + [setImpersonation, broadcast] ); const value = useMemo( () => ({ @@ -61,6 +63,18 @@ export const ImpersonationProvider = ({ [impersonation, set] ); + useEffect(() => { + const listener = (event: MessageEvent) => { + setImpersonation(event.data); + }; + broadcast.addEventListener('message', listener); + return () => { + broadcast.removeEventListener('message', listener); + broadcast.close(); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + // Expose devtools API const latest = useLatest(value); useEffect(() => {