Skip to content

Commit

Permalink
feat: 클라 -> 웹 영수증 OCR 결과 브릿지 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrin-byte committed Feb 7, 2025
1 parent 1264ff5 commit d75dc05
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const iosHandlers = {
};

const androidHandlers = {
[AppBridgeMessageType.OPEN_CAMERA]: () => window.AndroidBridge?.openCamera(),
[AppBridgeMessageType.OPEN_GALLERY]: () => window.AndroidBridge?.openGallery(),
[AppBridgeMessageType.SHARE]: () => window.AndroidBridge?.share(),
[AppBridgeMessageType.OPEN_CAMERA]: (message: string) =>
window.AndroidBridge?.openCamera(message),
[AppBridgeMessageType.OPEN_GALLERY]: (message: string) =>
window.AndroidBridge?.openGallery(message),
[AppBridgeMessageType.SHARE]: (message: string) => window.AndroidBridge?.share(message),
[AppBridgeMessageType.CREATE_REVIEW]: (message: { payload: { json: string } }) =>
window.AndroidBridge?.createReview(message.payload.json),
[AppBridgeMessageType.COPY]: (message: { payload: { json: string } }) =>
Expand Down
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 src/components/provider/WebBridgeProvider/WebBridgeProvider.tsx
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;
}
6 changes: 3 additions & 3 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ declare global {
};
};
AndroidBridge?: {
openCamera: () => void;
openGallery: () => void;
share: () => void;
openCamera: (request: string) => void;
openGallery: (request: string) => void;
share: (request: string) => void;
createReview: (json: string) => void;
copy: (json: string) => void;
};
Expand Down

0 comments on commit d75dc05

Please sign in to comment.