File size: 6,572 Bytes
77db471 dd8b5df 77db471 8b2fcca 77db471 8b2fcca 77db471 41d60be 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca dd8b5df 8b2fcca 77db471 8b2fcca 77db471 8b2fcca 77db471 8b2fcca 77db471 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 77db471 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 8b2fcca 41d60be 77db471 20bd18d 77db471 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
import React, { useState, useRef, useEffect } from 'react';
import ReactMarkdown from 'react-markdown';
import Avatar from './Avatar.jsx';
import '../App.css';
const ChatInterface = ({ messages = [], setMessages = () => {}, onMessageSent = () => {}, activeConversationId,
saveBotResponse, toLogin }) => {
const [inputMessage, setInputMessage] = useState('');
const [isLoading, setIsLoading] = useState(false);
const messagesEndRef = useRef(null);
const textareaRef = useRef(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
useEffect(scrollToBottom, [messages]);
const sendMessage = async (message) => {
try {
setIsLoading(true);
// Ajouter le message utilisateur à l'interface
setMessages(prev => [...prev, { sender: 'user', text: message }]);
// IMPORTANT: Obtenir l'ID de conversation mis à jour (nouvelle ou existante)
const updatedConversationId = await onMessageSent(message);
// Appel à l'API de chat
const chatRes = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ message }),
});
if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`);
const { response: botResponse } = await chatRes.json();
// Ajouter la réponse du bot à l'interface
setMessages(prev => [...prev, { sender: 'bot', text: botResponse }]);
// Utiliser l'ID de conversation mis à jour
if (updatedConversationId) {
saveBotResponse(updatedConversationId, botResponse);
} else if (activeConversationId) {
saveBotResponse(activeConversationId, botResponse);
}
} catch (error) {
console.error('Erreur:', error);
setMessages(prev => [...prev,
{ sender: 'user', text: message },
{ sender: 'bot', text: "Désolé, une erreur s'est produite. Veuillez réessayer." }
]);
} finally {
setIsLoading(false);
}
};
const handleSubmit = (e) => {
e.preventDefault();
const txt = inputMessage.trim();
if (!txt) return;
sendMessage(txt);
setInputMessage('');
if (textareaRef.current) textareaRef.current.style.height = 'auto';
};
const isMarkdown = (text) => /[#*_>`-]/.test(text);
return (
<div className="chat-container">
{messages.length === 0 ? (
<>
<div className="chat-header">
<h2 className="chat-title">Medic.ial</h2>
<Avatar onClick={toLogin} />
</div>
<div className="no-messages-view">
<div className="welcome-content">
<div className="welcome-message">
<p>Bonjour ! Comment puis-je vous aider aujourd'hui ? 🧑⚕️</p>
</div>
<div className="input-container centered">
<form onSubmit={handleSubmit} className="input-form">
<textarea
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
placeholder="Posez une question..."
disabled={isLoading}
rows="1"
ref={textareaRef}
className="input-textarea"
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
}}
onInput={(e) => {
e.target.style.height = 'auto';
e.target.style.height = `${e.target.scrollHeight}px`;
}}
/>
<button type="submit" disabled={isLoading || !inputMessage.trim()}>
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#e3e3e3">
<path d="M120-160v-240l320-80-320-80v-240l760 320-760 320Z"/>
</svg>
</button>
</form>
</div>
</div>
</div>
</>
) : (
<>
<div className="chat-header">
<Avatar onClick={toLogin} />
<h2 className="chat-title">Medic.ial</h2>
</div>
<div className="messages-container">
{messages.map((msg, index) => (
<div key={index} className={`message ${msg.sender}`}>
<div className="message-content">
{isMarkdown(msg.text) ? <ReactMarkdown>{msg.text}</ReactMarkdown> : msg.text}
</div>
</div>
))}
{isLoading && (
<div className="message bot">
<div className="message-content loading">
<span>.</span><span>.</span><span>.</span>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
<div className="input-container">
<form onSubmit={handleSubmit} className="input-form">
<textarea
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
placeholder="Tapez votre message ici..."
disabled={isLoading}
rows="1"
ref={textareaRef}
className="input-textarea"
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
}}
onInput={(e) => {
e.target.style.height = 'auto';
e.target.style.height = `${e.target.scrollHeight}px`;
}}
/>
<button type="submit" disabled={isLoading || !inputMessage.trim()}>
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#e3e3e3">
<path d="M120-160v-240l320-80-320-80v-240l760 320-760 320Z"/>
</svg>
</button>
</form>
<figcaption className="disclaimer-text">
Medic.ial est sujet à faire des erreurs. Vérifiez les informations fournies.
</figcaption>
</div>
</>
)}
</div>
);
};
export default ChatInterface;
|