page.tsx103 lines, 2.8 KB
| 1 | import { UserSearch } from "@/components/admin/users/user-search"; |
| 2 | import { UsersTable } from "@/components/admin/users/users-table"; |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { |
| 5 | Card, |
| 6 | CardContent, |
| 7 | CardDescription, |
| 8 | CardHeader, |
| 9 | CardTitle, |
| 10 | } from "@/components/ui/card"; |
| 11 | import { getAllUsers } from "@/server/actions/admin/users/get-all-users"; |
| 12 | import { auth } from "@/server/auth"; |
| 13 | import { ArrowLeft } from "lucide-react"; |
| 14 | import { Metadata } from "next"; |
| 15 | import Link from "next/link"; |
| 16 | import { redirect } from "next/navigation"; |
| 17 | |
| 18 | export const metadata: Metadata = { |
| 19 | title: "User Management | Admin | ShortLink", |
| 20 | description: "Manage users in the ShortLink application", |
| 21 | }; |
| 22 | |
| 23 | export default async function UserManagementPage({ |
| 24 | searchParams, |
| 25 | }: { |
| 26 | searchParams: Promise<{ |
| 27 | page?: string; |
| 28 | search?: string; |
| 29 | sortBy?: string; |
| 30 | sortOrder?: string; |
| 31 | }>; |
| 32 | }) { |
| 33 | const session = await auth(); |
| 34 | |
| 35 | // Redirect to login if not authenticated |
| 36 | if (!session?.user) { |
| 37 | redirect("/login"); |
| 38 | } |
| 39 | |
| 40 | // Redirect to dashboard if not an admin |
| 41 | if (session.user.role !== "admin") { |
| 42 | redirect("/dashboard"); |
| 43 | } |
| 44 | |
| 45 | // Parse search params |
| 46 | const params = await searchParams; |
| 47 | const page = params.page ? parseInt(params.page) : 1; |
| 48 | const search = params.search || ""; |
| 49 | const sortBy = (params.sortBy || "createdAt") as |
| 50 | | "name" |
| 51 | | "email" |
| 52 | | "role" |
| 53 | | "createdAt"; |
| 54 | const sortOrder = (params.sortOrder || "desc") as "asc" | "desc"; |
| 55 | |
| 56 | const response = await getAllUsers({ |
| 57 | page, |
| 58 | search, |
| 59 | sortBy, |
| 60 | sortOrder, |
| 61 | }); |
| 62 | |
| 63 | const users = response.success && response.data ? response.data.users : []; |
| 64 | const total = response.success && response.data ? response.data.total : 0; |
| 65 | |
| 66 | return ( |
| 67 | <> |
| 68 | <div className="flex items-center justify-between mb-6"> |
| 69 | <h1 className="text-3xl font-bold tracking-tight">User Management</h1> |
| 70 | <Link href="/admin" passHref> |
| 71 | <Button variant={"outline"} size={"sm"} className="gap-2"> |
| 72 | <ArrowLeft className="size-4" /> |
| 73 | Back to Admin |
| 74 | </Button> |
| 75 | </Link> |
| 76 | </div> |
| 77 | |
| 78 | <div className="grid gap-6"> |
| 79 | <Card className="shadow-sm"> |
| 80 | <CardHeader> |
| 81 | <CardTitle>Users</CardTitle> |
| 82 | <CardDescription> |
| 83 | View and manage all users in the system |
| 84 | </CardDescription> |
| 85 | </CardHeader> |
| 86 | <CardContent> |
| 87 | <div className="space-y-4"> |
| 88 | <UserSearch initialSearch={search} /> |
| 89 | <UsersTable |
| 90 | users={users} |
| 91 | total={total} |
| 92 | currentPage={page} |
| 93 | currentSearch={search} |
| 94 | currentSortBy={sortBy} |
| 95 | currentSortOrder={sortOrder} |
| 96 | /> |
| 97 | </div> |
| 98 | </CardContent> |
| 99 | </Card> |
| 100 | </div> |
| 101 | </> |
| 102 | ); |
| 103 | } |