File tree Expand file tree Collapse file tree 4 files changed +75
-6
lines changed
Expand file tree Collapse file tree 4 files changed +75
-6
lines changed Original file line number Diff line number Diff line change @@ -15,9 +15,11 @@ const iosHandlers = {
1515} ;
1616
1717const androidHandlers = {
18- [ AppBridgeMessageType . OPEN_CAMERA ] : ( ) => window . AndroidBridge ?. openCamera ( ) ,
19- [ AppBridgeMessageType . OPEN_GALLERY ] : ( ) => window . AndroidBridge ?. openGallery ( ) ,
20- [ AppBridgeMessageType . SHARE ] : ( ) => window . AndroidBridge ?. share ( ) ,
18+ [ AppBridgeMessageType . OPEN_CAMERA ] : ( message : string ) =>
19+ window . AndroidBridge ?. openCamera ( message ) ,
20+ [ AppBridgeMessageType . OPEN_GALLERY ] : ( message : string ) =>
21+ window . AndroidBridge ?. openGallery ( message ) ,
22+ [ AppBridgeMessageType . SHARE ] : ( message : string ) => window . AndroidBridge ?. share ( message ) ,
2123 [ AppBridgeMessageType . CREATE_REVIEW ] : ( message : { payload : { json : string } } ) =>
2224 window . AndroidBridge ?. createReview ( message . payload . json ) ,
2325 [ AppBridgeMessageType . COPY ] : ( message : { payload : { json : string } } ) =>
Original file line number Diff line number Diff line change 1+ export enum WebBridgeMessageType {
2+ RECEIVE_SCAN_RESULT = "RECEIVE_SCAN_RESULT" ,
3+ }
4+
5+ export type WebBridgeMessage = ReceiveScanResultMessage ;
6+
7+ export interface ReceiveScanResultMessage {
8+ type : WebBridgeMessageType . RECEIVE_SCAN_RESULT ;
9+ }
Original file line number Diff line number Diff line change 1+ import type { ReactNode } from "react" ;
2+ import { createContext , useContext , useEffect } from "react" ;
3+
4+ declare global {
5+ interface Window {
6+ response : {
7+ receiveScanResult : ( jsonData : string ) => void ;
8+ } ;
9+ }
10+ }
11+ // window.response = window.response || {};
12+
13+ interface WebBridgeMessage {
14+ type : string ;
15+ payload ?: unknown ;
16+ }
17+
18+ interface WebBridge {
19+ receive : ( message : WebBridgeMessage ) => void ;
20+ }
21+
22+ interface WebBridgeProviderProps {
23+ children : ReactNode ;
24+ }
25+
26+ export const WebBridgeContext = createContext < null | WebBridge > ( null ) ;
27+
28+ export function WebBridgeProvider ( { children } : WebBridgeProviderProps ) {
29+ const receive = ( message : WebBridgeMessage ) => {
30+ try {
31+ if ( typeof window !== "undefined" && window . response ) {
32+ if ( typeof window . response . receiveScanResult === "function" ) {
33+ window . response . receiveScanResult ( JSON . stringify ( message . payload ) ) ;
34+ } else {
35+ console . warn ( "window.response.receiveScanResult is not available." ) ;
36+ }
37+ }
38+ } catch ( error ) {
39+ console . error ( "WebBridge API call failed:" , error ) ;
40+ }
41+ } ;
42+
43+ useEffect ( ( ) => {
44+ window . response = window . response || { } ;
45+ } , [ ] ) ;
46+
47+ return < WebBridgeContext . Provider value = { { receive } } > { children } </ WebBridgeContext . Provider > ;
48+ }
49+
50+ export function useWebBridge ( ) {
51+ const webBridge = useContext ( WebBridgeContext ) ;
52+
53+ if ( webBridge == null ) {
54+ throw new Error ( "Wrap Web Bridge Provider" ) ;
55+ }
56+
57+ return webBridge ;
58+ }
Original file line number Diff line number Diff line change @@ -21,9 +21,9 @@ declare global {
2121 } ;
2222 } ;
2323 AndroidBridge ?: {
24- openCamera : ( ) => void ;
25- openGallery : ( ) => void ;
26- share : ( ) => void ;
24+ openCamera : ( request : string ) => void ;
25+ openGallery : ( request : string ) => void ;
26+ share : ( request : string ) => void ;
2727 createReview : ( json : string ) => void ;
2828 copy : ( json : string ) => void ;
2929 } ;
You can’t perform that action at this time.
0 commit comments