Skip to content

Commit

Permalink
feat: add app bridge to send message (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Najeong-Kim authored Nov 4, 2024
1 parent 1382fdf commit 9dff610
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/features/slop/ui/slop-camera.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
import { toast } from 'sonner';
import type { Position, Webcam } from '@/entities/slop/model/model';
import ArrowRightIcon from '@/shared/icons/arrow-right';
import NeutralFace from '@/shared/icons/neutral-face';
import { cn } from '@/shared/lib';
import postAppMessage from '@/shared/lib/postAppMessage';
import CameraButton from '@/shared/ui/cam-button';
import { Tooltip } from '@/shared/ui/tooltip';
import useSlopStore from '../hooks/useSlopStore';
Expand Down Expand Up @@ -45,11 +44,7 @@ const SlopCamera = ({
setOpenCamera();

if (!src) {
toast(
<>
<NeutralFace /> 선택한 웹캠은 아직 준비중 이에요
</>
);
postAppMessage('선택한 웹캠은 아직 준비중 이에요');
}
};

Expand Down
34 changes: 34 additions & 0 deletions src/shared/lib/postAppMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
declare global {
interface Window {
BRIDGE: {
sendMessage: (message: string) => void;
};
webkit: {
messageHandlers: {
weski: {
showToast: (message: string) => void;
};
};
};
}
}

const postAppMessage = (message: string) => {
const userAgent = navigator.userAgent;
const android = userAgent.match(/Android/i);
const iphone = userAgent.match(/iPhone/i);

if (android !== null) {
console.log("Android");
return window.BRIDGE.sendMessage(message);

} else if (iphone !== null) {
console.log("iOS");
return window.webkit.messageHandlers.weski.showToast(message);

} else {
return window.opener.postMessage(message);
}
}

export default postAppMessage;

0 comments on commit 9dff610

Please sign in to comment.