let threads = JSON.parse(localStorage.getItem('threads') || '[]'); function saveThreads() { localStorage.setItem('threads', JSON.stringify(threads)); } function createThread() { const title = document.getElementById('thread-title').value; const content = document.getElementById('thread-content').value; const imageInput = document.getElementById('thread-image'); const reader = new FileReader(); reader.onload = () => { const thread = { id: Date.now(), title, posts: [{ content, image: reader.result || '', date: new Date().toLocaleString() }] }; threads.unshift(thread); saveThreads(); renderThreads(); }; if (imageInput.files.length > 0) { reader.readAsDataURL(imageInput.files[0]); } else { reader.onload(); // No image } document.getElementById('thread-title').value = ''; document.getElementById('thread-content').value = ''; document.getElementById('thread-image').value = ''; } function replyToThread(threadId, content, imageFile) { const thread = threads.find(t => t.id === threadId); const reader = new FileReader(); reader.onload = () => { thread.posts.push({ content, image: reader.result || '', date: new Date().toLocaleString() }); saveThreads(); renderThreads(); }; if (imageFile) { reader.readAsDataURL(imageFile); } else { reader.onload(); } } function renderThreads() { const container = document.getElementById('threads'); container.innerHTML = ''; threads.forEach(thread => { const threadDiv = document.createElement('div'); threadDiv.className = 'thread'; threadDiv.innerHTML = `
${post.content}
${post.image ? `