1- import { useEffect , useState } from "react" ;
1+ import { useEffect } from "react" ;
22
33import styles from "@/components/Home/Home.module.scss" ;
44import { AppBridgeMessageType } from "@/components/provider/AppBridgeProvider/AppBridgeMessage.types" ;
55import { useAppBridge } from "@/components/provider/AppBridgeProvider/AppBridgeProvider" ;
6+ import { WebBridgeMessageType } from "@/components/provider/WebBridgeProvider/WebBridgeProvider" ;
7+ import { useWebBridge } from "@/components/provider/WebBridgeProvider/WebBridgeProvider" ;
68import IconButton from "@/components/ui/IconButton/IconButton" ;
79import Text from "@/components/ui/Text/Text" ;
810
@@ -15,33 +17,35 @@ export interface ScanResult {
1517}
1618
1719const Home = ( ) => {
18- const [ abc , setAbc ] = useState ( false ) ;
1920 const { send } = useAppBridge ( ) ;
21+ const { receive } = useWebBridge ( ) ;
2022
2123 const { scanData, setScanData } = useScanDataStore ( ) ;
2224
2325 const { navigateToReceiptEdit } = useRoute ( ) ;
2426
27+ // receive({ type: WebBridgeMessageType.RECEIVE_SCAN_RESULT, payload: scanData });
28+
2529 useEffect ( ( ) => {
26- window . response = {
27- receiveScanResult : ( jsonData : string ) => {
28- try {
29- const data : ScanResult [ ] = JSON . parse ( jsonData ) ;
30- setAbc ( true ) ;
31- setScanData ( data ) ;
32- // navigateToReceiptEdit();
33- } catch ( error ) {
34- console . error ( "Error parsing scan result JSON:" , error ) ;
35- }
36- } ,
37- } ;
38- } , [ scanData , setScanData , navigateToReceiptEdit ] ) ;
30+ // receive({ type: WebBridgeMessageType.RECEIVE_SCAN_RESULT, payload: scanData });
31+ // window.response = {
32+ // receiveScanResult: (jsonData: string) => {
33+ // try {
34+ // const data: ScanResult[] = JSON.parse(jsonData);
35+ // setAbc(true);
36+ // setScanData(data);
37+ // // navigateToReceiptEdit();
38+ // } catch (error) {
39+ // console.error("Error parsing scan result JSON:", error);
40+ // }
41+ // },
42+ // };
43+ } , [ ] ) ;
3944
4045 return (
4146 < div className = { styles . Home } >
4247 < div className = { styles . HomeTitle } >
4348 < Text variant = "titleLg" color = "gradient" align = "center" as = "h1" >
44- { abc && "스캔 완료 테스트용" }
4549 { `영수증으로\nAI 음식 리뷰 남겨요` }
4650 </ Text >
4751 < Text variant = "bodyLg" color = "secondary" align = "center" >
@@ -75,19 +79,55 @@ const Home = () => {
7579 < IconButton
7680 text = "카메라"
7781 iconName = "camera"
78- onClick = { ( ) => send ( { type : AppBridgeMessageType . OPEN_CAMERA , payload : "" } ) }
82+ onClick = { ( ) => {
83+ send ( { type : AppBridgeMessageType . OPEN_CAMERA , payload : "" } ) ;
84+
85+ // 네이티브 데이터 수신 핸들러 등록
86+ window . response = {
87+ receiveScanResult : ( jsonData : string ) => {
88+ try {
89+ const data : ScanResult [ ] = JSON . parse ( jsonData ) ;
90+ receive ( {
91+ type : WebBridgeMessageType . RECEIVE_SCAN_RESULT ,
92+ payload : data ,
93+ } ) ;
94+
95+ // 데이터 저장 및 페이지 이동
96+ setScanData ( data ) ;
97+ navigateToReceiptEdit ( ) ;
98+ } catch ( error ) {
99+ console . error ( "Error parsing scan result JSON:" , error ) ;
100+ }
101+ } ,
102+ } ;
103+ } }
79104 />
80105
81- < button
106+ { /* <button
82107 onClick={() => {
83- if ( window . response ) {
84- window . response . receiveScanResult (
85- JSON . stringify ( [ { sampleKey : "sampleValue" } , { sampleKey2 : "sampleValue2" } ] ) ,
86- ) ;
87- }
108+ receive({
109+ type: WebBridgeMessageType.RECEIVE_SCAN_RESULT,
110+ payload: [{ sampleKey: "sampleValue" }, { sampleKey2: "sampleValue2" }],
111+ });
112+ // if (window.response) {
113+ // window.response.receiveScanResult(
114+ // JSON.stringify([{ sampleKey: "sampleValue" }, { sampleKey2: "sampleValue2" }]),
115+ // );
116+ // }
88117 }}
89118 >
90119 테스트 데이터 전송
120+ </button> */ }
121+
122+ < button
123+ onClick = { ( ) => {
124+ receive ( {
125+ type : WebBridgeMessageType . RECEIVE_SCAN_RESULT ,
126+ payload : [ { sampleKey : "sampleValue" } , { sampleKey2 : "sampleValue2" } ] ,
127+ } ) ;
128+ } }
129+ >
130+ 테스트
91131 </ button >
92132 </ div >
93133 </ div >
0 commit comments