feat: improve users management UI with card-based layout
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
import { useEffect, useMemo, useState } from "react"
|
import { useEffect, useMemo, useState } from "react"
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { authClient } from "@/lib/auth-client"
|
import { authClient } from "@/lib/auth-client"
|
||||||
import { Loader2, SearchIcon, BanIcon, UserCheckIcon } from "lucide-react"
|
import { Loader2, SearchIcon, BanIcon, UserCheckIcon, ShieldIcon, UsersIcon } from "lucide-react"
|
||||||
|
|
||||||
type Role = "user" | "contributor" | "admin"
|
type Role = "user" | "contributor" | "admin"
|
||||||
|
|
||||||
@@ -30,6 +30,21 @@ function getInitial(name: string) {
|
|||||||
return name?.charAt(0)?.toUpperCase() || "?"
|
return name?.charAt(0)?.toUpperCase() || "?"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function RoleBadge({ role }: { role: Role }) {
|
||||||
|
const config = {
|
||||||
|
admin: { bg: "bg-red-500/10", text: "text-red-400", icon: ShieldIcon },
|
||||||
|
contributor: { bg: "bg-blue-500/10", text: "text-blue-400", icon: UserCheckIcon },
|
||||||
|
user: { bg: "bg-text/5", text: "text-text/50", icon: UsersIcon },
|
||||||
|
}[role]
|
||||||
|
const Icon = config.icon
|
||||||
|
return (
|
||||||
|
<span className={`inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-xs font-medium ${config.bg} ${config.text}`}>
|
||||||
|
<Icon className="h-3 w-3" />
|
||||||
|
{role.charAt(0).toUpperCase() + role.slice(1)}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export function UsersClient() {
|
export function UsersClient() {
|
||||||
const [users, setUsers] = useState<AdminUser[]>([])
|
const [users, setUsers] = useState<AdminUser[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
@@ -96,6 +111,15 @@ export function UsersClient() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
|
{/* Header with count */}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<UsersIcon className="h-5 w-5 text-text/50" />
|
||||||
|
<h2 className="text-lg font-semibold">Users</h2>
|
||||||
|
<span className="text-sm text-text/50">({filteredUsers.length})</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Search */}
|
{/* Search */}
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
|
<SearchIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-text/40" />
|
||||||
@@ -104,130 +128,94 @@ export function UsersClient() {
|
|||||||
value={search}
|
value={search}
|
||||||
onChange={(e) => setSearch(e.target.value)}
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
placeholder="Search by name or email..."
|
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"
|
className="w-full pl-9 pr-4 py-2.5 rounded-lg 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>
|
||||||
|
|
||||||
{/* Table */}
|
{/* Users List */}
|
||||||
<div className="rounded-xl border border-border overflow-hidden">
|
{loading ? (
|
||||||
<table className="w-full text-sm">
|
<div className="flex items-center justify-center py-12">
|
||||||
<thead className="bg-text/[0.03]">
|
<Loader2 className="h-6 w-6 animate-spin text-primary" />
|
||||||
<tr>
|
</div>
|
||||||
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
|
) : filteredUsers.length === 0 ? (
|
||||||
User
|
<div className="flex flex-col items-center justify-center py-12 text-text/50">
|
||||||
</th>
|
<UsersIcon className="h-8 w-8 mb-2" />
|
||||||
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
|
<p className="text-sm">No users found</p>
|
||||||
Role
|
</div>
|
||||||
</th>
|
) : (
|
||||||
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
|
<div className="space-y-2">
|
||||||
Status
|
{filteredUsers.map((user) => (
|
||||||
</th>
|
<div
|
||||||
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
|
key={user.id}
|
||||||
Joined
|
className="flex items-center gap-4 p-4 rounded-xl border border-border bg-text/[0.02] hover:bg-text/[0.04] transition-colors"
|
||||||
</th>
|
>
|
||||||
<th className="text-left px-4 py-3 text-xs font-medium uppercase tracking-wider text-text/50">
|
{/* Avatar */}
|
||||||
Actions
|
{user.image ? (
|
||||||
</th>
|
<Image
|
||||||
</tr>
|
src={user.image}
|
||||||
</thead>
|
alt=""
|
||||||
<tbody>
|
width={40}
|
||||||
{loading ? (
|
height={40}
|
||||||
<tr>
|
className="h-10 w-10 rounded-full object-cover"
|
||||||
<td colSpan={5} className="px-4 py-8 text-center text-text/50">
|
/>
|
||||||
<Loader2 className="h-5 w-5 animate-spin mx-auto" />
|
) : (
|
||||||
</td>
|
<div className="h-10 w-10 rounded-full bg-text/10 flex items-center justify-center text-sm font-medium text-text/70">
|
||||||
</tr>
|
{getInitial(user.name)}
|
||||||
) : filteredUsers.length === 0 ? (
|
</div>
|
||||||
<tr>
|
)}
|
||||||
<td colSpan={5} className="px-4 py-8 text-center text-text/50">
|
|
||||||
No users found.
|
{/* User Info */}
|
||||||
</td>
|
<div className="flex-1 min-w-0">
|
||||||
</tr>
|
<div className="flex items-center gap-2">
|
||||||
) : (
|
<p className="font-medium text-text truncate">{user.name || "Unnamed"}</p>
|
||||||
filteredUsers.map((user) => (
|
{user.banned && (
|
||||||
<tr
|
<span className="text-[10px] px-1.5 py-0.5 rounded bg-red-500/10 text-red-400">
|
||||||
key={user.id}
|
Banned
|
||||||
className="border-t border-border hover:bg-text/[0.02] transition-colors"
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-text/50 truncate">{user.email}</p>
|
||||||
|
<p className="text-[11px] text-text/30 mt-0.5">Joined {formatDate(user.createdAt)}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Role Selector */}
|
||||||
|
<select
|
||||||
|
value={user.role || "user"}
|
||||||
|
onChange={(e) => handleRoleChange(user.id, e.target.value as Role)}
|
||||||
|
disabled={actionLoading[user.id]}
|
||||||
|
className="text-xs px-3 py-1.5 rounded-lg border border-border bg-text/5 text-text focus:outline-none focus:border-primary/60 transition-colors cursor-pointer disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{roles.map((r) => (
|
||||||
|
<option key={r} value={r}>
|
||||||
|
{r.charAt(0).toUpperCase() + r.slice(1)}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
{/* Action Button */}
|
||||||
|
{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-3 py-1.5 rounded-lg text-xs font-medium bg-green-500/10 text-green-400 hover:bg-green-500/20 transition-colors cursor-pointer"
|
||||||
>
|
>
|
||||||
<td className="px-4 py-3">
|
<UserCheckIcon className="h-3.5 w-3.5" />
|
||||||
<div className="flex items-center gap-3">
|
Unban
|
||||||
{user.image ? (
|
</button>
|
||||||
<Image
|
) : (
|
||||||
src={user.image}
|
<button
|
||||||
alt=""
|
onClick={() => handleBan(user.id)}
|
||||||
width={32}
|
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors cursor-pointer"
|
||||||
height={32}
|
>
|
||||||
className="h-8 w-8 rounded-full object-cover"
|
<BanIcon className="h-3.5 w-3.5" />
|
||||||
/>
|
Ban
|
||||||
) : (
|
</button>
|
||||||
<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>
|
))}
|
||||||
)}
|
</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>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user