ok
This commit is contained in:
@@ -21,12 +21,15 @@ const CameraPage = ({ handleClose, handleUploadImage }) => {
|
|||||||
return () => window.removeEventListener('resize', checkMobile);
|
return () => window.removeEventListener('resize', checkMobile);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
let stream;
|
||||||
|
|
||||||
const startCamera = async () => {
|
const startCamera = async () => {
|
||||||
try {
|
try {
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia({
|
||||||
video: { facingMode: 'environment' },
|
video: { facingMode: 'environment' },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (videoRef.current) {
|
if (videoRef.current) {
|
||||||
videoRef.current.srcObject = stream;
|
videoRef.current.srcObject = stream;
|
||||||
}
|
}
|
||||||
@@ -34,15 +37,17 @@ const CameraPage = ({ handleClose, handleUploadImage }) => {
|
|||||||
console.error('Error accessing camera:', error);
|
console.error('Error accessing camera:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
startCamera();
|
startCamera();
|
||||||
|
|
||||||
|
// Clean-up logic (seperti socket.off)
|
||||||
return () => {
|
return () => {
|
||||||
if (videoRef.current && videoRef.current.srcObject) {
|
if (stream) {
|
||||||
const tracks = videoRef.current.srcObject.getTracks();
|
stream.getTracks().forEach(track => track.stop());
|
||||||
tracks.forEach(track => track.stop());
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
const captureImage = () => {
|
const captureImage = () => {
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import styles from './ChatBot.module.css';
|
import styles from './ChatBot.module.css';
|
||||||
import Camera from './Camera';
|
import Camera from './Camera';
|
||||||
|
import { useSearchParams, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
const ChatBot = ({ existingConversation }) => {
|
const ChatBot = ({ existingConversation }) => {
|
||||||
const [messages, setMessages] = useState([
|
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 [input, setInput] = useState('');
|
||||||
const [isLoading, setIsLoading] = useState('');
|
const [isLoading, setIsLoading] = useState('');
|
||||||
const [isPoppedUp, setIsPoppedUp] = useState('');
|
const [isPoppedUp, setIsPoppedUp] = useState('');
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const [phoneNumber, setPhoneNumber] = useState('');
|
const [phoneNumber, setPhoneNumber] = useState('');
|
||||||
const [isOpenCamera, setIsOpenCamera] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (existingConversation && existingConversation.length > 0) {
|
if (existingConversation && existingConversation.length > 0) {
|
||||||
@@ -114,8 +119,7 @@ const ChatBot = ({ existingConversation }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleUploadImage = async (img) => {
|
const handleUploadImage = async (img) => {
|
||||||
setIsOpenCamera(false);
|
setSearchParams({ })
|
||||||
|
|
||||||
const newMessages = [
|
const newMessages = [
|
||||||
...messages,
|
...messages,
|
||||||
{ sender: 'user', img: img, time: getTime() },
|
{ sender: 'user', img: img, time: getTime() },
|
||||||
@@ -228,7 +232,7 @@ const ChatBot = ({ existingConversation }) => {
|
|||||||
? (msg.text ?
|
? (msg.text ?
|
||||||
msg.text
|
msg.text
|
||||||
:
|
:
|
||||||
<img style={{ height: '160px', borderRadius: '12px' }} src={msg.img} />
|
<img style={{ maxHeight: '300px', maxWidth: '240px', borderRadius: '12px' }} src={msg.img} />
|
||||||
)
|
)
|
||||||
: (() => {
|
: (() => {
|
||||||
try {
|
try {
|
||||||
@@ -250,7 +254,7 @@ const ChatBot = ({ existingConversation }) => {
|
|||||||
))}
|
))}
|
||||||
<div
|
<div
|
||||||
className={styles.quickReply}
|
className={styles.quickReply}
|
||||||
onClick={() => setIsOpenCamera(true)}
|
onClick={() => setSearchParams({ camera: 'open' })}
|
||||||
style={{ color: 'white', backgroundColor: '#075e54', display: 'flex', flexDirection: 'row', alignItems: 'center' }}
|
style={{ color: 'white', backgroundColor: '#075e54', display: 'flex', flexDirection: 'row', alignItems: 'center' }}
|
||||||
>
|
>
|
||||||
<img style={{ marginRight: '5px', height: '14px', filter: 'invert(1)' }} src={'/camera.png'} />
|
<img style={{ marginRight: '5px', height: '14px', filter: 'invert(1)' }} src={'/camera.png'} />
|
||||||
@@ -282,7 +286,7 @@ const ChatBot = ({ existingConversation }) => {
|
|||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button onClick={() => setIsOpenCamera(true)} disabled={isLoading!=''}>
|
<button onClick={() => setSearchParams({ camera: 'open' })} disabled={isLoading!=''}>
|
||||||
<img
|
<img
|
||||||
src="/camera.png"
|
src="/camera.png"
|
||||||
alt="Kamera"
|
alt="Kamera"
|
||||||
@@ -345,7 +349,7 @@ const ChatBot = ({ existingConversation }) => {
|
|||||||
|
|
||||||
{isOpenCamera && (
|
{isOpenCamera && (
|
||||||
<Camera
|
<Camera
|
||||||
handleClose={() => setIsOpenCamera(false)}
|
handleClose={() => setSearchParams({})}
|
||||||
handleUploadImage={(e) => handleUploadImage(e)}
|
handleUploadImage={(e) => handleUploadImage(e)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user