refactor: rename admin route group to manage

This commit is contained in:
2026-04-29 01:25:00 +08:00
parent fcfcce614a
commit b68bf36bff
17 changed files with 16 additions and 12 deletions
@@ -0,0 +1,604 @@
"use client"
import { useCallback, useEffect, useRef, useState } from "react"
import Image from "next/image"
import Link from "next/link"
import {
Loader2,
SearchIcon,
ShieldCheckIcon,
TrashIcon,
RefreshCwIcon,
ExternalLinkIcon,
ChevronLeftIcon,
ChevronRightIcon,
} from "lucide-react"
interface PerformanceEntry {
id: string
versionId: string
hardwareSlug: string
userId: string
fpsAvg: number | null
fpsLow: number | null
fpsHigh: number | null
protonVersion: string | null
osVersion: string | null
upscalerType: string | null
upscalerVersion: string | null
frameGenMethod: string | null
loadTimeSsd: number | null
loadTimeSd: number | null
launchOptions: string | null
settingsJson: string | null
userNotes: string | null
estimatedBatteryMin: number | null
customSystem: string | null
isRemoved: boolean
removedReason: string | null
upvotes: number
downvotes: number
verifiedAt: string | null
verifiedBy: string | null
createdAt: string
updatedAt: string
gameId: string
gameTitle: string
versionString: string
hardwareName: string
authorName: string | null
authorImage: string | null
}
interface PerformanceApiResponse {
data: PerformanceEntry[]
total: number
limit: number
offset: number
}
type StatusFilter = "all" | "active" | "removed" | "unverified"
const LIMIT = 20
function formatDate(value: string | Date | null | undefined) {
if (!value) return "—"
const d = typeof value === "string" ? new Date(value) : value
return d.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
})
}
function getInitial(name: string | null | undefined) {
return name?.charAt(0)?.toUpperCase() || "?"
}
function statusBadgeClasses(entry: PerformanceEntry) {
if (entry.verifiedAt) return "bg-green-500/10 text-green-400"
if (entry.isRemoved) return "bg-red-500/10 text-red-400"
return "bg-text/5 text-text/50"
}
function statusLabel(entry: PerformanceEntry) {
if (entry.verifiedAt) return "Verified"
if (entry.isRemoved) return "Removed"
return "Active"
}
function statusDotClass(entry: PerformanceEntry) {
if (entry.verifiedAt) return "bg-green-400"
if (entry.isRemoved) return "bg-red-400"
return "bg-text/40"
}
function formatFps(entry: PerformanceEntry) {
if (entry.fpsAvg == null) return "—"
if (entry.fpsLow != null && entry.fpsHigh != null) {
return `${entry.fpsAvg} (${entry.fpsLow}${entry.fpsHigh})`
}
return `${entry.fpsAvg}`
}
export function BenchmarksClient() {
const [entries, setEntries] = useState<PerformanceEntry[]>([])
const [total, setTotal] = useState(0)
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState("")
const [offset, setOffset] = useState(0)
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
const [actionLoading, setActionLoading] = useState<Record<string, boolean>>({})
const [confirmAction, setConfirmAction] = useState<
| { type: "verify" | "remove" | "restore"; entry: PerformanceEntry }
| null
>(null)
const [removeReason, setRemoveReason] = useState("")
const isSearchChangeRef = useRef(false)
const handleSearchChange = (value: string) => {
setSearch(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handleStatusChange = (value: StatusFilter) => {
setStatusFilter(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handlePrev = () => {
setOffset((prev) => Math.max(0, prev - LIMIT))
isSearchChangeRef.current = false
}
const handleNext = () => {
setOffset((prev) => prev + LIMIT)
isSearchChangeRef.current = false
}
const loadData = useCallback(async () => {
setLoading(true)
try {
const params = new URLSearchParams({
limit: String(LIMIT),
offset: String(offset),
})
if (statusFilter === "active") {
params.set("removed", "false")
} else if (statusFilter === "removed") {
params.set("removed", "true")
} else if (statusFilter === "unverified") {
params.set("verified", "false")
}
if (search.trim()) {
params.set("search", search.trim())
}
const res = await fetch(`/api/admin/performance?${params.toString()}`)
if (res.ok) {
const json = (await res.json()) as PerformanceApiResponse
setEntries(json.data)
setTotal(json.total)
}
} catch {
// ignore
} finally {
setLoading(false)
}
}, [search, offset, statusFilter])
useEffect(() => {
const delay = isSearchChangeRef.current ? 300 : 0
isSearchChangeRef.current = false
let cancelled = false
const timer = setTimeout(async () => {
if (!cancelled) {
await loadData()
}
}, delay)
return () => {
cancelled = true
clearTimeout(timer)
}
}, [loadData])
const handleVerify = async (entry: PerformanceEntry) => {
setActionLoading((prev) => ({ ...prev, [entry.id]: true }))
try {
const res = await fetch(`/api/admin/performance/${entry.id}/verify`, {
method: "PATCH",
})
if (res.ok) {
await loadData()
setConfirmAction(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [entry.id]: false }))
}
}
const handleRemove = async (entry: PerformanceEntry) => {
setActionLoading((prev) => ({ ...prev, [entry.id]: true }))
try {
const body: { reason?: string } = {}
if (removeReason.trim()) {
body.reason = removeReason.trim()
}
const res = await fetch(`/api/admin/performance/${entry.id}/remove`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
if (res.ok) {
setRemoveReason("")
await loadData()
setConfirmAction(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [entry.id]: false }))
}
}
const handleRestore = async (entry: PerformanceEntry) => {
setActionLoading((prev) => ({ ...prev, [entry.id]: true }))
try {
const res = await fetch(`/api/admin/performance/${entry.id}/restore`, {
method: "PATCH",
})
if (res.ok) {
await loadData()
setConfirmAction(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [entry.id]: false }))
}
}
const hasMore = offset + entries.length < total
const tabs: { label: string; value: StatusFilter }[] = [
{ label: "All", value: "all" },
{ label: "Active", value: "active" },
{ label: "Removed", value: "removed" },
{ label: "Unverified", value: "unverified" },
]
return (
<div className="space-y-4">
{/* Search */}
<div className="relative">
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
<input
type="text"
value={search}
onChange={(e) => handleSearchChange(e.target.value)}
placeholder="Search benchmarks..."
className="w-full pl-9 pr-4 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
{/* Filter tabs */}
<div className="flex items-center gap-1">
{tabs.map((tab) => (
<button
key={tab.value}
onClick={() => handleStatusChange(tab.value)}
className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors cursor-pointer ${
statusFilter === tab.value
? "bg-primary/10 text-primary"
: "bg-text/5 text-text/70 hover:bg-text/10"
}`}
>
{tab.label}
</button>
))}
</div>
{/* Table */}
<div className="rounded-xl border border-border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-text/[0.03]">
<tr>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Game
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Author
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Hardware
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
FPS
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Upscaler
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Status
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Votes
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Date
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Actions
</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan={9} className="px-4 py-8 text-center text-text/50">
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
</td>
</tr>
) : entries.length === 0 ? (
<tr>
<td colSpan={9} className="px-4 py-8 text-center text-text/50">
No benchmarks found.
</td>
</tr>
) : (
entries.map((entry) => (
<tr
key={entry.id}
className="border-t border-border hover:bg-text/[0.02] transition-colors"
>
<td className="px-4 py-3">
<p className="font-medium text-text truncate max-w-[180px]">
{entry.gameTitle}
</p>
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-3">
{entry.authorImage ? (
<Image
src={entry.authorImage}
alt={entry.authorName || "User"}
width={32}
height={32}
className="h-8 w-8 rounded-full object-cover"
unoptimized
/>
) : (
<div className="h-8 w-8 rounded-full bg-text/10 flex items-center justify-center text-xs font-medium text-text/70">
{getInitial(entry.authorName)}
</div>
)}
<div className="min-w-0">
<p className="font-medium text-text truncate">
{entry.authorName || "Unknown"}
</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<p className="text-text/70 truncate max-w-[120px]">
{entry.hardwareName}
</p>
</td>
<td className="px-4 py-3">
<span className="text-text/70">{formatFps(entry)}</span>
</td>
<td className="px-4 py-3">
<span className="text-text/50">
{entry.upscalerType || "—"}
</span>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${statusBadgeClasses(entry)}`}
>
{entry.verifiedAt ? (
<ShieldCheckIcon className="h-3 w-3" />
) : (
<span
className={`h-1.5 w-1.5 rounded-full ${statusDotClass(entry)}`}
/>
)}
{statusLabel(entry)}
</span>
</td>
<td className="px-4 py-3">
<span className="inline-flex items-center gap-2 text-xs text-text/70">
<span> {entry.upvotes}</span>
<span> {entry.downvotes}</span>
</span>
</td>
<td className="px-4 py-3 text-xs text-text/50">
{formatDate(entry.createdAt)}
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2 flex-wrap">
<Link
href={`/game/${entry.gameId}`}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors"
>
<ExternalLinkIcon className="h-3.5 w-3.5" />
View
</Link>
{!entry.verifiedAt && (
<button
onClick={() =>
setConfirmAction({ type: "verify", entry })
}
disabled={actionLoading[entry.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<ShieldCheckIcon className="h-3.5 w-3.5" />
)}
Verify
</button>
)}
{!entry.isRemoved ? (
<button
onClick={() =>
setConfirmAction({ type: "remove", entry })
}
disabled={actionLoading[entry.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<TrashIcon className="h-3.5 w-3.5" />
)}
Remove
</button>
) : (
<button
onClick={() =>
setConfirmAction({ type: "restore", entry })
}
disabled={actionLoading[entry.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<RefreshCwIcon className="h-3.5 w-3.5" />
)}
Restore
</button>
)}
</div>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
{entries.length > 0 && (
<div className="flex items-center justify-between">
<p className="text-xs text-text/50">
Showing {offset + 1}{Math.min(offset + entries.length, total)} of{" "}
{total}
</p>
<div className="flex items-center gap-2">
<button
onClick={handlePrev}
disabled={offset === 0 || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
<ChevronLeftIcon className="h-3.5 w-3.5" />
Previous
</button>
<button
onClick={handleNext}
disabled={!hasMore || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
Next
<ChevronRightIcon className="h-3.5 w-3.5" />
</button>
</div>
</div>
)}
{/* Confirmation Dialogs */}
{confirmAction && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div className="bg-background border border-border rounded-xl p-6 max-w-sm w-full mx-4 space-y-4">
{confirmAction.type === "verify" && (
<>
<h2 className="text-base font-semibold text-text">
Confirm Verify
</h2>
<p className="text-sm text-text/70">
Are you sure you want to verify this benchmark? It will be
marked as verified.
</p>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => setConfirmAction(null)}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleVerify(confirmAction.entry)}
disabled={actionLoading[confirmAction.entry.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmAction.entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<ShieldCheckIcon className="h-3.5 w-3.5" />
)}
Verify
</button>
</div>
</>
)}
{confirmAction.type === "remove" && (
<>
<h2 className="text-base font-semibold text-text">
Confirm Remove
</h2>
<p className="text-sm text-text/70">
Are you sure you want to remove this benchmark?
</p>
<textarea
rows={3}
value={removeReason}
onChange={(e) => setRemoveReason(e.target.value)}
placeholder="Optional reason..."
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors resize-none"
/>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => {
setConfirmAction(null)
setRemoveReason("")
}}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleRemove(confirmAction.entry)}
disabled={actionLoading[confirmAction.entry.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmAction.entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<TrashIcon className="h-3.5 w-3.5" />
)}
Remove
</button>
</div>
</>
)}
{confirmAction.type === "restore" && (
<>
<h2 className="text-base font-semibold text-text">
Confirm Restore
</h2>
<p className="text-sm text-text/70">
Are you sure you want to restore this benchmark?
</p>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => setConfirmAction(null)}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleRestore(confirmAction.entry)}
disabled={actionLoading[confirmAction.entry.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmAction.entry.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<RefreshCwIcon className="h-3.5 w-3.5" />
)}
Restore
</button>
</div>
</>
)}
</div>
</div>
)}
</div>
)
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { BenchmarksClient } from "./benchmarks-client"
export const metadata: Metadata = {
title: "Benchmarks",
}
export default function BenchmarksPage() {
return <BenchmarksClient />
}
@@ -0,0 +1,491 @@
"use client"
import { useCallback, useEffect, useRef, useState } from "react"
import Image from "next/image"
import Link from "next/link"
import {
Loader2,
SearchIcon,
TrashIcon,
RotateCcwIcon,
ExternalLinkIcon,
ChevronLeftIcon,
ChevronRightIcon,
MessageSquareIcon,
} from "lucide-react"
interface Comment {
id: string
gameId: string
content: Record<string, unknown>
upvotes: number
isRemoved: boolean
createdAt: string
updatedAt: string
userId: string
userName: string | null
userImage: string | null
gameTitle: string
parentId: string | null
}
interface CommentsApiResponse {
data: Comment[]
total: number
limit: number
offset: number
}
type StatusFilter = "all" | "active" | "removed"
const LIMIT = 20
function formatDate(value: string | Date | null | undefined) {
if (!value) return "—"
const d = typeof value === "string" ? new Date(value) : value
return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })
}
function getInitial(name: string | null | undefined) {
return name?.charAt(0)?.toUpperCase() || "?"
}
function extractPlainText(content: Record<string, unknown>, maxLength = 80): string {
let result = ""
function walk(node: unknown) {
if (typeof node !== "object" || node === null) return
if (Array.isArray(node)) {
for (const item of node) {
walk(item)
if (result.length >= maxLength) return
}
return
}
const obj = node as Record<string, unknown>
if (typeof obj.text === "string") {
result += obj.text
if (result.length >= maxLength) return
}
if (Array.isArray(obj.content)) {
for (const item of obj.content) {
walk(item)
if (result.length >= maxLength) return
}
}
}
walk(content)
return result.length > maxLength ? result.slice(0, maxLength) + "…" : result
}
function statusBadgeClasses(isRemoved: boolean) {
return isRemoved
? "bg-red-500/10 text-red-400"
: "bg-green-500/10 text-green-400"
}
function statusDotClass(isRemoved: boolean) {
return isRemoved ? "bg-red-400" : "bg-green-400"
}
export function CommentsClient() {
const [comments, setComments] = useState<Comment[]>([])
const [total, setTotal] = useState(0)
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState("")
const [offset, setOffset] = useState(0)
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
const [actionLoading, setActionLoading] = useState<Record<string, boolean>>({})
const [confirmAction, setConfirmAction] = useState<
| { type: "remove" | "restore"; comment: Comment }
| null
>(null)
const isSearchChangeRef = useRef(false)
const handleSearchChange = (value: string) => {
setSearch(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handleStatusChange = (value: StatusFilter) => {
setStatusFilter(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handlePrev = () => {
setOffset((prev) => Math.max(0, prev - LIMIT))
isSearchChangeRef.current = false
}
const handleNext = () => {
setOffset((prev) => prev + LIMIT)
isSearchChangeRef.current = false
}
const loadData = useCallback(async () => {
setLoading(true)
try {
const params = new URLSearchParams({
limit: String(LIMIT),
offset: String(offset),
})
if (statusFilter === "active") {
params.set("removed", "false")
} else if (statusFilter === "removed") {
params.set("removed", "true")
}
if (search.trim()) {
params.set("search", search.trim())
}
const res = await fetch(`/api/admin/comments?${params.toString()}`)
if (res.ok) {
const json = (await res.json()) as CommentsApiResponse
setComments(json.data)
setTotal(json.total)
}
} catch {
// ignore
} finally {
setLoading(false)
}
}, [search, offset, statusFilter])
useEffect(() => {
const delay = isSearchChangeRef.current ? 300 : 0
isSearchChangeRef.current = false
let cancelled = false
const timer = setTimeout(async () => {
if (!cancelled) {
await loadData()
}
}, delay)
return () => {
cancelled = true
clearTimeout(timer)
}
}, [loadData])
const handleRemove = async (comment: Comment) => {
setActionLoading((prev) => ({ ...prev, [comment.id]: true }))
try {
const res = await fetch(`/api/admin/comments/${comment.id}/remove`, {
method: "PATCH",
})
if (res.ok) {
await loadData()
setConfirmAction(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [comment.id]: false }))
}
}
const handleRestore = async (comment: Comment) => {
setActionLoading((prev) => ({ ...prev, [comment.id]: true }))
try {
const res = await fetch(`/api/admin/comments/${comment.id}/restore`, {
method: "PATCH",
})
if (res.ok) {
await loadData()
setConfirmAction(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [comment.id]: false }))
}
}
const hasMore = offset + comments.length < total
const tabs: { label: string; value: StatusFilter }[] = [
{ label: "All", value: "all" },
{ label: "Active", value: "active" },
{ label: "Removed", value: "removed" },
]
return (
<div className="space-y-4">
{/* Header */}
<div className="flex items-center gap-3">
<MessageSquareIcon className="h-5 w-5 text-text/70" />
<h1 className="text-lg font-semibold text-text">Comments</h1>
</div>
{/* Search */}
<div className="relative">
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
<input
type="text"
value={search}
onChange={(e) => handleSearchChange(e.target.value)}
placeholder="Search comments..."
className="w-full pl-9 pr-4 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
{/* Filter tabs */}
<div className="flex items-center gap-1">
{tabs.map((tab) => (
<button
key={tab.value}
onClick={() => handleStatusChange(tab.value)}
className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors cursor-pointer ${
statusFilter === tab.value
? "bg-primary/10 text-primary"
: "bg-text/5 text-text/70 hover:bg-text/10"
}`}
>
{tab.label}
</button>
))}
</div>
{/* Table */}
<div className="rounded-xl border border-border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-text/[0.03]">
<tr>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Author
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Content
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Game
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Upvotes
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Status
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Date
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Actions
</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan={7} className="px-4 py-8 text-center text-text/50">
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
</td>
</tr>
) : comments.length === 0 ? (
<tr>
<td colSpan={7} className="px-4 py-8 text-center text-text/50">
No comments found.
</td>
</tr>
) : (
comments.map((comment) => (
<tr
key={comment.id}
className="border-t border-border hover:bg-text/[0.02] transition-colors"
>
<td className="px-4 py-3">
<div className="flex items-center gap-3">
{comment.userImage ? (
<Image
src={comment.userImage}
alt={comment.userName || "User"}
width={32}
height={32}
className="h-8 w-8 rounded-full object-cover"
unoptimized
/>
) : (
<div className="h-8 w-8 rounded-full bg-text/10 flex items-center justify-center text-xs font-medium text-text/70">
{getInitial(comment.userName)}
</div>
)}
<div className="min-w-0">
<p className="font-medium text-text truncate">
{comment.userName || "Unknown"}
</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<p className="text-text/70 truncate max-w-[240px]">
{extractPlainText(comment.content, 80)}
</p>
</td>
<td className="px-4 py-3">
<p className="font-medium text-text truncate max-w-[150px]">
{comment.gameTitle}
</p>
</td>
<td className="px-4 py-3">
<span className="text-text/70">{comment.upvotes}</span>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${statusBadgeClasses(comment.isRemoved)}`}
>
<span
className={`h-1.5 w-1.5 rounded-full ${statusDotClass(comment.isRemoved)}`}
/>
{comment.isRemoved ? "Removed" : "Active"}
</span>
</td>
<td className="px-4 py-3 text-xs text-text/50">
{formatDate(comment.createdAt)}
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2 flex-wrap">
<Link
href={`/game/${comment.gameId}`}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors"
>
<ExternalLinkIcon className="h-3.5 w-3.5" />
View
</Link>
{!comment.isRemoved ? (
<button
onClick={() =>
setConfirmAction({ type: "remove", comment })
}
disabled={actionLoading[comment.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[comment.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<TrashIcon className="h-3.5 w-3.5" />
)}
Remove
</button>
) : (
<button
onClick={() =>
setConfirmAction({ type: "restore", comment })
}
disabled={actionLoading[comment.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[comment.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<RotateCcwIcon className="h-3.5 w-3.5" />
)}
Restore
</button>
)}
</div>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
{comments.length > 0 && (
<div className="flex items-center justify-between">
<p className="text-xs text-text/50">
Showing {offset + 1}{Math.min(offset + comments.length, total)} of{" "}
{total}
</p>
<div className="flex items-center gap-2">
<button
onClick={handlePrev}
disabled={offset === 0 || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
<ChevronLeftIcon className="h-3.5 w-3.5" />
Previous
</button>
<button
onClick={handleNext}
disabled={!hasMore || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
Next
<ChevronRightIcon className="h-3.5 w-3.5" />
</button>
</div>
</div>
)}
{/* Confirmation Dialogs */}
{confirmAction && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div className="bg-background border border-border rounded-xl p-6 max-w-sm w-full mx-4 space-y-4">
{confirmAction.type === "remove" && (
<>
<h2 className="text-base font-semibold text-text">
Confirm Remove
</h2>
<p className="text-sm text-text/70">
Are you sure you want to remove this comment?
</p>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => setConfirmAction(null)}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleRemove(confirmAction.comment)}
disabled={actionLoading[confirmAction.comment.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmAction.comment.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<TrashIcon className="h-3.5 w-3.5" />
)}
Remove
</button>
</div>
</>
)}
{confirmAction.type === "restore" && (
<>
<h2 className="text-base font-semibold text-text">
Confirm Restore
</h2>
<p className="text-sm text-text/70">
Are you sure you want to restore this comment?
</p>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => setConfirmAction(null)}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleRestore(confirmAction.comment)}
disabled={actionLoading[confirmAction.comment.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmAction.comment.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<RotateCcwIcon className="h-3.5 w-3.5" />
)}
Restore
</button>
</div>
</>
)}
</div>
</div>
)}
</div>
)
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { CommentsClient } from "./comments-client"
export const metadata: Metadata = {
title: "Comments",
}
export default function CommentsPage() {
return <CommentsClient />
}
+288
View File
@@ -0,0 +1,288 @@
"use client"
import { useEffect, useRef, useState } from "react"
import Image from "next/image"
import Link from "next/link"
import {
Loader2,
SearchIcon,
ExternalLinkIcon,
TrashIcon,
Gamepad2Icon,
} from "lucide-react"
interface Game {
id: string
steamAppId: number | null
title: string
description: string | null
developer: string | null
publisher: string | null
genres: string[] | null
headerImage: string | null
capsuleImage: string | null
storeUrl: string | null
source: "steam" | "manual" | "gog" | "epic"
lastSync: string | null
syncStatus: string | null
createdAt: string
updatedAt: string
}
interface GamesApiResponse {
data: Game[]
total: number
limit: number
offset: number
}
const LIMIT = 50
export function GamesClient() {
const [games, setGames] = useState<Game[]>([])
const [total, setTotal] = useState(0)
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState("")
const [offset, setOffset] = useState(0)
const [deletingIds, setDeletingIds] = useState<Set<string>>(new Set())
const isSearchChangeRef = useRef(false)
const handleSearchChange = (value: string) => {
setSearch(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handlePrev = () => {
setOffset((prev) => Math.max(0, prev - LIMIT))
isSearchChangeRef.current = false
}
const handleNext = () => {
setOffset((prev) => prev + LIMIT)
isSearchChangeRef.current = false
}
useEffect(() => {
const delay = isSearchChangeRef.current ? 300 : 0
isSearchChangeRef.current = false
let cancelled = false
const timer = setTimeout(async () => {
setLoading(true)
try {
const res = await fetch(
`/api/games?limit=${LIMIT}&offset=${offset}&search=${encodeURIComponent(search)}`
)
if (res.ok && !cancelled) {
const json = (await res.json()) as GamesApiResponse
setGames(json.data)
setTotal(json.total)
}
} catch {
// ignore
} finally {
if (!cancelled) setLoading(false)
}
}, delay)
return () => {
cancelled = true
clearTimeout(timer)
}
}, [search, offset])
const handleDelete = async (game: Game) => {
if (!confirm(`Are you sure you want to delete "${game.title}"?`)) return
setDeletingIds((prev) => new Set(prev).add(game.id))
try {
const res = await fetch(`/api/games/${game.id}`, { method: "DELETE" })
if (res.ok) {
setGames((prev) => prev.filter((g) => g.id !== game.id))
setTotal((prev) => Math.max(0, prev - 1))
}
} finally {
setDeletingIds((prev) => {
const next = new Set(prev)
next.delete(game.id)
return next
})
}
}
const hasMore = offset + games.length < total
return (
<div className="space-y-4">
{/* Search */}
<div className="relative">
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
<input
type="text"
value={search}
onChange={(e) => handleSearchChange(e.target.value)}
placeholder="Search games..."
className="w-full pl-9 pr-4 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
{/* Table */}
<div className="rounded-xl border border-border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-text/[0.03]">
<tr>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50 w-14">
Cover
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Title
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Developer
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Source
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Sync
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Actions
</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan={6} className="px-4 py-8 text-center text-text/50">
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
</td>
</tr>
) : games.length === 0 ? (
<tr>
<td colSpan={6} className="px-4 py-8 text-center text-text/50">
No games found.
</td>
</tr>
) : (
games.map((game) => (
<tr
key={game.id}
className="border-t border-border hover:bg-text/[0.02] transition-colors"
>
<td className="px-4 py-3">
<div className="h-10 w-10 rounded overflow-hidden bg-text/5 flex items-center justify-center">
{game.capsuleImage || game.headerImage ? (
<Image
src={game.capsuleImage || game.headerImage || ""}
alt={game.title}
width={40}
height={40}
className="h-10 w-10 object-cover"
unoptimized
/>
) : (
<Gamepad2Icon className="h-4 w-4 text-text/40" />
)}
</div>
</td>
<td className="px-4 py-3">
<p className="font-medium text-text truncate max-w-[200px]">
{game.title}
</p>
</td>
<td className="px-4 py-3">
<p className="text-text/50 truncate max-w-[150px]">
{game.developer || "—"}
</p>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${
game.source === "steam"
? "bg-blue-500/10 text-blue-400"
: game.source === "manual"
? "bg-text/5 text-text/50"
: "bg-text/5 text-text/50"
}`}
>
{game.source}
</span>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${
game.syncStatus === "synced"
? "bg-green-500/10 text-green-400"
: "bg-yellow-500/10 text-yellow-400"
}`}
>
<span
className={`h-1.5 w-1.5 rounded-full ${
game.syncStatus === "synced"
? "bg-green-400"
: "bg-yellow-400"
}`}
/>
{game.syncStatus === "synced" ? "Synced" : "Stale"}
</span>
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2">
<Link
href={`/game/${game.id}`}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors"
>
<ExternalLinkIcon className="h-3.5 w-3.5" />
View
</Link>
<button
onClick={() => handleDelete(game)}
disabled={deletingIds.has(game.id)}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{deletingIds.has(game.id) ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<TrashIcon className="h-3.5 w-3.5" />
)}
Delete
</button>
</div>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
{games.length > 0 && (
<div className="flex items-center justify-between">
<p className="text-xs text-text/50">
Showing {offset + 1}{Math.min(offset + games.length, total)} of{" "}
{total}
</p>
<div className="flex items-center gap-2">
<button
onClick={handlePrev}
disabled={offset === 0 || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
>
Previous
</button>
<button
onClick={handleNext}
disabled={!hasMore || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
>
Next
</button>
</div>
</div>
)}
</div>
)
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { GamesClient } from "./games-client"
export const metadata: Metadata = {
title: "Games",
}
export default function GamesPage() {
return <GamesClient />
}
@@ -0,0 +1,355 @@
"use client"
import { useEffect, useState, FormEvent } from "react"
import {
Gamepad2Icon,
PlusIcon,
PencilIcon,
TrashIcon,
Loader2,
XIcon,
} from "lucide-react"
import { getDeviceColor } from "@/components/charts/EChartWrapper"
interface DeviceItem {
slug: string
name: string
deviceType: string
image: string | null
sortOrder: number
totalBenchmarks?: number
avgFps?: number | null
gameCount?: number
}
interface DeviceFormData {
slug: string
name: string
deviceType: "handheld" | "console"
image: string
sortOrder: number
}
export function HardwareClient() {
const [devices, setDevices] = useState<DeviceItem[]>([])
const [loading, setLoading] = useState(true)
const [showModal, setShowModal] = useState(false)
const [editingDevice, setEditingDevice] = useState<DeviceItem | null>(null)
const [form, setForm] = useState<DeviceFormData>({
slug: "",
name: "",
deviceType: "handheld",
image: "",
sortOrder: 0,
})
const [saving, setSaving] = useState(false)
const [deleting, setDeleting] = useState(false)
const fetchDevices = async () => {
setLoading(true)
try {
const res = await fetch("/api/hardware/stats")
if (res.ok) {
const data = await res.json()
setDevices(
(data as DeviceItem[]).map((item) => ({
...item,
image: item.image ?? null,
}))
)
}
} catch {
// ignore
} finally {
setLoading(false)
}
}
useEffect(() => {
// eslint-disable-next-line react-hooks/set-state-in-effect
fetchDevices()
}, [])
const openCreate = () => {
setEditingDevice(null)
setForm({
slug: "",
name: "",
deviceType: "handheld",
image: "",
sortOrder: 0,
})
setShowModal(true)
}
const openEdit = (device: DeviceItem) => {
setEditingDevice(device)
setForm({
slug: device.slug,
name: device.name,
deviceType: device.deviceType as "handheld" | "console",
image: device.image ?? "",
sortOrder: device.sortOrder,
})
setShowModal(true)
}
const closeModal = () => {
setShowModal(false)
setEditingDevice(null)
}
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
setSaving(true)
try {
const body = editingDevice
? {
name: form.name,
deviceType: form.deviceType,
image: form.image || null,
sortOrder: form.sortOrder,
}
: {
slug: form.slug,
name: form.name,
deviceType: form.deviceType,
image: form.image || null,
sortOrder: form.sortOrder,
}
const url = editingDevice
? `/api/hardware/${editingDevice.slug}`
: "/api/hardware"
const method = editingDevice ? "PATCH" : "POST"
const res = await fetch(url, {
method,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
if (res.ok) {
closeModal()
await fetchDevices()
}
} finally {
setSaving(false)
}
}
const handleDelete = async (slug: string) => {
if (!confirm("Are you sure you want to delete this device?")) return
setDeleting(true)
try {
const res = await fetch(`/api/hardware/${slug}`, { method: "DELETE" })
if (res.ok) {
await fetchDevices()
}
} finally {
setDeleting(false)
}
}
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<h1 className="text-xl font-semibold text-text">Hardware</h1>
<button
onClick={openCreate}
className="flex items-center gap-2 px-3 py-2 rounded-md text-sm font-medium bg-primary/10 text-primary hover:bg-primary/20 transition-colors cursor-pointer"
>
<PlusIcon className="h-4 w-4" />
Add Device
</button>
</div>
{loading ? (
<div className="flex items-center justify-center py-16 text-text/50">
<Loader2 className="h-6 w-6 animate-spin" />
</div>
) : devices.length === 0 ? (
<div className="text-center py-16 text-text/40">
<Gamepad2Icon className="h-10 w-10 mx-auto mb-2" />
<p>No devices found</p>
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{devices.map((device, index) => {
const color = getDeviceColor(index)
return (
<div
key={device.slug}
className="rounded-xl border border-border bg-text/[0.02] p-4 flex flex-col gap-3 hover:bg-text/[0.04] transition-colors"
>
<div className="flex items-start justify-between">
<div className="flex items-center gap-3">
<div
className="h-10 w-10 rounded-lg flex items-center justify-center"
style={{ backgroundColor: `${color}20`, color }}
>
<Gamepad2Icon className="h-5 w-5" />
</div>
<div>
<h3 className="font-medium text-text text-sm">
{device.name}
</h3>
<p className="text-xs text-text/50">/{device.slug}</p>
</div>
</div>
<span
className={`text-[10px] uppercase tracking-wider px-2 py-0.5 rounded-full border ${
device.deviceType === "handheld"
? "text-primary bg-primary/10 border-primary/20"
: "text-secondary bg-secondary/10 border-secondary/20"
}`}
>
{device.deviceType}
</span>
</div>
<div className="text-xs text-text/60">
{device.totalBenchmarks ?? 0} benchmarks
</div>
<div className="flex items-center gap-2 mt-auto">
<button
onClick={() => openEdit(device)}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
<PencilIcon className="h-3.5 w-3.5" />
Edit
</button>
<button
onClick={() => handleDelete(device.slug)}
disabled={deleting}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
<TrashIcon className="h-3.5 w-3.5" />
Delete
</button>
</div>
</div>
)
})}
</div>
)}
{showModal && (
<div className="fixed inset-0 z-50 flex items-center justify-center">
<div
className="absolute inset-0 bg-black/50"
onClick={closeModal}
/>
<div className="relative bg-background border border-border rounded-xl p-6 max-w-md w-full mx-4">
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-medium text-text">
{editingDevice ? "Edit Device" : "Add Device"}
</h2>
<button
onClick={closeModal}
className="p-1 rounded-md hover:bg-text/5 text-text/60 hover:text-text transition-colors cursor-pointer"
>
<XIcon className="h-4 w-4" />
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-xs font-medium text-text/70 mb-1">
Slug
</label>
<input
type="text"
value={form.slug}
onChange={(e) =>
setForm((f) => ({ ...f, slug: e.target.value }))
}
disabled={!!editingDevice}
required
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
/>
</div>
<div>
<label className="block text-xs font-medium text-text/70 mb-1">
Name
</label>
<input
type="text"
value={form.name}
onChange={(e) =>
setForm((f) => ({ ...f, name: e.target.value }))
}
required
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
<div>
<label className="block text-xs font-medium text-text/70 mb-1">
Device Type
</label>
<select
value={form.deviceType}
onChange={(e) =>
setForm((f) => ({
...f,
deviceType: e.target.value as "handheld" | "console",
}))
}
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text focus:outline-none focus:border-primary/60 transition-colors"
>
<option value="handheld">Handheld</option>
<option value="console">Console</option>
</select>
</div>
<div>
<label className="block text-xs font-medium text-text/70 mb-1">
Sort Order
</label>
<input
type="number"
value={form.sortOrder}
onChange={(e) =>
setForm((f) => ({
...f,
sortOrder: Number(e.target.value),
}))
}
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
<div>
<label className="block text-xs font-medium text-text/70 mb-1">
Image URL
</label>
<input
type="text"
value={form.image}
onChange={(e) =>
setForm((f) => ({ ...f, image: e.target.value }))
}
placeholder="https://..."
className="w-full px-3 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
<div className="flex items-center justify-end gap-2 pt-2">
<button
type="button"
onClick={closeModal}
className="px-3 py-2 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
type="submit"
disabled={saving}
className="flex items-center gap-1.5 px-3 py-2 rounded-md text-xs font-medium bg-primary text-white hover:bg-primary/90 transition-colors cursor-pointer disabled:opacity-50"
>
{saving && <Loader2 className="h-3.5 w-3.5 animate-spin" />}
{editingDevice ? "Save Changes" : "Create Device"}
</button>
</div>
</form>
</div>
</div>
)}
</div>
)
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { HardwareClient } from "./hardware-client"
export const metadata: Metadata = {
title: "Hardware",
}
export default function HardwarePage() {
return <HardwareClient />
}
+5
View File
@@ -0,0 +1,5 @@
import { redirect } from "next/navigation"
export default function AdminPage() {
redirect("/admin/users")
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { ReportsClient } from "./reports-client"
export const metadata: Metadata = {
title: "Reports",
}
export default function ReportsPage() {
return <ReportsClient />
}
@@ -0,0 +1,477 @@
"use client"
import { useEffect, useMemo, useRef, useState } from "react"
import Image from "next/image"
import {
Loader2,
SearchIcon,
ShieldCheckIcon,
XIcon,
ChevronLeftIcon,
ChevronRightIcon,
FlagIcon,
} from "lucide-react"
interface Report {
id: string
entryId: string
reporterId: string
reporterName: string | null
reason: "inaccurate" | "spam" | "inappropriate" | "other"
details: string | null
status: "open" | "reviewed" | "dismissed"
createdAt: string
entry: {
id: string
userId: string
fpsAvg: number | null
fpsLow: number | null
fpsHigh: number | null
upscalerType: string | null
userNotes: string | null
isRemoved: boolean
authorName: string | null
}
gameVersion: {
id: string
versionString: string
}
game: {
id: string
title: string
headerImage: string | null
}
}
interface ReportsApiResponse {
data: Report[]
total: number
limit: number
offset: number
}
type StatusFilter = "all" | "open" | "reviewed" | "dismissed"
const LIMIT = 20
function formatDate(value: string | Date | null | undefined) {
if (!value) return "—"
const d = typeof value === "string" ? new Date(value) : value
return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })
}
function getInitial(name: string | null | undefined) {
return name?.charAt(0)?.toUpperCase() || "?"
}
function truncate(str: string | null | undefined, maxLen = 60) {
if (!str) return "—"
return str.length > maxLen ? str.slice(0, maxLen) + "…" : str
}
function statusBadgeClasses(status: Report["status"]) {
switch (status) {
case "open":
return "bg-yellow-500/10 text-yellow-400"
case "reviewed":
return "bg-green-500/10 text-green-400"
case "dismissed":
return "bg-text/5 text-text/50"
}
}
function reasonBadgeClasses(reason: Report["reason"]) {
switch (reason) {
case "inaccurate":
return "bg-blue-500/10 text-blue-400"
case "spam":
return "bg-red-500/10 text-red-400"
case "inappropriate":
return "bg-orange-500/10 text-orange-400"
case "other":
return "bg-text/5 text-text/50"
}
}
export function ReportsClient() {
const [reports, setReports] = useState<Report[]>([])
const [total, setTotal] = useState(0)
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState("")
const [offset, setOffset] = useState(0)
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
const [actionLoading, setActionLoading] = useState<Record<string, boolean>>({})
const [confirmReport, setConfirmReport] = useState<Report | null>(null)
const isSearchChangeRef = useRef(false)
const handleSearchChange = (value: string) => {
setSearch(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handleStatusChange = (value: StatusFilter) => {
setStatusFilter(value)
setOffset(0)
isSearchChangeRef.current = true
}
const handlePrev = () => {
setOffset((prev) => Math.max(0, prev - LIMIT))
isSearchChangeRef.current = false
}
const handleNext = () => {
setOffset((prev) => prev + LIMIT)
isSearchChangeRef.current = false
}
useEffect(() => {
const delay = isSearchChangeRef.current ? 300 : 0
isSearchChangeRef.current = false
let cancelled = false
const timer = setTimeout(async () => {
setLoading(true)
try {
const params = new URLSearchParams({
limit: String(LIMIT),
offset: String(offset),
})
if (statusFilter !== "all") {
params.set("status", statusFilter)
}
const res = await fetch(`/api/admin/reports?${params.toString()}`)
if (res.ok && !cancelled) {
const json = (await res.json()) as ReportsApiResponse
setReports(json.data)
setTotal(json.total)
}
} catch {
// ignore
} finally {
if (!cancelled) setLoading(false)
}
}, delay)
return () => {
cancelled = true
clearTimeout(timer)
}
}, [search, offset, statusFilter])
const handleUpdateStatus = async (report: Report, status: "reviewed" | "dismissed") => {
setActionLoading((prev) => ({ ...prev, [report.id]: true }))
try {
const res = await fetch(`/api/admin/reports/${report.id}/status`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status }),
})
if (res.ok) {
// Refresh list after action
const params = new URLSearchParams({
limit: String(LIMIT),
offset: String(offset),
})
if (statusFilter !== "all") {
params.set("status", statusFilter)
}
const listRes = await fetch(`/api/admin/reports?${params.toString()}`)
if (listRes.ok) {
const json = (await listRes.json()) as ReportsApiResponse
setReports(json.data)
setTotal(json.total)
}
setConfirmReport(null)
}
} finally {
setActionLoading((prev) => ({ ...prev, [report.id]: false }))
}
}
const filteredReports = useMemo(() => {
const term = search.trim().toLowerCase()
if (!term) return reports
return reports.filter(
(r) =>
r.reporterName?.toLowerCase().includes(term) ||
r.game.title.toLowerCase().includes(term) ||
r.reason.toLowerCase().includes(term) ||
r.details?.toLowerCase().includes(term) ||
r.status.toLowerCase().includes(term)
)
}, [reports, search])
const hasMore = offset + filteredReports.length < total
const tabs: { label: string; value: StatusFilter }[] = [
{ label: "All", value: "all" },
{ label: "Open", value: "open" },
{ label: "Reviewed", value: "reviewed" },
{ label: "Dismissed", value: "dismissed" },
]
return (
<div className="space-y-4">
{/* Header */}
<div className="flex items-center gap-3">
<FlagIcon className="h-5 w-5 text-text/70" />
<h1 className="text-lg font-semibold text-text">Reports</h1>
</div>
{/* Search */}
<div className="relative">
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
<input
type="text"
value={search}
onChange={(e) => handleSearchChange(e.target.value)}
placeholder="Search reports..."
className="w-full pl-9 pr-4 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
{/* Filter tabs */}
<div className="flex items-center gap-1">
{tabs.map((tab) => (
<button
key={tab.value}
onClick={() => handleStatusChange(tab.value)}
className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors cursor-pointer ${
statusFilter === tab.value
? "bg-primary/10 text-primary"
: "bg-text/5 text-text/70 hover:bg-text/10"
}`}
>
{tab.label}
</button>
))}
</div>
{/* Table */}
<div className="rounded-xl border border-border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-text/[0.03]">
<tr>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Reporter
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Game
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
FPS
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Reason
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Details
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Status
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Date
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Actions
</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan={8} className="px-4 py-8 text-center text-text/50">
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
</td>
</tr>
) : filteredReports.length === 0 ? (
<tr>
<td colSpan={8} className="px-4 py-8 text-center text-text/50">
No reports found.
</td>
</tr>
) : (
filteredReports.map((report) => (
<tr
key={report.id}
className="border-t border-border hover:bg-text/[0.02] transition-colors"
>
<td className="px-4 py-3">
<div className="flex items-center gap-3">
<div className="h-8 w-8 rounded-full bg-text/10 flex items-center justify-center text-xs font-medium text-text/70">
{getInitial(report.reporterName)}
</div>
<div className="min-w-0">
<p className="font-medium text-text truncate">
{report.reporterName || "Unknown"}
</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2">
{report.game.headerImage ? (
<Image
src={report.game.headerImage}
alt={report.game.title}
width={40}
height={20}
className="h-5 w-10 object-cover rounded"
unoptimized
/>
) : null}
<p className="font-medium text-text truncate max-w-[150px]">
{report.game.title}
</p>
</div>
</td>
<td className="px-4 py-3">
<span className="text-text/70">
{report.entry.fpsAvg ?? "—"}
</span>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${reasonBadgeClasses(report.reason)}`}
>
{report.reason}
</span>
</td>
<td className="px-4 py-3">
<p className="text-text/50 truncate max-w-[200px]">
{truncate(report.details, 80)}
</p>
</td>
<td className="px-4 py-3">
<span
className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${statusBadgeClasses(report.status)}`}
>
<span
className={`h-1.5 w-1.5 rounded-full ${
report.status === "open"
? "bg-yellow-400"
: report.status === "reviewed"
? "bg-green-400"
: "bg-text/40"
}`}
/>
{report.status}
</span>
</td>
<td className="px-4 py-3 text-xs text-text/50">
{formatDate(report.createdAt)}
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2">
{report.status === "open" ? (
<>
<button
onClick={() => setConfirmReport(report)}
disabled={actionLoading[report.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[report.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<ShieldCheckIcon className="h-3.5 w-3.5" />
)}
Review
</button>
<button
onClick={() => handleUpdateStatus(report, "dismissed")}
disabled={actionLoading[report.id]}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text/70 hover:bg-text/10 transition-colors cursor-pointer disabled:opacity-50"
>
{actionLoading[report.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<XIcon className="h-3.5 w-3.5" />
)}
Dismiss
</button>
</>
) : (
<span
className={`inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider ${statusBadgeClasses(report.status)}`}
>
{report.status}
</span>
)}
</div>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
{filteredReports.length > 0 && (
<div className="flex items-center justify-between">
<p className="text-xs text-text/50">
Showing {offset + 1}{Math.min(offset + filteredReports.length, total)} of{" "}
{total}
</p>
<div className="flex items-center gap-2">
<button
onClick={handlePrev}
disabled={offset === 0 || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
<ChevronLeftIcon className="h-3.5 w-3.5" />
Previous
</button>
<button
onClick={handleNext}
disabled={!hasMore || loading}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer flex items-center gap-1"
>
Next
<ChevronRightIcon className="h-3.5 w-3.5" />
</button>
</div>
</div>
)}
{/* Confirmation Dialog */}
{confirmReport && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div className="bg-background border border-border rounded-xl p-6 max-w-sm w-full mx-4 space-y-4">
<h2 className="text-base font-semibold text-text">Confirm Review</h2>
<p className="text-sm text-text/70">
This will also remove the reported benchmark. Are you sure?
</p>
<div className="flex items-center justify-end gap-2">
<button
onClick={() => setConfirmReport(null)}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-text/5 text-text hover:bg-text/10 transition-colors cursor-pointer"
>
Cancel
</button>
<button
onClick={() => handleUpdateStatus(confirmReport, "reviewed")}
disabled={actionLoading[confirmReport.id]}
className="px-3 py-1.5 rounded-md text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer disabled:opacity-50 flex items-center gap-1.5"
>
{actionLoading[confirmReport.id] ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<ShieldCheckIcon className="h-3.5 w-3.5" />
)}
Review
</button>
</div>
</div>
</div>
)}
</div>
)
}
+10
View File
@@ -0,0 +1,10 @@
import type { Metadata } from "next"
import { UsersClient } from "./users-client"
export const metadata: Metadata = {
title: "Users",
}
export default function UsersPage() {
return <UsersClient />
}
+233
View File
@@ -0,0 +1,233 @@
"use client"
import { useEffect, useMemo, useState } from "react"
import Image from "next/image"
import { authClient } from "@/lib/auth-client"
import { Loader2, SearchIcon, BanIcon, UserCheckIcon } from "lucide-react"
type Role = "user" | "contributor" | "admin"
interface AdminUser {
id: string
name: string
email: string
emailVerified: boolean
image: string | null
createdAt: Date | string
role: Role
banned: boolean
}
const roles: Role[] = ["user", "contributor", "admin"]
function formatDate(value: Date | string | null | undefined) {
if (!value) return "—"
const d = typeof value === "string" ? new Date(value) : value
return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })
}
function getInitial(name: string) {
return name?.charAt(0)?.toUpperCase() || "?"
}
export function UsersClient() {
const [users, setUsers] = useState<AdminUser[]>([])
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState("")
const [actionLoading, setActionLoading] = useState<Record<string, boolean>>({})
useEffect(() => {
const fetchUsers = async () => {
setLoading(true)
const res = await authClient.admin.listUsers({ query: { limit: 100 } })
if (res.data?.users) {
setUsers(res.data.users as AdminUser[])
}
setLoading(false)
}
fetchUsers()
}, [])
const filteredUsers = useMemo(() => {
const term = search.trim().toLowerCase()
if (!term) return users
return users.filter(
(u) =>
u.name?.toLowerCase().includes(term) ||
u.email?.toLowerCase().includes(term)
)
}, [users, search])
const handleRoleChange = async (userId: string, newRole: Role) => {
setActionLoading((prev) => ({ ...prev, [userId]: true }))
try {
await authClient.admin.setRole({ userId, role: newRole as "user" | "admin" })
setUsers((prev) =>
prev.map((u) => (u.id === userId ? { ...u, role: newRole } : u))
)
} finally {
setActionLoading((prev) => ({ ...prev, [userId]: false }))
}
}
const handleBan = async (userId: string) => {
setActionLoading((prev) => ({ ...prev, [userId]: true }))
try {
await authClient.admin.banUser({ userId })
setUsers((prev) =>
prev.map((u) => (u.id === userId ? { ...u, banned: true } : u))
)
} finally {
setActionLoading((prev) => ({ ...prev, [userId]: false }))
}
}
const handleUnban = async (userId: string) => {
setActionLoading((prev) => ({ ...prev, [userId]: true }))
try {
await authClient.admin.unbanUser({ userId })
setUsers((prev) =>
prev.map((u) => (u.id === userId ? { ...u, banned: false } : u))
)
} finally {
setActionLoading((prev) => ({ ...prev, [userId]: false }))
}
}
return (
<div className="space-y-4">
{/* Search */}
<div className="relative">
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search by name or email..."
className="w-full pl-9 pr-4 py-2 rounded-md bg-text/5 border border-border text-sm text-text placeholder:text-text/40 focus:outline-none focus:border-primary/60 transition-colors"
/>
</div>
{/* Table */}
<div className="rounded-xl border border-border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-text/[0.03]">
<tr>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
User
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Role
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Status
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Joined
</th>
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
Actions
</th>
</tr>
</thead>
<tbody>
{loading ? (
<tr>
<td colSpan={5} className="px-4 py-8 text-center text-text/50">
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
</td>
</tr>
) : filteredUsers.length === 0 ? (
<tr>
<td colSpan={5} className="px-4 py-8 text-center text-text/50">
No users found.
</td>
</tr>
) : (
filteredUsers.map((user) => (
<tr
key={user.id}
className="border-t border-border hover:bg-text/[0.02] transition-colors"
>
<td className="px-4 py-3">
<div className="flex items-center gap-3">
{user.image ? (
<Image
src={user.image}
alt=""
width={32}
height={32}
className="h-8 w-8 rounded-full object-cover"
/>
) : (
<div className="h-8 w-8 rounded-full bg-text/10 flex items-center justify-center text-xs font-medium text-text/70">
{getInitial(user.name)}
</div>
)}
<div className="min-w-0">
<p className="font-medium text-text truncate">
{user.name || "Unnamed"}
</p>
<p className="text-xs text-text/50 truncate">{user.email}</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<select
value={user.role || "user"}
onChange={(e) =>
handleRoleChange(user.id, e.target.value as Role)
}
disabled={actionLoading[user.id]}
className="text-xs px-2 py-1 rounded-full border border-border bg-text/5 text-text focus:outline-none focus:border-primary/60 transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
>
{roles.map((r) => (
<option key={r} value={r}>
{r.charAt(0).toUpperCase() + r.slice(1)}
</option>
))}
</select>
</td>
<td className="px-4 py-3">
<div className="flex items-center gap-2">
<span
className={`h-2 w-2 rounded-full ${user.banned ? "bg-red-500" : "bg-green-500"}`}
/>
<span className="text-xs text-text/70">
{user.banned ? "Banned" : "Active"}
</span>
</div>
</td>
<td className="px-4 py-3 text-xs text-text/50">
{formatDate(user.createdAt)}
</td>
<td className="px-4 py-3">
{actionLoading[user.id] ? (
<Loader2 className="h-4 w-4 animate-spin text-text/50" />
) : user.banned ? (
<button
onClick={() => handleUnban(user.id)}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer"
>
<UserCheckIcon className="h-3.5 w-3.5" />
Unban
</button>
) : (
<button
onClick={() => handleBan(user.id)}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer"
>
<BanIcon className="h-3.5 w-3.5" />
Ban
</button>
)}
</td>
</tr>
))
)}
</tbody>
</table>
</div>
</div>
)
}