From 341d532e11abf7ec3a0d74cdfab47665c30f6319 Mon Sep 17 00:00:00 2001 From: Rob Knight Date: Tue, 24 Sep 2024 17:13:38 +0100 Subject: [PATCH] Add listen modes for embedded vs. embedding --- .../screens/ZappScreens/ZappScreen.tsx | 2 ++ apps/passport-client/pages/index.tsx | 3 +-- .../passport-client/src/zapp/useZappServer.ts | 25 ++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/passport-client/components/screens/ZappScreens/ZappScreen.tsx b/apps/passport-client/components/screens/ZappScreens/ZappScreen.tsx index 9c07e0bdd4..bc8967b3f7 100644 --- a/apps/passport-client/components/screens/ZappScreens/ZappScreen.tsx +++ b/apps/passport-client/components/screens/ZappScreens/ZappScreen.tsx @@ -1,5 +1,6 @@ import { ReactNode } from "react"; import { useDispatch, useEmbeddedScreenState } from "../../../src/appHooks"; +import { ListenMode, useZappServer } from "../../../src/zapp/useZappServer"; import { AdhocModal } from "../../modals/AdhocModal"; import { EmbeddedScreen } from "../EmbeddedScreens/EmbeddedScreen"; @@ -17,6 +18,7 @@ export function ZappScreen({ url }: { url: string }): ReactNode { } function ZappModal(): ReactNode { + useZappServer(ListenMode.LISTEN_IF_NOT_EMBEDDED); const embeddedScreen = useEmbeddedScreenState(); const dispatch = useDispatch(); return ( diff --git a/apps/passport-client/pages/index.tsx b/apps/passport-client/pages/index.tsx index 0214ecb497..736259a97c 100644 --- a/apps/passport-client/pages/index.tsx +++ b/apps/passport-client/pages/index.tsx @@ -62,11 +62,10 @@ import { Emitter } from "../src/emitter"; import { loadInitialState } from "../src/loadInitialState"; import { registerServiceWorker } from "../src/registerServiceWorker"; import { AppState, StateEmitter } from "../src/state"; -import { useZappServer } from "../src/zapp/useZappServer"; function App(): JSX.Element { useBackgroundJobs(); - useZappServer(); + // useZappServer(); const state = useStateContext().getState(); const hasStack = !!state.error?.stack; diff --git a/apps/passport-client/src/zapp/useZappServer.ts b/apps/passport-client/src/zapp/useZappServer.ts index a24bf948b5..2f3d0dbadd 100644 --- a/apps/passport-client/src/zapp/useZappServer.ts +++ b/apps/passport-client/src/zapp/useZappServer.ts @@ -8,13 +8,31 @@ import { v4 as uuidv4 } from "uuid"; import { useStateContext } from "../appHooks"; import { ZupassRPCProcessor } from "./ZappServer"; -export function useZappServer(): void { +export enum ListenMode { + LISTEN_IF_EMBEDDED, + LISTEN_IF_NOT_EMBEDDED +} + +export function useZappServer( + mode: ListenMode = ListenMode.LISTEN_IF_EMBEDDED +): void { const context = useStateContext(); useEffect(() => { + if ( + mode === ListenMode.LISTEN_IF_EMBEDDED && + window.parent === window.self + ) { + return; + } + if ( + mode === ListenMode.LISTEN_IF_NOT_EMBEDDED && + window.parent !== window.self + ) { + return; + } (async (): Promise => { const { zapp, advice, origin } = await listen(); - console.log("Zapp connected", zapp, advice, origin); if (!context.getState().self) { advice.showClient(); @@ -94,9 +112,8 @@ export function useZappServer(): void { window.location.hash = "embedded"; } - console.log("Zapp ready"); advice.ready(server); } })(); - }, [context]); + }, [context, mode]); }