Spaces:
Sleeping
Sleeping
File size: 9,894 Bytes
70d956a |
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
// === Yardımcı Fonksiyonlar ===
const qs = (selector) => document.querySelector(selector);
const qsa = (selector) => document.querySelectorAll(selector);
// === DOM Elemanları ===
const sidebar = qs('#sidebar');
const openSidebarBtn = qs('#open-sidebar');
const closeSidebarBtn = qs('#close-sidebar');
const chatForm = qs('#chat-form');
const messageInput = qs('#message-input');
const messagesContainer = qs('#messages');
const newChatBtn = qs('#new-chat');
const chatHistoryContainer = qs('#chat-history');
const toggleThemeBtn = qs('#toggle-theme');
const emojiPickerBtn = qs('#emoji-picker-btn');
const emojiPicker = qs('#emoji-picker');
const usernameInput = qs('#username-input');
const themeSelector = qs('#theme-selector');
const themePreview = qs('#theme-preview');
const chatSearch = qs('#chat-search');
const bubbleStyle = qs('#bubble-style');
const chatContainer = qs('#chat-container') || messagesContainer;
let chats = JSON.parse(localStorage.getItem('chats')) || [];
let currentChatId = null;
let currentUser = localStorage.getItem('username') || 'Kullanıcı';
let lastSender = null;
let messageDraft = localStorage.getItem('messageDraft') || '';
// === Tema Renkleri ===
const themeColors = {
default: '#4f46e5', blue: '#3b82f6', green: '#10b981', purple: '#8b5cf6', red: '#ef4444'
};
// === Başlangıç Ayarları ===
function initializeSettings() {
usernameInput.value = currentUser;
messageInput.value = messageDraft;
if (localStorage.getItem('theme') === 'dark') document.body.classList.add('dark');
const savedColor = localStorage.getItem('colorTheme') || 'default';
const savedBubble = localStorage.getItem('bubbleStyle') || 'rounded';
themeSelector.value = savedColor;
bubbleStyle.value = savedBubble;
themePreview.style.background = themeColors[savedColor];
document.body.classList.add(`theme-${savedColor}`);
document.documentElement.style.setProperty('--bubble-radius', savedBubble === 'rounded' ? '12px' : '4px');
}
// === Tema Değiştirici ===
function applyTheme(theme) {
document.body.className = document.body.className.replace(/theme-\w+/g, '');
document.body.classList.add(`theme-${theme}`);
themePreview.style.background = themeColors[theme];
localStorage.setItem('colorTheme', theme);
}
// === Karanlık Mod Değiştirici ===
toggleThemeBtn?.addEventListener('click', () => {
document.body.classList.toggle('dark');
localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light');
});
// === Event Listeners ===
usernameInput.addEventListener('change', () => {
currentUser = usernameInput.value.trim() || 'Kullanıcı';
localStorage.setItem('username', currentUser);
renderChatHistory();
});
messageInput.addEventListener('input', () => {
localStorage.setItem('messageDraft', messageInput.value);
});
themeSelector.addEventListener('change', () => applyTheme(themeSelector.value));
themeSelector.addEventListener('mouseover', () => themePreview.style.background = themeColors[themeSelector.value]);
bubbleStyle.addEventListener('change', () => {
const radius = bubbleStyle.value === 'rounded' ? '12px' : '4px';
document.documentElement.style.setProperty('--bubble-radius', radius);
localStorage.setItem('bubbleStyle', bubbleStyle.value);
});
openSidebarBtn?.addEventListener('click', () => sidebar.classList.remove('hidden'));
closeSidebarBtn?.addEventListener('click', () => sidebar.classList.add('hidden'));
emojiPickerBtn?.addEventListener('click', () => emojiPicker.classList.toggle('hidden'));
chatSearch.addEventListener('input', () => renderChatHistory(chatSearch.value.toLowerCase()));
messageInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
chatForm.dispatchEvent(new Event('submit'));
} else if (e.key === 'Escape') {
emojiPicker.classList.add('hidden');
}
});
// === Yeni Sohbet Başlat ===
newChatBtn.addEventListener('click', () => {
currentChatId = Date.now().toString();
chats.push({ id: currentChatId, messages: [] });
saveChats();
renderChatHistory();
messagesContainer.innerHTML = '';
lastSender = null;
showNotification('Yeni sohbet başlatıldı!', 'success');
});
// === Mesaj Gönderme ===
let isWaitingResponse = false;
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
if (isWaitingResponse) return; // Aynı anda birden fazla istek gönderilmesini engeller
const query = messageInput.value.trim();
if (!query) return showNotification('Lütfen bir mesaj yazın!', 'error');
isWaitingResponse = true;
addMessage(query, 'user');
showTypingIndicator();
try {
const res = await fetch('/api/ask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: query })
});
const data = await res.json();
removeTypingIndicator();
addMessage(data.answer || 'Bir hata oluştu.', 'bot');
saveMessage(data.answer || 'Bir hata oluştu.', 'bot');
} catch {
removeTypingIndicator();
addMessage('Cevap alınamadı.', 'bot');
saveMessage('Cevap alınamadı.', 'bot');
}
saveMessage(query, 'user');
messageInput.value = '';
localStorage.removeItem('messageDraft');
isWaitingResponse = false;
scrollToBottom();
});
// === Mesajları Kaydetme ===
function saveMessage(text, sender) {
if (!currentChatId) {
currentChatId = Date.now().toString();
chats.push({ id: currentChatId, messages: [] });
}
const chat = chats.find(c => c.id === currentChatId);
chat.messages.push({ id: Date.now().toString(), text, sender, timestamp: new Date().toLocaleTimeString(), isPinned: false, reactions: '' });
saveChats();
renderChatHistory();
}
function saveChats() {
localStorage.setItem('chats', JSON.stringify(chats));
}
// === Görsel İşlevler ===
function scrollToBottom() {
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function showTypingIndicator() {
const typingDiv = document.createElement('div');
typingDiv.id = 'typing-indicator';
typingDiv.className = 'typing';
typingDiv.innerHTML = '<span></span><span></span><span></span>';
messagesContainer.appendChild(typingDiv);
scrollToBottom();
}
function removeTypingIndicator() {
qs('#typing-indicator')?.remove();
}
function showNotification(text, type = 'success') {
const notif = document.createElement('div');
notif.className = `notification fixed bottom-4 left-4 p-3 rounded-lg text-white ${type === 'success' ? 'bg-green-500' : 'bg-red-500'}`;
notif.textContent = text;
document.body.appendChild(notif);
setTimeout(() => notif.remove(), 3000);
}
// === Mesaj Ekleme ===
function addMessage(text, sender, id = Date.now().toString(), isPinned = false) {
const isSameSender = sender === lastSender;
const div = document.createElement('div');
div.className = `message chat-group ${sender === 'user' ? 'user-message' : 'bot-message'}${isSameSender ? ' mt-1' : ''}${isPinned ? ' pinned-message' : ''}`;
div.dataset.id = id;
div.innerHTML = `
<img src="${sender === 'user' ? 'https://img.icons8.com/color/48/000000/user.png' : 'https://img.icons8.com/color/48/000000/bot.png'}" class="w-8 h-8 rounded-full ${isSameSender && !isPinned ? 'invisible' : ''}">
<div class="flex-1">
<div class="flex justify-between items-baseline">
<span class="text-sm font-semibold ${sender === 'user' ? 'text-white' : 'text-gray-700 dark:text-gray-300'}">${sender === 'user' ? currentUser : 'Bot'}</span>
<span class="text-xs text-gray-400">${new Date().toLocaleTimeString()}</span>
</div>
<div class="break-words">${text}</div>
</div>`;
messagesContainer.appendChild(div);
lastSender = sender;
scrollToBottom();
}
// === Sohbet Geçmişi ===
function renderChatHistory(query = '') {
chatHistoryContainer.innerHTML = '';
chats.filter(chat => !query || chat.messages.some(m => m.text.toLowerCase().includes(query)))
.forEach(chat => {
const last = chat.messages.at(-1);
const item = document.createElement('div');
item.className = `p-3 rounded-lg flex justify-between items-center cursor-pointer ${currentChatId === chat.id ? 'bg-indigo-100' : 'bg-gray-100'} hover:bg-indigo-50`;
item.innerHTML = `
<div class="flex-1" onclick="loadChat('${chat.id}')">
<div class="text-sm font-semibold text-gray-800">${chat.messages[0]?.text.substring(0, 25) || 'Yeni Sohbet'}...</div>
<div class="text-xs text-gray-500">${last?.timestamp || ''}</div>
</div>
<button onclick="deleteChat('${chat.id}')" class="text-red-500 hover:text-red-700"><i class="fas fa-trash"></i></button>
`;
chatHistoryContainer.appendChild(item);
});
}
// === Global Fonksiyonlar ===
window.loadChat = function(id) {
currentChatId = id;
const chat = chats.find(c => c.id === id);
messagesContainer.innerHTML = '';
lastSender = null;
chat.messages.forEach(msg => addMessage(msg.text, msg.sender, msg.id, msg.isPinned));
scrollToBottom();
sidebar.classList.add('hidden');
};
window.deleteChat = function(id) {
chats = chats.filter(c => c.id !== id);
saveChats();
renderChatHistory();
if (currentChatId === id) {
messagesContainer.innerHTML = '';
currentChatId = null;
lastSender = null;
}
showNotification('Sohbet silindi!', 'success');
};
// === Emoji ve Quick Reply ===
function addEmoji(emoji) {
messageInput.value += emoji;
messageInput.focus();
}
function quickReply(text) {
messageInput.value = text;
chatForm.dispatchEvent(new Event('submit'));
}
// === Başlat ===
initializeSettings();
renderChatHistory();
|