anycoder-0ea08227 / index.html
samirerty's picture
Upload folder using huggingface_hub
a3b70ce verified
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=0.8, maximum-scale=0.8, user-scalable=no">
<title>پیام رسان شیشه‌ای | GlassChat</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: {
glass: {
100: 'rgba(255, 255, 255, 0.1)',
200: 'rgba(255, 255, 255, 0.2)',
300: 'rgba(255, 255, 255, 0.3)',
400: 'rgba(255, 255, 255, 0.4)',
500: 'rgba(255, 255, 255, 0.5)',
},
ios: {
blue: '#007AFF',
indigo: '#5856D6',
purple: '#AF52DE',
pink: '#FF2D55',
red: '#FF3B30',
orange: '#FF9500',
yellow: '#FFCC00',
green: '#34C759',
teal: '#5AC8FA',
gray: '#8E8E93',
bg: '#F2F2F7'
}
},
animation: {
'fade-in': 'fadeIn 0.5s ease-out',
'slide-up': 'slideUp 0.5s cubic-bezier(0.16, 1, 0.3, 1)',
'float': 'float 6s ease-in-out infinite',
'pulse-slow': 'pulse 4s cubic-bezier(0.4, 0, 0.6, 1) infinite',
'blob': 'blob 7s infinite',
},
keyframes: {
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
slideUp: {
'0%': { transform: 'translateY(30px)', opacity: '0' },
'100%': { transform: 'translateY(0)', opacity: '1' },
},
float: {
'0%, 100%': { transform: 'translateY(0)' },
'50%': { transform: 'translateY(-20px)' },
},
blob: {
'0%': { transform: 'translate(0px, 0px) scale(1)' },
'33%': { transform: 'translate(30px, -50px) scale(1.1)' },
'66%': { transform: 'translate(-20px, 20px) scale(0.9)' },
'100%': { transform: 'translate(0px, 0px) scale(1)' },
}
}
}
}
}
</script>
<style>
/* iOS Glassmorphism Base Styles */
body {
font-family: 'Vazirmatn', -apple-system, BlinkMacSystemFont, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
background-attachment: fixed;
overflow: hidden;
}
/* Animated Background Blobs */
.blob {
position: absolute;
filter: blur(80px);
z-index: 0;
opacity: 0.6;
animation: blob 7s infinite;
}
.blob-1 { top: 0; left: 0; width: 300px; height: 300px; background: #ff006e; animation-delay: 0s; }
.blob-2 { top: 0; right: 0; width: 400px; height: 400px; background: #8338ec; animation-delay: 2s; }
.blob-3 { bottom: 0; left: 20%; width: 350px; height: 350px; background: #3a86ff; animation-delay: 4s; }
.blob-4 { bottom: 10%; right: 10%; width: 250px; height: 250px; background: #fb5607; animation-delay: 6s; }
/* iOS Glass Utility Classes */
.ios-glass {
background: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid rgba(255, 255, 255, 0.3);
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
}
.ios-glass-strong {
background: rgba(255, 255, 255, 0.25);
backdrop-filter: blur(30px);
-webkit-backdrop-filter: blur(30px);
border: 1px solid rgba(255, 255, 255, 0.4);
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.2);
}
.ios-glass-dark {
background: rgba(0, 0, 0, 0.3);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.ios-button {
background: rgba(255, 255, 255, 0.3);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.4);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.ios-button:hover {
background: rgba(255, 255, 255, 0.5);
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.ios-button:active {
transform: translateY(0);
}
.ios-input {
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
backdrop-filter: blur(10px);
transition: all 0.3s;
}
.ios-input:focus {
background: rgba(255, 255, 255, 0.35);
border-color: rgba(255, 255, 255, 0.6);
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.1);
}
/* Custom Scrollbar - iOS Style */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.3);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.5);
}
/* Message Bubbles - Glass Style */
.message-bubble-me {
background: linear-gradient(135deg, rgba(0, 122, 255, 0.8), rgba(88, 86, 214, 0.8));
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.3);
box-shadow: 0 4px 15px rgba(0, 122, 255, 0.3);
}
.message-bubble-other {
background: rgba(255, 255, 255, 0.25);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.4);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
}
/* Animations */
.message-enter {
animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes popIn {
from { opacity: 0; transform: scale(0.8) translateY(10px); }
to { opacity: 1; transform: scale(1) translateY(0); }
}
/* Glass Card Hover Effect */
.glass-card {
transition: all 0.3s ease;
}
.glass-card:hover {
transform: translateY(-5px);
background: rgba(255, 255, 255, 0.25);
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
/* iOS Toggle Switch */
.ios-toggle {
appearance: none;
width: 50px;
height: 30px;
background: rgba(255,255,255,0.3);
border-radius: 30px;
position: relative;
cursor: pointer;
border: 1px solid rgba(255,255,255,0.4);
}
.ios-toggle::after {
content: '';
position: absolute;
width: 26px;
height: 26px;
background: white;
border-radius: 50%;
top: 1px;
right: 1px;
transition: transform 0.3s;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.ios-toggle:checked::after {
transform: translateX(-20px);
}
.ios-toggle:checked {
background: #34C759;
}
/* Responsive Adjustments */
@media (max-width: 768px) {
.ios-glass, .ios-glass-strong {
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
}
/* Loading Spinner */
.ios-spinner {
width: 20px;
height: 20px;
border: 2px solid rgba(255,255,255,0.3);
border-top-color: white;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Tab Animation */
.tab-active {
background: rgba(255, 255, 255, 0.4);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>
</head>
<body class="text-white h-screen overflow-hidden selection:bg-white/30 selection:text-white">
<!-- Animated Background -->
<div class="blob blob-1"></div>
<div class="blob blob-2"></div>
<div class="blob blob-3"></div>
<div class="blob blob-4"></div>
<!-- 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-3 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 z-10 p-4">
<div class="ios-glass-strong p-8 rounded-3xl shadow-2xl w-full max-w-md animate-slide-up mx-4 relative overflow-hidden">
<!-- Decorative shine effect -->
<div class="absolute top-0 left-0 right-0 h-1/2 bg-gradient-to-b from-white/20 to-transparent pointer-events-none"></div>
<div class="text-center mb-8 relative">
<div class="w-20 h-20 ios-glass rounded-3xl mx-auto flex items-center justify-center text-white text-3xl shadow-lg mb-4 rotate-3 hover:rotate-6 transition-transform duration-500 border border-white/40">
<i class="fa-solid fa-comments"></i>
</div>
<h1 class="text-3xl font-bold text-white drop-shadow-lg">پیام رسان شیشه‌ای</h1>
<p class="text-white/80 mt-2 text-sm font-light">بدون نیاز به سرور، فقط با مرورگر</p>
</div>
<!-- Tabs -->
<div class="flex bg-black/10 p-1.5 rounded-2xl mb-6 backdrop-blur-sm border border-white/10">
<button onclick="switchAuthTab('login')" id="tab-login" class="flex-1 py-3 rounded-xl text-sm font-bold transition-all duration-300 tab-active text-white">ورود</button>
<button onclick="switchAuthTab('register')" id="tab-register" class="flex-1 py-3 rounded-xl text-sm font-medium text-white/70 hover:text-white transition-all duration-300">ثبت نام</button>
</div>
<!-- Login Form -->
<form id="login-form" class="space-y-4" onsubmit="handleLogin(event)">
<div class="group">
<label class="block text-xs font-bold text-white/80 mb-2 mr-1">شماره موبایل</label>
<div class="relative">
<i class="fa-solid fa-phone absolute right-4 top-3.5 text-white/60"></i>
<input type="tel" id="login-phone" placeholder="09XXXXXXXXX" class="w-full ios-input rounded-2xl py-3.5 pr-12 pl-4 text-white placeholder-white/40 focus:outline-none" required>
</div>
</div>
<div class="group">
<label class="block text-xs font-bold text-white/80 mb-2 mr-1">رمز عبور</label>
<div class="relative">
<i class="fa-solid fa-lock absolute right-4 top-3.5 text-white/60"></i>
<input type="password" id="login-password" placeholder="••••••••" class="w-full ios-input rounded-2xl py-3.5 pr-12 pl-4 text-white placeholder-white/40 focus:outline-none" required>
</div>
</div>
<button type="submit" class="w-full bg-white/20 hover:bg-white/30 text-white font-bold py-4 rounded-2xl shadow-lg border border-white/40 transition-all transform hover:scale-[1.02] active:scale-95 backdrop-blur-sm mt-6">
ورود به حساب
</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-white/80 mb-2 mr-1">نام کامل</label>
<div class="relative">
<i class="fa-solid fa-user absolute right-4 top-3.5 text-white/60"></i>
<input type="text" id="reg-name" placeholder="مثلا: علی احمدی" class="w-full ios-input rounded-2xl py-3.5 pr-12 pl-4 text-white placeholder-white/40 focus:outline-none" required>
</div>
</div>
<div>
<label class="block text-xs font-bold text-white/80 mb-2 mr-1">شماره موبایل</label>
<div class="relative">
<i class="fa-solid fa-phone absolute right-4 top-3.5 text-white/60"></i>
<input type="tel" id="reg-phone" placeholder="09XXXXXXXXX" class="w-full ios-input rounded-2xl py-3.5 pr-12 pl-4 text-white placeholder-white/40 focus:outline-none" required>
</div>
</div>
<div>
<label class="block text-xs font-bold text-white/80 mb-2 mr-1">رمز عبور</label>
<div class="relative">
<i class="fa-solid fa-lock absolute right-4 top-3.5 text-white/60"></i>
<input type="password" id="reg-password" placeholder="حداقل 4 کاراکتر" class="w-full ios-input rounded-2xl py-3.5 pr-12 pl-4 text-white placeholder-white/40 focus:outline-none" required>
</div>
</div>
<button type="submit" class="w-full bg-white/20 hover:bg-white/30 text-white font-bold py-4 rounded-2xl shadow-lg border border-white/40 transition-all transform hover:scale-[1.02] active:scale-95 backdrop-blur-sm mt-6">
ایجاد حساب کاربری
</button>
</form>
<div class="mt-8 text-center">
<p class="text-xs text-white/60">
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank"
class="text-white hover:text-white/80 underline decoration-white/30">anycoder</a>
</p>
</div>
</div>
</div>
<!-- ================= DASHBOARD VIEW ================= -->
<div id="dashboard-view" class="hidden h-full w-full flex flex-col md:flex-row relative z-10">
<!-- Sidebar -->
<aside class="w-full md:w-80 ios-glass border-l border-white/20 flex flex-col z-20 shadow-2xl md:shadow-none h-full">
<!-- User Profile Header -->
<div class="p-6 border-b border-white/10 bg-white/5">
<div class="flex items-center gap-4">
<div class="relative">
<div class="w-14 h-14 rounded-full bg-gradient-to-tr from-ios-blue to-ios-purple flex items-center justify-center text-white text-xl font-bold shadow-lg border-2 border-white/30">
<span id="user-avatar-initial">U</span>
</div>
<div class="absolute bottom-0 right-0 w-4 h-4 bg-ios-green border-2 border-white rounded-full shadow-sm"></div>
</div>
<div class="flex-1">
<h2 id="user-name-display" class="font-bold text-white text-lg drop-shadow-md">کاربر</h2>
<p id="user-phone-display" class="text-xs text-white/70 font-mono">09--------</p>
</div>
<button onclick="logout()" class="text-white/60 hover:text-white hover:bg-white/20 p-2 rounded-full transition-all" title="خروج">
<i class="fa-solid fa-power-off text-lg"></i>
</button>
</div>
</div>
<!-- Create Room Section -->
<div class="p-6 border-b border-white/10">
<div class="flex justify-between items-center mb-4">
<h3 class="font-bold text-white drop-shadow-sm">اتاق های من</h3>
<span id="room-count-badge" class="ios-glass text-white text-xs font-bold px-3 py-1 rounded-full border border-white/30">0/3</span>
</div>
<button onclick="openCreateRoomModal()" id="btn-create-room" class="w-full py-3 border-2 border-dashed border-white/30 text-white/90 rounded-2xl hover:bg-white/20 hover:border-white/50 transition-all flex items-center justify-center gap-2 font-medium text-sm backdrop-blur-sm">
<i class="fa-solid fa-plus"></i> ساخت اتاق جدید
</button>
<p id="room-limit-msg" class="text-xs text-red-300 mt-3 hidden text-center font-medium drop-shadow">شما به حداکثر تعداد اتاق رسیدید.</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-white/50 text-sm flex flex-col items-center">
<div class="w-16 h-16 ios-glass rounded-full flex items-center justify-center mb-3 border border-white/20">
<i class="fa-regular fa-folder-open text-2xl"></i>
</div>
<p>هنوز اتاقی نساختید</p>
</div>
</div>
</aside>
<!-- Main Content Area -->
<main class="flex-1 flex flex-col h-full relative">
<!-- Top Bar -->
<header class="h-16 ios-glass border-b border-white/10 flex items-center justify-between px-6 shadow-sm z-10">
<div class="flex items-center gap-3">
<div class="md:hidden text-white/80 cursor-pointer hover:bg-white/20 p-2 rounded-full transition-all">
<i class="fa-solid fa-bars text-xl"></i>
</div>
<h1 class="font-bold text-xl text-white drop-shadow-md 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 ios-glass hover:bg-white/30 text-white px-4 py-2 rounded-full transition-all border border-white/20 shadow-sm">
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 overflow-y-auto">
<div class="w-32 h-32 ios-glass rounded-full flex items-center justify-center mb-6 relative border border-white/30 shadow-2xl">
<i class="fa-solid fa-rocket text-4xl text-white/80 drop-shadow-lg"></i>
<div class="absolute inset-0 border-2 border-white/30 border-dashed rounded-full animate-spin-slow" style="animation-duration: 15s;"></div>
</div>
<h2 class="text-3xl font-bold text-white mb-3 drop-shadow-lg">به پیام رسان خوش آمدید</h2>
<p class="text-white/80 max-w-md mb-10 text-lg leading-relaxed">یک اتاق جدید بسازید یا از لیست سمت راست یکی را انتخاب کنید تا گفتگو را شروع کنید.</p>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 w-full max-w-3xl text-right">
<div class="glass-card ios-glass p-5 rounded-2xl border border-white/20 cursor-pointer group">
<div class="w-12 h-12 rounded-2xl bg-ios-blue/30 flex items-center justify-center mb-4 text-white text-xl border border-white/20 shadow-lg group-hover:scale-110 transition-transform">
<i class="fa-solid fa-bolt"></i>
</div>
<h4 class="font-bold text-white mb-2 text-lg drop-shadow-md">سریع و آنی</h4>
<p class="text-sm text-white/70 leading-relaxed">بدون نیاز به سرور، داده ها در مرورگر شما ذخیره می شوند.</p>
</div>
<div class="glass-card ios-glass p-5 rounded-2xl border border-white/20 cursor-pointer group">
<div class="w-12 h-12 rounded-2xl bg-ios-purple/30 flex items-center justify-center mb-4 text-white text-xl border border-white/20 shadow-lg group-hover:scale-110 transition-transform">
<i class="fa-solid fa-link"></i>
</div>
<h4 class="font-bold text-white mb-2 text-lg drop-shadow-md">اشتراک گذاری</h4>
<p class="text-sm text-white/70 leading-relaxed">لینک اتاق را کپی کنید و برای دوستانتان بفرستید.</p>
</div>
<div class="glass-card ios-glass p-5 rounded-2xl border border-white/20 cursor-pointer group">
<div class="w-12 h-12 rounded-2xl bg-ios-green/30 flex items-center justify-center mb-4 text-white text-xl border border-white/20 shadow-lg group-hover:scale-110 transition-transform">
<i class="fa-solid fa-shield-halved"></i>
</div>
<h4 class="font-bold text-white mb-2 text-lg drop-shadow-md">امنیت محلی</h4>
<p class="text-sm text-white/70 leading-relaxed">هیچ داده ای به سرور ارسال نمی شود. کاملا خصوصی.</p>
</div>
</div>
</div>
<!-- Chat Interface (Hidden initially) -->
<div id="chat-interface" class="hidden flex-col h-full absolute inset-0 bg-transparent z-20 animate-fade-in backdrop-blur-sm">
<!-- Chat Header -->
<div class="h-16 ios-glass border-b border-white/10 flex items-center justify-between px-4 shadow-lg">
<div class="flex items-center gap-3">
<button onclick="closeChat()" class="md:hidden w-10 h-10 flex items-center justify-center rounded-full hover:bg-white/20 text-white transition-all">
<i class="fa-solid fa-arrow-right"></i>
</button>
<div class="w-10 h-10 rounded-full bg-gradient-to-br from-ios-indigo to-ios-blue flex items-center justify-center text-white font-bold shadow-md border border-white/30">
<i class="fa-solid fa-hashtag"></i>
</div>
<div>
<h3 id="chat-room-name" class="font-bold text-white drop-shadow-md">نام اتاق</h3>
<div class="flex items-center gap-2 text-xs text-white/70">
<span class="w-2 h-2 bg-ios-green rounded-full shadow-sm animate-pulse"></span>
<span id="chat-user-count">2 عضو آنلاین</span>
</div>
</div>
</div>
<div class="flex items-center gap-2">
<button onclick="copyRoomLink()" class="p-3 text-white/70 hover:text-white hover:bg-white/20 rounded-full transition-all" title="کپی لینک">
<i class="fa-solid fa-share-nodes"></i>
</button>
<button onclick="closeChat()" class="hidden md:block p-3 text-white/70 hover:text-white hover:bg-white/20 rounded-full transition-all" 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">
<!-- Messages injected via JS -->
</div>
<!-- Input Area -->
<div class="p-4 ios-glass border-t border-white/10 backdrop-blur-xl">
<form onsubmit="sendMessage(event)" class="flex gap-3 items-center">
<input type="text" id="message-input" placeholder="پیام خود را بنویسید..." class="flex-1 ios-input rounded-full px-6 py-3.5 text-white placeholder-white/50 focus:outline-none focus:ring-2 focus:ring-white/30" autocomplete="off">
<button type="submit" class="bg-white/20 hover:bg-white/30 text-white w-12 h-12 rounded-full flex items-center justify-center shadow-lg border border-white/40 transition-all transform active:scale-95 hover:rotate-12">
<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-black/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="ios-glass-strong rounded-3xl shadow-2xl p-6 animate-slide-up border border-white/40">
<div class="flex justify-between items-center mb-6">
<h3 class="text-xl font-bold text-white drop-shadow-md">ساخت اتاق جدید</h3>
<button onclick="closeCreateRoomModal()" class="text-white/70 hover:text-white hover:bg-white/20 w-8 h-8 rounded-full flex items-center justify-center transition-all">
<i class="fa-solid fa-xmark text-xl"></i>
</button>
</div>
<form onsubmit="handleCreateRoom(event)">
<div class="mb-6">
<label class="block text-sm font-bold text-white/80 mb-2 mr-1">نام اتاق</label>
<input type="text" id="new-room-name" class="w-full ios-input rounded-2xl px-4 py-3.5 text-white placeholder-white/40 focus:outline-none" placeholder="مثلا: گروه دوستان" required>
</div>
<div class="flex gap-3">
<button type="button" onclick="closeCreateRoomModal()" class="flex-1 py-3.5 rounded-2xl border border-white/30 text-white font-bold hover:bg-white/20 transition-all backdrop-blur-sm">انصراف</button>
<button type="submit" class="flex-1 py-3.5 rounded-2xl bg-white/20 hover:bg-white/30 text-white font-bold border border-white/40 shadow-lg transition-all transform hover:scale-[1.02] active:scale-95 backdrop-blur-sm">ساختن</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('tab-active', 'text-white');
loginTab.classList.remove('text-white/70');
registerTab.classList.remove('tab-active', 'text-white');
registerTab.classList.add('text-white/70');
} else {
loginForm.classList.add('hidden');
registerForm.classList.remove('hidden');
registerTab.classList.add('tab-active', 'text-white');
registerTab.classList.remove('text-white/70');
loginTab.classList.remove('tab-active', 'text-white');
loginTab.classList.add('text-white/70');
}
}
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 },
colors: ['#ffffff', '#rgba(255,255,255,0.8)', '#rgba(255,255,255,0.5)']
});
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-white/50 text-sm flex flex-col items-center">
<div class="w-16 h-16 ios-glass rounded-full flex items-center justify-center mb-3 border border-white/20 shadow-lg">
<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-2xl hover:bg-white/20 cursor-pointer border border-transparent hover:border-white/20 transition-all flex items-center justify-between glass-card mb-2">
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-xl ios-glass flex items-center justify-center font-bold text-white shadow-md border border-white/20 group-hover:scale-110 transition-transform">
${room.name.charAt(0)}
</div>
<div>
<h4 class="font-bold text-white text-sm drop-shadow-sm">${room.name}</h4>
<p class="text-xs text-white/60">ایجاد شده در ${new Date(room.createdAt).toLocaleDateString('fa-IR')}</p>
</div>
</div>
<i class="fa-solid fa-chevron-left text-white/40 text-xs group-hover:text-white group-hover:translate-x-1 transition-all"></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.getElementBy