From bcec9d7298c4b00edf04931f0b25c2c6ef5b5b31 Mon Sep 17 00:00:00 2001 From: Vicky Chen Date: Thu, 5 Sep 2024 14:33:03 -0400 Subject: [PATCH] add photo thumbnail to the top right corner --- client/components/photo/photo.tsx | 11 +++++++++++ client/pages/index.tsx | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 client/components/photo/photo.tsx diff --git a/client/components/photo/photo.tsx b/client/components/photo/photo.tsx new file mode 100644 index 0000000..ab3a046 --- /dev/null +++ b/client/components/photo/photo.tsx @@ -0,0 +1,11 @@ +import Image from 'next/image'; + +export const Photo = ({ img }: { img: string }) => { + if (!img) return null; + + return ( +
+ Received image +
+ ); +}; diff --git a/client/pages/index.tsx b/client/pages/index.tsx index ad04ff4..f4b1603 100644 --- a/client/pages/index.tsx +++ b/client/pages/index.tsx @@ -5,6 +5,7 @@ import CommentFeed from '@/components/comments/commentFeed'; import { ResponseData } from '../../shared/types'; import { LoadingOverlay } from '@/components/loadingOverlay/loadingOverlay'; import { EndingOverlay } from '@/components/endingOverlay/endingOverlay'; +import { Photo } from '@/components/photo/photo'; const Home = () => { const intervalRef = useRef | null>(null); // test timer @@ -17,6 +18,7 @@ const Home = () => { const [commentsComplete, setCommentsComplete] = useState(false); const [isLoading, setIsLoading] = useState(true); + const [img, setImg] = useState(''); useEffect(() => { console.log('Connecting Socket'); @@ -43,6 +45,11 @@ const Home = () => { } }; + const onSendPhoto = (data: string) => { + console.log('Received image data'); + setImg(`data:image/jpeg;base64,${data}`); + }; + const onErrorMessage = (msg: string) => { // on error take a new photo console.error(msg); @@ -51,6 +58,7 @@ const Home = () => { socket.on('connect', onConnect); socket.on('api_response', onApiResonse); + socket.on('send_photo', onSendPhoto); socket.on('err_msg', onErrorMessage); return () => { @@ -59,6 +67,7 @@ const Home = () => { // clean up socket event listeners socket.off('connect', onConnect); socket.off('api_response', onApiResonse); + socket.off('send_photo', onSendPhoto); if (intervalRef.current) { clearInterval(intervalRef.current); @@ -76,7 +85,7 @@ const Home = () => { console.log('Likes and comments animation completed'); setLikesComplete(false); setCommentsComplete(false); - + setImg(''); donePhoto(); } // fire useEffect when either state vars change @@ -129,6 +138,7 @@ const Home = () => { )} )} + {img && !(likesComplete && commentsComplete) && } );