style: align admin and landing with jokey barnyard aesthetic
This commit is contained in:
+233
-310
@@ -1,331 +1,254 @@
|
||||
'use client';
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useStore } from '@/lib/store';
|
||||
import { adminApi } from '@/lib/api';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import {
|
||||
Users,
|
||||
Settings,
|
||||
Pause,
|
||||
Play,
|
||||
TrendingUp,
|
||||
Search,
|
||||
Gift
|
||||
} from 'lucide-react';
|
||||
import { formatNumber } from '@/lib/utils';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
ArrowLeft,
|
||||
ChevronRight,
|
||||
Database,
|
||||
Minus,
|
||||
Plus,
|
||||
RefreshCcw,
|
||||
Search,
|
||||
Settings,
|
||||
ShieldAlert,
|
||||
TrendingUp,
|
||||
Users,
|
||||
} from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { adminApi, api } from "@/lib/api";
|
||||
import { useStore } from "@/lib/store";
|
||||
import { formatNumber } from "@/lib/utils";
|
||||
|
||||
interface Analytics {
|
||||
users: {
|
||||
total: number;
|
||||
with_wallet: number;
|
||||
new_this_week: number;
|
||||
};
|
||||
transactions: {
|
||||
total_earned: number;
|
||||
total_spent: number;
|
||||
total_transactions: number;
|
||||
};
|
||||
watchStats: {
|
||||
total_seconds: number;
|
||||
total_credits: number;
|
||||
total_events: number;
|
||||
};
|
||||
users: { total: number; with_wallet: number; active_30d: number };
|
||||
transactions: {
|
||||
total_earned: number;
|
||||
total_spent: number;
|
||||
total_count: number;
|
||||
};
|
||||
requests: {
|
||||
total: number;
|
||||
pending: number;
|
||||
approved: number;
|
||||
declined: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
plexUsername: string;
|
||||
email: string | null;
|
||||
isAdmin: boolean;
|
||||
isActive: boolean;
|
||||
totalEarned: number;
|
||||
totalSpent: number;
|
||||
watchTimeMinutes: number;
|
||||
createdAt: string;
|
||||
id: string;
|
||||
plexUsername: string;
|
||||
isAdmin: boolean;
|
||||
totalEarned: number;
|
||||
totalSpent: number;
|
||||
}
|
||||
|
||||
export default function AdminPage() {
|
||||
const router = useRouter();
|
||||
const { user, isAuthenticated } = useStore();
|
||||
const [analytics, setAnalytics] = useState<Analytics | null>(null);
|
||||
const [users, setUsers] = useState<User[]>([]);
|
||||
const [settings, setSettings] = useState<any>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const router = useRouter();
|
||||
const { user, isAuthenticated } = useStore();
|
||||
const [analytics, setAnalytics] = useState<Analytics | null>(null);
|
||||
const [users, setUsers] = useState<User[]>([]);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) {
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated || !user?.isAdmin) {
|
||||
router.push("/dashboard");
|
||||
return;
|
||||
}
|
||||
loadData();
|
||||
}, [isAuthenticated, user, router]);
|
||||
|
||||
if (!user?.isAdmin) {
|
||||
router.push('/dashboard');
|
||||
return;
|
||||
}
|
||||
const loadData = async () => {
|
||||
try {
|
||||
const [analyticsRes, usersRes] = await Promise.all([
|
||||
api.get("/admin/analytics"),
|
||||
api.get("/admin/users"),
|
||||
]);
|
||||
setAnalytics(analyticsRes.data);
|
||||
setUsers(usersRes.data);
|
||||
} catch {
|
||||
toast.error("Failed to load coop secrets");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadData();
|
||||
}, [isAuthenticated, user, router]);
|
||||
const adjustUser = async (userId: string, amount: number, reason: string) => {
|
||||
try {
|
||||
await adminApi.adjustUser(userId, amount, reason);
|
||||
toast.success("Feathers adjusted!");
|
||||
loadData();
|
||||
} catch {
|
||||
toast.error("Failed to adjust nest");
|
||||
}
|
||||
};
|
||||
|
||||
const loadData = async () => {
|
||||
try {
|
||||
const [analyticsRes, usersRes, settingsRes] = await Promise.all([
|
||||
adminApi.getAnalytics(),
|
||||
adminApi.getUsers(),
|
||||
adminApi.getSettings()
|
||||
]);
|
||||
const toggleAdmin = async (userId: string, isAdmin: boolean) => {
|
||||
try {
|
||||
await api.patch(`/admin/users/${userId}`, { isAdmin });
|
||||
toast.success(isAdmin ? "Promoted to Rooster!" : "Demoted to Hen.");
|
||||
loadData();
|
||||
} catch {
|
||||
toast.error("Failed to change pecking order");
|
||||
}
|
||||
};
|
||||
|
||||
setAnalytics(analyticsRes.data);
|
||||
setUsers(usersRes.data.users);
|
||||
setSettings(settingsRes.data);
|
||||
} catch (error) {
|
||||
toast.error('Failed to load admin data');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
const filteredUsers = users.filter((u) =>
|
||||
u.plexUsername?.toLowerCase().includes(searchQuery.toLowerCase()),
|
||||
);
|
||||
|
||||
const handlePause = async () => {
|
||||
try {
|
||||
await adminApi.pause();
|
||||
toast.success('Minting paused');
|
||||
loadData();
|
||||
} catch (error) {
|
||||
toast.error('Failed to pause minting');
|
||||
}
|
||||
};
|
||||
if (isLoading)
|
||||
return (
|
||||
<div className="p-8 font-display animate-pulse text-primary">
|
||||
COUNTING THE CHICKENS...
|
||||
</div>
|
||||
);
|
||||
|
||||
const handleResume = async () => {
|
||||
try {
|
||||
await adminApi.resume();
|
||||
toast.success('Minting resumed');
|
||||
loadData();
|
||||
} catch (error) {
|
||||
toast.error('Failed to resume minting');
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="min-h-screen bg-background p-4 sm:p-8 font-sans">
|
||||
<div className="mx-auto max-w-6xl space-y-8">
|
||||
{/* Admin Header */}
|
||||
<div className="flex flex-col items-start justify-between gap-4 rounded-3xl border-4 border-primary bg-secondary/20 p-6 shadow-[8px_8px_0px_0px_rgba(0,0,0,1)] md:flex-row md:items-center">
|
||||
<div className="flex items-center gap-4">
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={() => router.push("/dashboard")}
|
||||
className="rounded-full p-2 h-10 w-10"
|
||||
>
|
||||
<ArrowLeft className="h-6 w-6" />
|
||||
</Button>
|
||||
<div>
|
||||
<h1 className="font-display text-4xl leading-none text-primary uppercase italic tracking-tighter">
|
||||
COOP CONTROL
|
||||
</h1>
|
||||
<p className="text-sm font-bold text-muted-foreground">
|
||||
Admin Peering Glass
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
onClick={loadData}
|
||||
variant="outline"
|
||||
className="rounded-full border-2 border-primary font-bold"
|
||||
>
|
||||
<RefreshCcw className="mr-2 h-4 w-4" /> REFRESH
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
const handleGrantBonus = async (userId: string) => {
|
||||
const amount = prompt('Enter bonus amount:');
|
||||
if (!amount) return;
|
||||
{/* Big Stats */}
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<div className="rounded-2xl border-4 border-foreground bg-card p-4 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
|
||||
<div className="flex items-center gap-2 text-primary font-black uppercase text-[10px]">
|
||||
<Users className="h-3 w-3" /> Flock Size
|
||||
</div>
|
||||
<div className="mt-1 text-3xl font-black">
|
||||
{analytics?.users.total || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border-4 border-foreground bg-card p-4 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
|
||||
<div className="flex items-center gap-2 text-primary font-black uppercase text-[10px]">
|
||||
<Database className="h-3 w-3" /> Total $COOP
|
||||
</div>
|
||||
<div className="mt-1 text-3xl font-black">
|
||||
{formatNumber(analytics?.transactions.total_earned || 0)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border-4 border-foreground bg-card p-4 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
|
||||
<div className="flex items-center gap-2 text-primary font-black uppercase text-[10px]">
|
||||
<TrendingUp className="h-3 w-3" /> Requests
|
||||
</div>
|
||||
<div className="mt-1 text-3xl font-black">
|
||||
{analytics?.requests.total || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border-4 border-foreground bg-card p-4 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
|
||||
<div className="flex items-center gap-2 text-primary font-black uppercase text-[10px]">
|
||||
<ShieldAlert className="h-3 w-3" /> Pending
|
||||
</div>
|
||||
<div className="mt-1 text-3xl font-black">
|
||||
{analytics?.requests.pending || 0}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
try {
|
||||
await adminApi.grantBonus(userId, parseInt(amount), 'Admin bonus');
|
||||
toast.success('Bonus granted');
|
||||
loadData();
|
||||
} catch (error) {
|
||||
toast.error('Failed to grant bonus');
|
||||
}
|
||||
};
|
||||
{/* User Management */}
|
||||
<div className="rounded-[2.5rem] border-4 border-foreground bg-card p-6 shadow-[10px_10px_0px_0px_rgba(0,0,0,0.05)]">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<h2 className="font-display text-3xl uppercase italic">
|
||||
The Peeps
|
||||
</h2>
|
||||
<div className="relative w-full sm:w-64">
|
||||
<Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder="Search chickens..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
className="rounded-full border-2 border-foreground pl-10 focus-visible:ring-primary"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
if (!isAuthenticated || !user?.isAdmin) {
|
||||
return null;
|
||||
}
|
||||
<div className="space-y-4">
|
||||
{filteredUsers.map((u) => (
|
||||
<div
|
||||
key={u.id}
|
||||
className="flex flex-col gap-4 rounded-2xl border-2 border-foreground bg-accent/5 p-4 sm:flex-row sm:items-center sm:justify-between hover:bg-accent/10 transition-colors"
|
||||
>
|
||||
<div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-black text-lg">{u.plexUsername}</span>
|
||||
{u.isAdmin && (
|
||||
<span className="rounded-full bg-primary px-2 py-0.5 text-[10px] font-bold text-white uppercase">
|
||||
Rooster
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-xs font-bold text-muted-foreground">
|
||||
Balance: {formatNumber(u.totalEarned - u.totalSpent)} $COOP
|
||||
</div>
|
||||
</div>
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center">
|
||||
<p>Loading...</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
<header className="border-b bg-card">
|
||||
<div className="max-w-7xl mx-auto px-4 py-4 flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<h1 className="text-2xl font-bold">Admin Dashboard</h1>
|
||||
<Badge variant="secondary">{user.plexUsername}</Badge>
|
||||
</div>
|
||||
<Button variant="ghost" onClick={() => router.push('/dashboard')}>
|
||||
Back to Dashboard
|
||||
</Button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="max-w-7xl mx-auto px-4 py-8">
|
||||
{/* Stats Overview */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between pb-2">
|
||||
<CardTitle className="text-sm font-medium">Total Users</CardTitle>
|
||||
<Users className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{analytics?.users.total || 0}</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{analytics?.users.with_wallet || 0} with wallets
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between pb-2">
|
||||
<CardTitle className="text-sm font-medium">Total Minted</CardTitle>
|
||||
<TrendingUp className="h-4 w-4 text-green-500" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatNumber(analytics?.transactions.total_earned || 0)} $COOP
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{analytics?.transactions.total_transactions || 0} transactions
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between pb-2">
|
||||
<CardTitle className="text-sm font-medium">Watch Time</CardTitle>
|
||||
<TrendingUp className="h-4 w-4 text-blue-500" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{Math.floor((analytics?.watchStats.total_seconds || 0) / 3600)}h
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{analytics?.watchStats.total_events || 0} watch events
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between pb-2">
|
||||
<CardTitle className="text-sm font-medium">System Status</CardTitle>
|
||||
<Settings className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className={`w-2 h-2 rounded-full ${settings?.mintingPaused ? 'bg-red-500' : 'bg-green-500'}`} />
|
||||
<span className="font-bold">
|
||||
{settings?.mintingPaused ? 'Paused' : 'Active'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex gap-2 mt-2">
|
||||
<Button size="sm" variant="outline" onClick={handlePause} disabled={settings?.mintingPaused}>
|
||||
<Pause className="h-3 w-3 mr-1" />
|
||||
Pause
|
||||
</Button>
|
||||
<Button size="sm" variant="outline" onClick={handleResume} disabled={!settings?.mintingPaused}>
|
||||
<Play className="h-3 w-3 mr-1" />
|
||||
Resume
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<Tabs defaultValue="users">
|
||||
<TabsList>
|
||||
<TabsTrigger value="users">Users</TabsTrigger>
|
||||
<TabsTrigger value="settings">Settings</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="users" className="space-y-4">
|
||||
<div className="flex gap-4">
|
||||
<div className="relative flex-1 max-w-sm">
|
||||
<Search className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder="Search users..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
className="pl-10"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
<div className="divide-y">
|
||||
{users.map((u) => (
|
||||
<div key={u.id} className="flex items-center justify-between p-4">
|
||||
<div>
|
||||
<p className="font-medium">{u.plexUsername}</p>
|
||||
<p className="text-sm text-muted-foreground">{u.email}</p>
|
||||
<div className="flex gap-2 mt-1">
|
||||
{u.isAdmin && <Badge variant="default">Admin</Badge>}
|
||||
{!u.isActive && <Badge variant="destructive">Inactive</Badge>}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="font-mono text-sm">
|
||||
{formatNumber(u.totalEarned - u.totalSpent)} $COOP
|
||||
</p>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => handleGrantBonus(u.id)}
|
||||
>
|
||||
<Gift className="h-4 w-4 mr-1" />
|
||||
Bonus
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="settings">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Minting Settings</CardTitle>
|
||||
<CardDescription>Configure how users earn $COOP</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Credits Per Minute</Label>
|
||||
<Input
|
||||
type="number"
|
||||
value={settings?.creditsPerMinute || 10}
|
||||
onChange={(e) => setSettings({ ...settings, creditsPerMinute: parseInt(e.target.value) })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>Min Watch Percent</Label>
|
||||
<Input
|
||||
type="number"
|
||||
value={settings?.minWatchPercent || 80}
|
||||
onChange={(e) => setSettings({ ...settings, minWatchPercent: parseInt(e.target.value) })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>Movie Request Cost</Label>
|
||||
<Input
|
||||
type="number"
|
||||
value={settings?.movieRequestCost || 100}
|
||||
onChange={(e) => setSettings({ ...settings, movieRequestCost: parseInt(e.target.value) })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>TV Request Cost</Label>
|
||||
<Input
|
||||
type="number"
|
||||
value={settings?.tvRequestCost || 200}
|
||||
onChange={(e) => setSettings({ ...settings, tvRequestCost: parseInt(e.target.value) })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Button onClick={() => adminApi.updateSettings(settings).then(() => toast.success('Settings saved'))}>
|
||||
Save Settings
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="rounded-full border-2"
|
||||
onClick={() => adjustUser(u.id, 100, "Admin bonus")}
|
||||
>
|
||||
<Plus className="h-3 w-3 mr-1" /> 100
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="rounded-full border-2"
|
||||
onClick={() => adjustUser(u.id, -100, "Admin correction")}
|
||||
>
|
||||
<Minus className="h-3 w-3 mr-1" /> 100
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant={u.isAdmin ? "destructive" : "secondary"}
|
||||
className="rounded-full font-bold"
|
||||
onClick={() => toggleAdmin(u.id, !u.isAdmin)}
|
||||
>
|
||||
{u.isAdmin ? "Demote" : "Make Admin"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user