anycoder-17067b18 / index.html
samirerty's picture
Upload folder using huggingface_hub
a0a7ec2 verified
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پیام رسان سریع | FastChat</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Google Fonts: Vazirmatn for Persian -->
<link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@100;300;400;500;700;900&display=swap" rel="stylesheet">
<!-- Confetti for effects -->
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.6.0/dist/confetti.browser.min.js"></script>
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['Vazirmatn', 'sans-serif'],
},
colors: {
brand: {
50: '#f0f9ff',
100: '#e0f2fe',
500: '#0ea5e9',
600: '#0284c7',
900: '#0c4a6e',
},
dark: {
800: '#1e293b',
900: '#0f172a',
}
},
animation: {
'fade-in': 'fadeIn 0.5s ease-out',
'slide-up': 'slideUp 0.5s ease-out',
'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
},
keyframes: {
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
slideUp: {
'0%': { transform: 'translateY(20px)', opacity: '0' },
'100%': { transform: 'translateY(0)', opacity: '1' },
}
}
}
}
}
</script>
<style>
/* Custom Scrollbar */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
}
::-webkit-scrollbar-thumb {
background: #cbd5e1;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #94a3b8;
}
/* Glassmorphism Utilities */
.glass {
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.3);
}
.glass-dark {
background: rgba(15, 23, 42, 0.8);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.05);
}
/* Message Bubble Animation */
.message-enter {
animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}
@keyframes popIn {
from { opacity: 0; transform: scale(0.8) translateY(10px); }
to { opacity: 1; transform: scale(1) translateY(0); }
}
/* Loader */
.loader {
border: 3px solid #f3f3f3;
border-radius: 50%;
border-top: 3px solid #3498db;
width: 20px;
height: 20px;
-webkit-animation: spin 1s linear infinite; /* Safari */
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body class="bg-slate-100 text-slate-800 font-sans h-screen overflow-hidden selection:bg-brand-200 selection:text-brand-900">
<!-- Toast Notification Container -->
<div id="toast-container" class="fixed top-5 left-1/2 transform -translate-x-1/2 z-50 flex flex-col gap-2 w-full max-w-sm px-4 pointer-events-none"></div>
<!-- ================= AUTHENTICATION VIEW ================= -->
<div id="auth-view" class="h-full w-full flex items-center justify-center relative overflow-hidden">
<!-- Decorative Background Blobs -->
<div class="absolute top-0 left-0 w-96 h-96 bg-brand-300 rounded-full mix-blend-multiply filter blur-3xl opacity-30 animate-pulse-slow"></div>
<div class="absolute bottom-0 right-0 w-96 h-96 bg-purple-300 rounded-full mix-blend-multiply filter blur-3xl opacity-30 animate-pulse-slow" style="animation-delay: 1s;"></div>
<div class="glass p-8 rounded-2xl shadow-2xl w-full max-w-md z-10 animate-slide-up mx-4">
<div class="text-center mb-8">
<div class="w-16 h-16 bg-brand-600 rounded-2xl mx-auto flex items-center justify-center text-white text-2xl shadow-lg mb-4 rotate-3 hover:rotate-6 transition-transform duration-300">
<i class="fa-solid fa-comments"></i>
</div>
<h1 class="text-3xl font-bold text-slate-800">پیام رسان سریع</h1>
<p class="text-slate-500 mt-2 text-sm">بدون نیاز به سرور، فقط با مرورگر</p>
</div>
<!-- Tabs -->
<div class="flex bg-slate-100 p-1 rounded-xl mb-6">
<button onclick="switchAuthTab('login')" id="tab-login" class="flex-1 py-2 rounded-lg text-sm font-medium transition-all duration-300 bg-white text-brand-600 shadow-sm">ورود</button>
<button onclick="switchAuthTab('register')" id="tab-register" class="flex-1 py-2 rounded-lg text-sm font-medium text-slate-500 hover:text-slate-700 transition-all duration-300">ثبت نام</button>
</div>
<!-- Login Form -->
<form id="login-form" class="space-y-4" onsubmit="handleLogin(event)">
<div>
<label class="block text-xs font-bold text-slate-500 mb-1">شماره موبایل</label>
<div class="relative">
<i class="fa-solid fa-phone absolute right-3 top-3 text-slate-400"></i>
<input type="tel" id="login-phone" placeholder="09XXXXXXXXX" class="w-full bg-slate-50 border border-slate-200 rounded-xl py-2.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all" required>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-500 mb-1">رمز عبور</label>
<div class="relative">
<i class="fa-solid fa-lock absolute right-3 top-3 text-slate-400"></i>
<input type="password" id="login-password" placeholder="••••••••" class="w-full bg-slate-50 border border-slate-200 rounded-xl py-2.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all" required>
</div>
</div>
<button type="submit" class="w-full bg-brand-600 hover:bg-brand-700 text-white font-bold py-3 rounded-xl shadow-lg shadow-brand-500/30 transition-all transform hover:scale-[1.02] active:scale-95">
ورود به حساب
</button>
</form>
<!-- Register Form (Hidden by default) -->
<form id="register-form" class="space-y-4 hidden" onsubmit="handleRegister(event)">
<div>
<label class="block text-xs font-bold text-slate-500 mb-1">نام کامل</label>
<div class="relative">
<i class="fa-solid fa-user absolute right-3 top-3 text-slate-400"></i>
<input type="text" id="reg-name" placeholder="مثلا: علی احمدی" class="w-full bg-slate-50 border border-slate-200 rounded-xl py-2.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all" required>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-500 mb-1">شماره موبایل</label>
<div class="relative">
<i class="fa-solid fa-phone absolute right-3 top-3 text-slate-400"></i>
<input type="tel" id="reg-phone" placeholder="09XXXXXXXXX" class="w-full bg-slate-50 border border-slate-200 rounded-xl py-2.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all" required>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-500 mb-1">رمز عبور</label>
<div class="relative">
<i class="fa-solid fa-lock absolute right-3 top-3 text-slate-400"></i>
<input type="password" id="reg-password" placeholder="حداقل 4 کاراکتر" class="w-full bg-slate-50 border border-slate-200 rounded-xl py-2.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all" required>
</div>
</div>
<button type="submit" class="w-full bg-slate-800 hover:bg-slate-900 text-white font-bold py-3 rounded-xl shadow-lg shadow-slate-800/30 transition-all transform hover:scale-[1.02] active:scale-95">
ایجاد حساب کاربری
</button>
</form>
<div class="mt-6 text-center">
<p class="text-xs text-slate-400">
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="text-brand-600 hover:underline">anycoder</a>
</p>
</div>
</div>
</div>
<!-- ================= DASHBOARD VIEW ================= -->
<div id="dashboard-view" class="hidden h-full w-full flex flex-col md:flex-row bg-slate-50">
<!-- Sidebar -->
<aside class="w-full md:w-80 bg-white border-l border-slate-200 flex flex-col z-20 shadow-xl md:shadow-none">
<!-- User Profile Header -->
<div class="p-6 border-b border-slate-100 bg-gradient-to-br from-slate-50 to-white">
<div class="flex items-center gap-4">
<div class="relative">
<div class="w-14 h-14 rounded-full bg-gradient-to-tr from-brand-400 to-purple-500 flex items-center justify-center text-white text-xl font-bold shadow-md">
<span id="user-avatar-initial">U</span>
</div>
<div class="absolute bottom-0 right-0 w-4 h-4 bg-green-500 border-2 border-white rounded-full"></div>
</div>
<div>
<h2 id="user-name-display" class="font-bold text-slate-800 text-lg">کاربر</h2>
<p id="user-phone-display" class="text-xs text-slate-500 font-mono">09--------</p>
</div>
<button onclick="logout()" class="mr-auto text-slate-400 hover:text-red-500 transition-colors" title="خروج">
<i class="fa-solid fa-power-off text-lg"></i>
</button>
</div>
</div>
<!-- Create Room Section -->
<div class="p-6 border-b border-slate-100">
<div class="flex justify-between items-center mb-3">
<h3 class="font-bold text-slate-700">اتاق های من</h3>
<span id="room-count-badge" class="bg-slate-100 text-slate-600 text-xs font-bold px-2 py-1 rounded-md">0/3</span>
</div>
<button onclick="openCreateRoomModal()" id="btn-create-room" class="w-full py-2.5 border-2 border-dashed border-brand-200 text-brand-600 rounded-xl hover:bg-brand-50 hover:border-brand-400 transition-all flex items-center justify-center gap-2 font-medium text-sm">
<i class="fa-solid fa-plus"></i> ساخت اتاق جدید
</button>
<p id="room-limit-msg" class="text-xs text-red-500 mt-2 hidden text-center">شما به حداکثر تعداد اتاق رسیدید.</p>
</div>
<!-- Room List -->
<div class="flex-1 overflow-y-auto p-3 space-y-2" id="room-list-container">
<!-- Rooms injected via JS -->
<div class="text-center mt-10 text-slate-400 text-sm">
<i class="fa-regular fa-folder-open text-3xl mb-2 block"></i>
هنوز اتاقی نساختید
</div>
</div>
</aside>
<!-- Main Content Area -->
<main class="flex-1 flex flex-col h-full relative bg-slate-50/50">
<!-- Top Bar -->
<header class="h-16 bg-white border-b border-slate-200 flex items-center justify-between px-6 shadow-sm z-10">
<div class="flex items-center gap-3">
<div class="md:hidden text-slate-500">
<i class="fa-solid fa-bars text-xl"></i>
</div>
<h1 class="font-bold text-xl text-slate-800 hidden md:block">داشبورد</h1>
</div>
<div class="flex items-center gap-4">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="text-xs bg-slate-100 hover:bg-slate-200 text-slate-600 px-3 py-1.5 rounded-full transition-colors">
Built with anycoder
</a>
</div>
</header>
<!-- Dashboard Content / Welcome -->
<div id="welcome-screen" class="flex-1 flex flex-col items-center justify-center p-8 text-center">
<div class="w-32 h-32 bg-slate-100 rounded-full flex items-center justify-center mb-6 relative">
<i class="fa-solid fa-rocket text-4xl text-slate-300"></i>
<div class="absolute inset-0 border-2 border-slate-200 border-dashed rounded-full animate-spin-slow" style="animation-duration: 10s;"></div>
</div>
<h2 class="text-2xl font-bold text-slate-800 mb-2">به پیام رسان خوش آمدید</h2>
<p class="text-slate-500 max-w-md mb-8">یک اتاق جدید بسازید یا از لیست سمت راست یکی را انتخاب کنید تا گفتگو را شروع کنید.</p>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 w-full max-w-2xl text-right">
<div class="bg-white p-4 rounded-xl shadow-sm border border-slate-100">
<div class="w-10 h-10 rounded-lg bg-blue-100 text-blue-600 flex items-center justify-center mb-3">
<i class="fa-solid fa-bolt"></i>
</div>
<h4 class="font-bold text-slate-700 mb-1">سریع و آنی</h4>
<p class="text-xs text-slate-500">بدون نیاز به سرور، داده ها در مرورگر شما ذخیره می شوند.</p>
</div>
<div class="bg-white p-4 rounded-xl shadow-sm border border-slate-100">
<div class="w-10 h-10 rounded-lg bg-purple-100 text-purple-600 flex items-center justify-center mb-3">
<i class="fa-solid fa-link"></i>
</div>
<h4 class="font-bold text-slate-700 mb-1">اشتراک گذاری</h4>
<p class="text-xs text-slate-500">لینک اتاق را کپی کنید و برای دوستانتان بفرستید.</p>
</div>
<div class="bg-white p-4 rounded-xl shadow-sm border border-slate-100">
<div class="w-10 h-10 rounded-lg bg-green-100 text-green-600 flex items-center justify-center mb-3">
<i class="fa-solid fa-shield-halved"></i>
</div>
<h4 class="font-bold text-slate-700 mb-1">امنیت محلی</h4>
<p class="text-xs text-slate-500">هیچ داده ای به سرور ارسال نمی شود. کاملا خصوصی.</p>
</div>
</div>
</div>
<!-- Chat Interface (Hidden initially) -->
<div id="chat-interface" class="hidden flex-col h-full absolute inset-0 bg-white z-20 animate-fade-in">
<!-- Chat Header -->
<div class="h-16 border-b border-slate-100 flex items-center justify-between px-4 bg-white">
<div class="flex items-center gap-3">
<button onclick="closeChat()" class="md:hidden w-8 h-8 flex items-center justify-center rounded-full hover:bg-slate-100 text-slate-600">
<i class="fa-solid fa-arrow-right"></i>
</button>
<div class="w-10 h-10 rounded-full bg-gradient-to-br from-indigo-400 to-brand-500 flex items-center justify-center text-white font-bold shadow-sm">
<i class="fa-solid fa-hashtag"></i>
</div>
<div>
<h3 id="chat-room-name" class="font-bold text-slate-800">نام اتاق</h3>
<div class="flex items-center gap-2 text-xs text-slate-500">
<span class="w-2 h-2 bg-green-500 rounded-full"></span>
<span id="chat-user-count">2 عضو آنلاین</span>
</div>
</div>
</div>
<div class="flex items-center gap-2">
<button onclick="copyRoomLink()" class="p-2 text-slate-400 hover:text-brand-600 hover:bg-brand-50 rounded-lg transition-colors tooltip-btn" title="کپی لینک">
<i class="fa-solid fa-share-nodes"></i>
</button>
<button onclick="closeChat()" class="hidden md:block p-2 text-slate-400 hover:text-red-500 hover:bg-red-50 rounded-lg transition-colors" title="بستن">
<i class="fa-solid fa-xmark"></i>
</button>
</div>
</div>
<!-- Messages Area -->
<div id="messages-container" class="flex-1 overflow-y-auto p-4 space-y-4 bg-slate-50">
<!-- Messages injected via JS -->
</div>
<!-- Input Area -->
<div class="p-4 bg-white border-t border-slate-100">
<form onsubmit="sendMessage(event)" class="flex gap-2">
<input type="text" id="message-input" placeholder="پیام خود را بنویسید..." class="flex-1 bg-slate-100 border-0 rounded-xl px-4 py-3 focus:ring-2 focus:ring-brand-500 focus:bg-white transition-all" autocomplete="off">
<button type="submit" class="bg-brand-600 hover:bg-brand-700 text-white w-12 h-12 rounded-xl flex items-center justify-center shadow-lg shadow-brand-500/30 transition-transform active:scale-95">
<i class="fa-solid fa-paper-plane"></i>
</button>
</form>
</div>
</div>
</main>
</div>
<!-- Create Room Modal -->
<div id="create-room-modal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-slate-900/40 backdrop-blur-sm transition-opacity" onclick="closeCreateRoomModal()"></div>
<div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-full max-w-md p-4">
<div class="bg-white rounded-2xl shadow-2xl p-6 animate-slide-up">
<div class="flex justify-between items-center mb-6">
<h3 class="text-xl font-bold text-slate-800">ساخت اتاق جدید</h3>
<button onclick="closeCreateRoomModal()" class="text-slate-400 hover:text-slate-600">
<i class="fa-solid fa-xmark text-xl"></i>
</button>
</div>
<form onsubmit="handleCreateRoom(event)">
<div class="mb-4">
<label class="block text-sm font-bold text-slate-700 mb-2">نام اتاق</label>
<input type="text" id="new-room-name" class="w-full border border-slate-200 rounded-xl px-4 py-3 focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="مثلا: گروه دوستان" required>
</div>
<div class="flex gap-3">
<button type="button" onclick="closeCreateRoomModal()" class="flex-1 py-3 rounded-xl border border-slate-200 text-slate-600 font-bold hover:bg-slate-50">انصراف</button>
<button type="submit" class="flex-1 py-3 rounded-xl bg-brand-600 text-white font-bold hover:bg-brand-700 shadow-lg shadow-brand-500/30">ساختن</button>
</div>
</form>
</div>
</div>
</div>
<script>
// --- DATA & STATE MANAGEMENT ---
// Simulating a database using LocalStorage
const DB_KEYS = {
USERS: 'chat_app_users',
ROOMS: 'chat_app_rooms',
MESSAGES: 'chat_app_messages',
SESSION: 'chat_app_session'
};
let currentUser = null;
let currentRoomId = null;
// --- INITIALIZATION ---
document.addEventListener('DOMContentLoaded', () => {
checkSession();
// Check URL for room invite
const urlParams = new URLSearchParams(window.location.search);
const inviteRoomId = urlParams.get('room');
if (inviteRoomId) {
// If user is logged in, try to join. If not, store intent.
// For this simple version, we just show a toast if they are logged in.
if(currentUser) {
joinRoomById(inviteRoomId);
} else {
showToast('لطفا ابتدا وارد شوید تا بتوانید به اتاق دعوت شده بپیوندید.', 'info');
}
}
});
function checkSession() {
const session = localStorage.getItem(DB_KEYS.SESSION);
if (session) {
currentUser = JSON.parse(session);
showDashboard();
} else {
showAuth();
}
}
// --- AUTHENTICATION LOGIC ---
function switchAuthTab(tab) {
const loginForm = document.getElementById('login-form');
const registerForm = document.getElementById('register-form');
const loginTab = document.getElementById('tab-login');
const registerTab = document.getElementById('tab-register');
if (tab === 'login') {
loginForm.classList.remove('hidden');
registerForm.classList.add('hidden');
loginTab.classList.add('bg-white', 'text-brand-600', 'shadow-sm');
loginTab.classList.remove('text-slate-500');
registerTab.classList.remove('bg-white', 'text-brand-600', 'shadow-sm');
registerTab.classList.add('text-slate-500');
} else {
loginForm.classList.add('hidden');
registerForm.classList.remove('hidden');
registerTab.classList.add('bg-white', 'text-brand-600', 'shadow-sm');
registerTab.classList.remove('text-slate-500');
loginTab.classList.remove('bg-white', 'text-brand-600', 'shadow-sm');
loginTab.classList.add('text-slate-500');
}
}
function handleRegister(e) {
e.preventDefault();
const name = document.getElementById('reg-name').value;
const phone = document.getElementById('reg-phone').value;
const password = document.getElementById('reg-password').value;
let users = JSON.parse(localStorage.getItem(DB_KEYS.USERS) || '[]');
if (users.find(u => u.phone === phone)) {
showToast('این شماره قبلا ثبت نام شده است.', 'error');
return;
}
const newUser = {
id: Date.now().toString(),
name,
phone,
password, // In a real app, hash this!
createdAt: new Date().toISOString()
};
users.push(newUser);
localStorage.setItem(DB_KEYS.USERS, JSON.stringify(users));
// Auto login
currentUser = { id: newUser.id, name: newUser.name, phone: newUser.phone };
localStorage.setItem(DB_KEYS.SESSION, JSON.stringify(currentUser));
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 }
});
showToast('ثبت نام با موفقیت انجام شد!', 'success');
showDashboard();
}
function handleLogin(e) {
e.preventDefault();
const phone = document.getElementById('login-phone').value;
const password = document.getElementById('login-password').value;
const users = JSON.parse(localStorage.getItem(DB_KEYS.USERS) || '[]');
const user = users.find(u => u.phone === phone && u.password === password);
if (user) {
currentUser = { id: user.id, name: user.name, phone: user.phone };
localStorage.setItem(DB_KEYS.SESSION, JSON.stringify(currentUser));
showToast(`خوش آمدید ${user.name}`, 'success');
showDashboard();
} else {
showToast('شماره یا رمز عبور اشتباه است.', 'error');
}
}
function logout() {
localStorage.removeItem(DB_KEYS.SESSION);
currentUser = null;
showAuth();
showToast('از حساب کاربری خارج شدید.', 'info');
}
// --- VIEW MANAGEMENT ---
function showAuth() {
document.getElementById('auth-view').classList.remove('hidden');
document.getElementById('dashboard-view').classList.add('hidden');
}
function showDashboard() {
document.getElementById('auth-view').classList.add('hidden');
document.getElementById('dashboard-view').classList.remove('hidden');
// Update Profile Info
document.getElementById('user-name-display').innerText = currentUser.name;
document.getElementById('user-phone-display').innerText = currentUser.phone;
document.getElementById('user-avatar-initial').innerText = currentUser.name.charAt(0);
renderRoomList();
}
// --- ROOM MANAGEMENT ---
function renderRoomList() {
const allRooms = JSON.parse(localStorage.getItem(DB_KEYS.ROOMS) || '[]');
// Filter rooms created by current user OR rooms where user is a member
// For this demo, we show rooms user created + rooms they joined via link (simplified logic)
const myRooms = allRooms.filter(r => r.creatorId === currentUser.id || (r.members && r.members.includes(currentUser.id)));
const container = document.getElementById('room-list-container');
const countBadge = document.getElementById('room-count-badge');
const createBtn = document.getElementById('btn-create-room');
const limitMsg = document.getElementById('room-limit-msg');
// Update Badge
const createdCount = allRooms.filter(r => r.creatorId === currentUser.id).length;
countBadge.innerText = `${createdCount}/3`;
// Handle Limit
if (createdCount >= 3) {
createBtn.disabled = true;
createBtn.classList.add('opacity-50', 'cursor-not-allowed');
limitMsg.classList.remove('hidden');
} else {
createBtn.disabled = false;
createBtn.classList.remove('opacity-50', 'cursor-not-allowed');
limitMsg.classList.add('hidden');
}
// Render List
if (myRooms.length === 0) {
container.innerHTML = `
<div class="text-center mt-10 text-slate-400 text-sm flex flex-col items-center">
<div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mb-3">
<i class="fa-regular fa-folder-open text-2xl"></i>
</div>
<p>هنوز اتاقی نساختید</p>
</div>`;
return;
}
container.innerHTML = myRooms.map(room => `
<div onclick="openRoom('${room.id}')" class="group p-3 rounded-xl hover:bg-slate-50 cursor-pointer border border-transparent hover:border-slate-200 transition-all flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-lg bg-gradient-to-br from-slate-200 to-slate-300 text-slate-600 flex items-center justify-center font-bold group-hover:from-brand-100 group-hover:to-brand-200 group-hover:text-brand-600 transition-colors">
${room.name.charAt(0)}
</div>
<div>
<h4 class="font-bold text-slate-700 text-sm">${room.name}</h4>
<p class="text-xs text-slate-400">ایجاد شده در ${new Date(room.createdAt).toLocaleDateString('fa-IR')}</p>
</div>
</div>
<i class="fa-solid fa-chevron-left text-slate-300 text-xs group-hover:text-brand-500"></i>
</div>
`).join('');
}
function openCreateRoomModal() {
document.getElementById('create-room-modal').classList.remove('hidden');
}
function closeCreateRoomModal() {
document.getElementById('create-room-modal').classList.add('hidden');
}
function handleCreateRoom(e) {
e.preventDefault();
const name = document.getElementById('new-room-name').value;
const rooms = JSON.parse(localStorage.getItem(DB_KEYS.ROOMS) || '[]');
const myRoomCount = rooms.filter(r => r.creatorId === currentUser.id).length;
if (myRoomCount >= 3) {
showToast('شما نمی توانید بیش از 3 اتاق بسازید.', 'error');
return;
}
const newRoom = {
id: 'room_' + Date.now(),
name: name,
creatorId: currentUser.id,
members: [currentUser.id], // Creator is first member
createdAt: new Date().toISOString()
};
rooms.push(newRoom);
localStorage.setItem(DB_KEYS.ROOMS, JSON.stringify(rooms));
closeCreateRoomModal();
document.getElementById('new-room-name').value = '';
renderRoomList();
showToast('اتاق با موفقیت ساخته شد!', 'success');
// Auto open
openRoom(newRoom.id);
}
function joinRoomById(roomId) {
const rooms = JSON.parse(localStorage.getItem(DB_KEYS.ROOMS) || '[]');
const roomIndex = rooms.findIndex(r => r.id === roomId);
if (roomIndex === -1) {
showToast('اتاق مورد نظر یافت نشد.', 'error');
return;
}
// Add user to members if not already
if (!rooms[roomIndex].members) rooms[roomIndex].members = [];
if (!rooms[roomIndex].members.includes(currentUser.id)) {
rooms[roomIndex].members.push(currentUser.id);
localStorage.setItem(DB_KEYS.ROOMS, JSON.stringify(rooms));
showToast('شما به اتاق پیوستید!', 'success');
renderRoomList();
}
openRoom(roomId);
}
// --- CHAT LOGIC ---
function openRoom(roomId) {
currentRoomId = roomId;
const rooms = JSON.parse(localStorage.getItem(DB_KEYS.ROOMS) || '[]');
const room = rooms.find(r => r.id === roomId);
if (!room) return;
// UI Updates
document.getElementById('welcome-screen').classList.add('hidden');
document.getElementById('chat-interface').classList.remove('hidden');
document.getElementById('chat-interface').classList.add('flex');
document.getElementById('chat-room-name').innerText = room.name;
document.getElementById('chat-user-count').innerText = `${room.members ? room.members.length : 1} عضو`;
loadMessages();
// Focus input
setTimeout(() => document.getElementById('message-input').focus(), 100);
}
function closeChat() {
document.getElementById('chat-interface').classList.add('hidden');
document.getElementById('chat-interface').classList.remove('flex');
document.getElementById('welcome-screen').classList.remove('hidden');
currentRoomId = null;
}
function loadMessages() {
const allMessages = JSON.parse(localStorage.getItem(DB_KEYS.MESSAGES) || '[]');
const roomMessages = allMessages.filter(m => m.roomId === currentRoomId);
const container = document.getElementById('messages-container');
container.innerHTML = '';
if (roomMessages.length === 0) {
container.innerHTML = `
<div class="text-center text-slate-400 mt-10">
<p>هنوز پیامی در این اتاق نیست.</p>
<p class="text-sm">اولین نفری باشید که پیام می دهد!</p>
</div>`;
} else {
roomMessages.forEach(msg => renderMessageBubble(msg));
}
scrollToBottom();
}
function renderMessageBubble(msg) {
const container = document.getElementById('messages-container');
const isMe = msg.userId === currentUser.id;
const div = document.createElement('div');
div.className = `flex w-full ${isMe ? 'justify-start' : 'justify-end'} message-enter`;
const bubbleColor = isMe ? 'bg-brand-600 text-white rounded-tr-none' : 'bg-white text-slate-800 border border-slate-200 rounded-tl-none';
const align = isMe ? 'items-start' : 'items-end';
// Get user name
const users = JSON.parse(localStorage.getItem(DB_KEYS.USERS) || '[]');
const sender = users.find(u => u.id === msg.userId);
const senderName = sender ? sender.name : 'Unknown';
div.innerHTML = `
<div class="max-w-[80%] flex flex-col ${align}">
${!isMe ? `<span class="text-xs text-slate-500 mb-1 mr-1">${senderName}</span>` : ''}
<div class="${bubbleColor} px-4 py-2 rounded-2xl shadow-sm text-sm leading-relaxed">
${msg.text}
</div>
<span class="text-[10px] text-slate-400 mt-1 mx-1">${new Date(msg.timestamp).toLocaleTimeString('fa-IR', {hour: '2-digit', minute:'2-digit'})}</span>
</div>
`;
container.appendChild(div);
}
function sendMessage(e) {
e.preventDefault();
const input = document.getElementById('message-input');
const text = input.value.trim();
if (!text || !currentRoomId) return;
const newMessage = {
id: Date.now().toString(),
roomId: currentRoomId,
userId: currentUser.id,
text: text,
timestamp: new Date().toISOString()
};
const allMessages = JSON.parse(localStorage.getItem(DB_KEYS.MESSAGES) || '[]');
allMessages.push(newMessage);
localStorage.setItem(DB_KEYS.MESSAGES, JSON.stringify(allMessages));
// Clear input and refresh
input.value = '';
// Remove "empty" message if exists
const container = document.getElementById('messages-container');
if (container.children.length > 0 && container.children[0].classList.contains('text-center')) {
container.innerHTML = '';
}
renderMessageBubble(newMessage);
scrollToBottom();
}
function scrollToBottom() {
const container = document.getElementById('messages-container');
container.scrollTop = container.scrollHeight;
}
function copyRoomLink() {
if(!currentRoomId) return;
// Create a shareable link (in a real app, this would be the actual URL)
// Since we are in a single file, we simulate the link structure
const dummyLink = `${window.location.origin}${window.location.pathname}?room=${currentRoomId}`;
navigator.clipboard.writeText(dummyLink).then(() => {
showToast('لینک اتاق کپی شد!', 'success');
}).catch(() => {
showToast('خطا در کپی لینک', 'error');
});
}
// --- UTILS ---
function showToast(message, type = 'info') {
const container = document.getElementById('toast-container');
const toast = document.createElement('div');
let colors = 'bg-slate-800 text-white';
let icon = 'fa-info-circle';
if (type === 'success') { colors = 'bg-green-600 text-white'; icon = 'fa-check-circle'; }
if (type === 'error') { colors = 'bg-red-500 text-white'; icon = 'fa-exclamation-circle'; }
toast.className = `${colors} px-4 py-3 rounded-xl shadow-lg flex items-center gap-3 transform transition-all duration-500 translate-y-10 opacity-0 pointer-events-auto`;
toast.innerHTML = `
<i class="fa-solid ${icon}"></i>
<span class="text-sm font-medium">${message}</span>
`;
container.appendChild(toast);
// Animate in
requestAnimationFrame(() => {
toast.classList.remove('translate-y-10', 'opacity-0');
});
// Remove after 3s
setTimeout(() => {
toast.classList.add('translate-y-10', 'opacity-0');
setTimeout(() => toast.remove(), 500);
}, 3000);
}
</script>
</body>
</html>