From ae11a094eb77bc70b83c353c71f02bf39f7cc998 Mon Sep 17 00:00:00 2001 From: everythingonblack Date: Mon, 7 Jul 2025 11:32:03 +0700 Subject: [PATCH] ok --- src/Camera.js | 45 +++++++++++++++++++++++++-------------------- src/ChatBot.js | 18 +++++++++++------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Camera.js b/src/Camera.js index a498fbb..71c44d4 100644 --- a/src/Camera.js +++ b/src/Camera.js @@ -21,28 +21,33 @@ const CameraPage = ({ handleClose, handleUploadImage }) => { return () => window.removeEventListener('resize', checkMobile); }, []); - useEffect(() => { - const startCamera = async () => { - try { - const stream = await navigator.mediaDevices.getUserMedia({ - video: { facingMode: 'environment' }, - }); - if (videoRef.current) { - videoRef.current.srcObject = stream; - } - } catch (error) { - console.error('Error accessing camera:', error); - } - }; - startCamera(); +useEffect(() => { + let stream; - return () => { - if (videoRef.current && videoRef.current.srcObject) { - const tracks = videoRef.current.srcObject.getTracks(); - tracks.forEach(track => track.stop()); + const startCamera = async () => { + try { + stream = await navigator.mediaDevices.getUserMedia({ + video: { facingMode: 'environment' }, + }); + + if (videoRef.current) { + videoRef.current.srcObject = stream; } - }; - }, []); + } catch (error) { + console.error('Error accessing camera:', error); + } + }; + + startCamera(); + + // Clean-up logic (seperti socket.off) + return () => { + if (stream) { + stream.getTracks().forEach(track => track.stop()); + } + }; +}, []); + const captureImage = () => { const canvas = canvasRef.current; diff --git a/src/ChatBot.js b/src/ChatBot.js index 7d96478..03af59b 100644 --- a/src/ChatBot.js +++ b/src/ChatBot.js @@ -1,6 +1,7 @@ import React, { useState, useEffect } from 'react'; import styles from './ChatBot.module.css'; import Camera from './Camera'; +import { useSearchParams, useNavigate } from 'react-router-dom'; const ChatBot = ({ existingConversation }) => { const [messages, setMessages] = useState([ @@ -15,12 +16,16 @@ const ChatBot = ({ existingConversation }) => { }, ]); + const [searchParams, setSearchParams] = useSearchParams(); +const navigate = useNavigate(); + +const isOpenCamera = searchParams.get('camera') === 'open'; + const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(''); const [isPoppedUp, setIsPoppedUp] = useState(''); const [name, setName] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); - const [isOpenCamera, setIsOpenCamera] = useState(false); useEffect(() => { if (existingConversation && existingConversation.length > 0) { @@ -114,8 +119,7 @@ const ChatBot = ({ existingConversation }) => { }; const handleUploadImage = async (img) => { - setIsOpenCamera(false); - + setSearchParams({ }) const newMessages = [ ...messages, { sender: 'user', img: img, time: getTime() }, @@ -228,7 +232,7 @@ const ChatBot = ({ existingConversation }) => { ? (msg.text ? msg.text : - + ) : (() => { try { @@ -250,7 +254,7 @@ const ChatBot = ({ existingConversation }) => { ))}
setIsOpenCamera(true)} + onClick={() => setSearchParams({ camera: 'open' })} style={{ color: 'white', backgroundColor: '#075e54', display: 'flex', flexDirection: 'row', alignItems: 'center' }} > @@ -282,7 +286,7 @@ const ChatBot = ({ existingConversation }) => { /> -