Update app/page.tsx
Browse files1 unmodified line23456789101112131415163 unmodified lines202122232425267 unmodified lines34353637383918 unmodified lines585960616263646566676869707172737475763 unmodified lines8081828384858687888990919293949596979827 unmodified lines1261271281291301311321331341351361371381391401411421431441451461471481491501511521531 unmodified lineimport { useState, useEffect } from 'react';import useSWR from 'swr';import Link from 'next/link';import { Shield, Hammer, Eye, EyeOff, Trash2, Edit } from 'lucide-react';const fetcher = (url: string) => fetch(url).then(r => r.json());export default function Dashboard() {const [user, setUser] = useState<any>(null);// Real-time pollingconst { data, mutate } = useSWR(user ? `/api/game-data?userId=${user.id}` : null, fetcher, { refreshInterval: 2000 });useEffect(() => {// Load user safely3 unmodified lines}}, []);// --- DM ACTIONS ---async function handleDMAction(type: string, payload: any) {if (!data?.isAdmin) return;try {7 unmodified lines}}function createLog() {const title = prompt("Event Title:");if (!title) return;18 unmodified lines}}// --- LOGOUT ---function logout() {localStorage.removeItem('rpg_user');window.location.reload();}if (!user) return (<div className="h-screen flex flex-col items-center justify-center bg-slate-950 text-white gap-4"><h1 className="text-4xl font-bold text-purple-500">RPG COMPANION</h1><Link href="/login" className="bg-blue-600 px-6 py-2 rounded hover:bg-blue-500">Login / Register</Link></div>);return (<div className="min-h-screen bg-slate-950 text-slate-200 p-6 font-mono">{/* HEADER */}3 unmodified lines</h1><div className="flex gap-4 items-center"><Link href="/profile" className="hover:text-white text-slate-400 border border-slate-700 px-3 py-1 rounded">My Character</Link><button onClick={logout} className="text-red-400 text-sm hover:text-red-300">Logout</button><span className={`text-xs px-2 py-1 rounded ${data?.isAdmin ? 'bg-red-900 text-red-200 border border-red-500' : 'bg-green-900 text-green-200'}`}>{data?.isAdmin ? 'GOD MODE ACTIVE' : 'PLAYER MODE'}</span></div></div><div className="grid grid-cols-1 lg:grid-cols-3 gap-8">{/* TIMELINE */}<div className="col-span-2 space-y-4"><div className="flex justify-between items-center bg-slate-900 p-2 rounded"><h2 className="text-xl font-bold text-blue-400 flex gap-2"><Shield /> TIMELINE</h2>27 unmodified lines</div></div>{/* TECH TREE / BASE */}<div className="space-y-4"><div className="flex justify-between items-center bg-slate-900 p-2 rounded"><h2 className="text-xl font-bold text-green-400 flex gap-2"><Hammer /> UPGRADES</h2>{data?.isAdmin && <button onClick={() => updateTech(prompt("New Tech Name:") || "New Tech", 0)} className="bg-green-700 text-white text-xs px-3 py-2 rounded hover:bg-green-600">+ Add</button>}</div><div className="bg-slate-900 p-4 rounded border border-slate-800 space-y-6 min-h-[300px]">{data?.tech?.map((t: any) => (<div key={t.id} onClick={() => updateTech(t.name, t.progress)} className={data.isAdmin ? "cursor-pointer hover:bg-slate-800 p-2 rounded transition-colors" : ""}><div className="flex justify-between mb-1"><span className="font-bold text-sm text-slate-300">{t.name}</span><span className="text-[10px] bg-slate-800 px-2 py-0.5 rounded text-slate-500 uppercase">{t.status}</span></div><div className="w-full bg-slate-950 h-3 rounded-full overflow-hidden border border-slate-700 relative"><div className="bg-gradient-to-r from-green-600 to-emerald-400 h-full transition-all duration-1000" style={{ width: `${t.progress}%` }}></div></div><div className="text-right text-[10px] text-slate-500 mt-1">{t.progress}%</div></div>))}{data?.tech?.length === 0 && <div className="text-slate-600 text-sm text-center italic mt-10">No active research projects.</div>}</div></div></div>1 unmodified line23456789101112131415161718193 unmodified lines2324252627282930313233343536377 unmodified lines454647484950515253545556575818 unmodified lines77787980818283848586878889909192939495963 unmodified lines10010110210310410510610710810911011111211311411511611727 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052061 unmodified lineimport { useState, useEffect } from 'react';import useSWR from 'swr';import Link from 'next/link';import { Shield, Hammer, Eye, EyeOff, Trash2, Users, LogOut } from 'lucide-react';const fetcher = (url: string) => fetch(url).then(r => r.json());export default function Dashboard() {const [user, setUser] = useState<any>(null);// Real-time polling for Game Dataconst { data, mutate } = useSWR(user ? `/api/game-data?userId=${user.id}` : null, fetcher, { refreshInterval: 2000 });// Real-time polling for User List (Only runs if Admin)const { data: userData, mutate: mutateUsers } = useSWR(data?.isAdmin ? '/api/auth' : null, fetcher);useEffect(() => {// Load user safely3 unmodified lines}}, []);// --- ACTIONS ---function logout() {// HARD LOGOUT: Completely wipe storage to fix crash loopslocalStorage.removeItem('rpg_user');window.location.href = '/login';}// Generic DM Action Handlerasync function handleDMAction(type: string, payload: any) {if (!data?.isAdmin) return;try {7 unmodified lines}}// Delete User Action (DM Only)async function deleteUser(id: number, name: string) {if (confirm(`Are you sure you want to BANISH ${name}? This deletes their character and logs forever.`)) {await fetch(`/api/auth?id=${id}`, { method: 'DELETE' });mutateUsers();}}function createLog() {const title = prompt("Event Title:");if (!title) return;18 unmodified lines}}// --- LOGIN SCREEN / CRASH RECOVERY ---if (!user) return (<div className="h-screen flex flex-col items-center justify-center bg-slate-950 text-white gap-6"><h1 className="text-4xl font-bold text-purple-500">RPG COMPANION</h1><Link href="/login" className="bg-blue-600 px-8 py-3 rounded font-bold hover:bg-blue-500">LOGIN / REGISTER</Link>{/* THE FIX CRASH BUTTON */}<button onClick={logout} className="text-slate-500 hover:text-red-400 text-sm underline mt-4">Reset / Fix Crash (Force Logout)</button></div>);// --- MAIN DASHBOARD ---return (<div className="min-h-screen bg-slate-950 text-slate-200 p-6 font-mono">{/* HEADER */}3 unmodified lines</h1><div className="flex gap-4 items-center"><Link href="/profile" className="hover:text-white text-slate-400 border border-slate-700 px-3 py-1 rounded text-sm">My Character</Link><button onClick={logout} className="bg-red-900/30 text-red-400 px-3 py-1 rounded text-sm hover:bg-red-900/50 flex items-center gap-2"><LogOut size={14}/> Logout</button></div></div><div className="grid grid-cols-1 lg:grid-cols-3 gap-8">{/* LEFT COLUMN: TIMELINE */}<div className="col-span-2 space-y-4"><div className="flex justify-between items-center bg-slate-900 p-2 rounded"><h2 className="text-xl font-bold text-blue-400 flex gap-2"><Shield /> TIMELINE</h2>27 unmodified lines</div></div>{/* RIGHT COLUMN: TECH TREE & USERS */}<div className="space-y-8">{/* TECH TREE SECTION */}<div className="space-y-4"><div className="flex justify-between items-center bg-slate-900 p-2 rounded"><h2 className="text-xl font-bold text-green-400 flex gap-2"><Hammer /> UPGRADES</h2>{data?.isAdmin && <button onClick={() => updateTech(prompt("New Tech Name:") || "New Tech", 0)} className="bg-green-700 text-white text-xs px-3 py-2 rounded hover:bg-green-600">+ Add</button>}</div><div className="bg-slate-900 p-4 rounded border border-slate-800 space-y-6">{data?.tech?.map((t: any) => (<div key={t.id} onClick={() => updateTech(t.name, t.progress)} className={data.isAdmin ? "cursor-pointer hover:bg-slate-800 p-2 rounded transition-colors" : ""}><div className="flex justify-between mb-1"><span className="font-bold text-sm text-slate-300">{t.name}</span><span className="text-[10px] bg-slate-800 px-2 py-0.5 rounded text-slate-500 uppercase">{t.status}</span></div><div className="w-full bg-slate-950 h-3 rounded-full overflow-hidden border border-slate-700 relative"><div className="bg-gradient-to-r from-green-600 to-emerald-400 h-full transition-all duration-1000" style={{ width: `${t.progress}%` }}></div></div><div className="text-right text-[10px] text-slate-500 mt-1">{t.progress}%</div></div>))}{data?.tech?.length === 0 && <div className="text-slate-600 text-sm text-center italic mt-10">No active research projects.</div>}</div></div>{/* USER MANAGEMENT (DM ONLY) */}{data?.isAdmin && (<div className="space-y-4"><div className="bg-slate-900 p-2 rounded flex items-center gap-2"><Users className="text-purple-400" /><h2 className="text-xl font-bold text-purple-400">PLAYERS</h2></div><div className="bg-slate-900 p-4 rounded border border-slate-800 space-y-2 max-h-[250px] overflow-y-auto scrollbar-thin scrollbar-thumb-slate-700">{userData?.users?.map((u: any) => (<div key={u.id} className="flex justify-between items-center p-2 border-b border-slate-800 last:border-0 hover:bg-slate-800/50"><div><div className="font-bold text-sm text-slate-200">{u.username}</div><div className="text-[10px] text-slate-500 uppercase tracking-wider">{u.role}</div></div>{u.role !== 'admin' && (<buttononClick={() => deleteUser(u.id, u.username)}className="text-red-500 hover:text-red-400 p-1 hover:bg-red-900/20 rounded transition"title="Banish User"><Trash2 size={14} /></button>)}</div>))}{userData?.users?.length === 0 && <div className="text-slate-500 text-xs">No users found.</div>}</div></div>)}</div></div>
app/page.tsx+91-38