Spaces:
Running
Running
| const sidebar = document.getElementById('sidebar'); | |
| const toggleBtn = document.getElementById('toggle-sidebar'); | |
| const userInput = document.getElementById('user-input'); | |
| const sendBtn = document.getElementById('send-btn'); | |
| const chatWindow = document.getElementById('chat-window'); | |
| // إخفاء وإظهار الشريط الجانبي | |
| toggleBtn.onclick = () => sidebar.classList.toggle('hidden'); | |
| // تحميل المحادثات من المتصفح | |
| let currentChat = JSON.parse(localStorage.getItem('genisi_chats')) || []; | |
| async function sendMessage() { | |
| const text = userInput.value.trim(); | |
| if (!text) return; | |
| appendMessage('user', text); | |
| userInput.value = ''; | |
| const response = await fetch('/chat', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({ message: text, history: currentChat }) | |
| }); | |
| const data = await response.json(); | |
| appendMessage('bot', data.response); | |
| // حفظ في ذاكرة المتصفح | |
| currentChat.push({user: text, bot: data.response}); | |
| localStorage.setItem('genisi_chats', JSON.stringify(currentChat)); | |
| } | |
| function appendMessage(role, text) { | |
| const div = document.createElement('div'); | |
| div.className = role === 'user' ? 'msg-user' : 'msg-bot'; | |
| div.innerText = text; | |
| chatWindow.appendChild(div); | |
| chatWindow.scrollTop = chatWindow.scrollHeight; | |
| } | |
| sendBtn.onclick = sendMessage; |