Skip to content

Commit

Permalink
add photo thumbnail to the top right corner
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicky Chen committed Sep 5, 2024
1 parent e21e86b commit bcec9d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions client/components/photo/photo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Image from 'next/image';

export const Photo = ({ img }: { img: string }) => {
if (!img) return null;

return (
<div className="fixed top-4 right-4 border border-gray-300 shadow-md">
<Image id="image" alt="Received image" src={img} width={400} height={400} />
</div>
);
};
12 changes: 11 additions & 1 deletion client/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof setTimeout> | null>(null); // test timer
Expand All @@ -17,6 +18,7 @@ const Home = () => {
const [commentsComplete, setCommentsComplete] = useState<boolean>(false);

const [isLoading, setIsLoading] = useState<boolean>(true);
const [img, setImg] = useState<string>('');

useEffect(() => {
console.log('Connecting Socket');
Expand All @@ -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);
Expand All @@ -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 () => {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -129,6 +138,7 @@ const Home = () => {
)}
</>
)}
{img && !(likesComplete && commentsComplete) && <Photo img={img} />}
</div>
</>
);
Expand Down

0 comments on commit bcec9d7

Please sign in to comment.