-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1264ff5
commit d75dc05
Showing
4 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/components/provider/WebBridgeProvider/WebBridgeMessage.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum WebBridgeMessageType { | ||
RECEIVE_SCAN_RESULT = "RECEIVE_SCAN_RESULT", | ||
} | ||
|
||
export type WebBridgeMessage = ReceiveScanResultMessage; | ||
|
||
export interface ReceiveScanResultMessage { | ||
type: WebBridgeMessageType.RECEIVE_SCAN_RESULT; | ||
} |
58 changes: 58 additions & 0 deletions
58
src/components/provider/WebBridgeProvider/WebBridgeProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { ReactNode } from "react"; | ||
import { createContext, useContext, useEffect } from "react"; | ||
|
||
declare global { | ||
interface Window { | ||
response: { | ||
receiveScanResult: (jsonData: string) => void; | ||
}; | ||
} | ||
} | ||
// window.response = window.response || {}; | ||
|
||
interface WebBridgeMessage { | ||
type: string; | ||
payload?: unknown; | ||
} | ||
|
||
interface WebBridge { | ||
receive: (message: WebBridgeMessage) => void; | ||
} | ||
|
||
interface WebBridgeProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const WebBridgeContext = createContext<null | WebBridge>(null); | ||
|
||
export function WebBridgeProvider({ children }: WebBridgeProviderProps) { | ||
const receive = (message: WebBridgeMessage) => { | ||
try { | ||
if (typeof window !== "undefined" && window.response) { | ||
if (typeof window.response.receiveScanResult === "function") { | ||
window.response.receiveScanResult(JSON.stringify(message.payload)); | ||
} else { | ||
console.warn("window.response.receiveScanResult is not available."); | ||
} | ||
} | ||
} catch (error) { | ||
console.error("WebBridge API call failed:", error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.response = window.response || {}; | ||
}, []); | ||
|
||
return <WebBridgeContext.Provider value={{ receive }}>{children}</WebBridgeContext.Provider>; | ||
} | ||
|
||
export function useWebBridge() { | ||
const webBridge = useContext(WebBridgeContext); | ||
|
||
if (webBridge == null) { | ||
throw new Error("Wrap Web Bridge Provider"); | ||
} | ||
|
||
return webBridge; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters