Skip to content

Commit

Permalink
Broadcast impersonation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Oct 14, 2024
1 parent 5e58b44 commit 89d7f21
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/api/client/ImpersonationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,7 +52,7 @@ export const ImpersonationProvider = ({
sameSite: 'strict',
});
},
[setImpersonation]
[setImpersonation, broadcast]
);
const value = useMemo(
() => ({
Expand All @@ -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(() => {
Expand Down

0 comments on commit 89d7f21

Please sign in to comment.