page.tsx228 lines, 8.9 KB
| 1 | import { UrlsTable } from "@/components/admin/urls/urls-table"; |
| 2 | import { Button } from "@/components/ui/button"; |
| 3 | import { |
| 4 | Card, |
| 5 | CardContent, |
| 6 | CardDescription, |
| 7 | CardHeader, |
| 8 | CardTitle, |
| 9 | } from "@/components/ui/card"; |
| 10 | import { getAllUrls } from "@/server/actions/admin/urls/get-all-urls"; |
| 11 | import { auth } from "@/server/auth"; |
| 12 | import { AlertTriangle, ArrowLeft, ShieldAlert } from "lucide-react"; |
| 13 | import { Metadata } from "next"; |
| 14 | import Link from "next/link"; |
| 15 | import { redirect } from "next/navigation"; |
| 16 | |
| 17 | export const metadata: Metadata = { |
| 18 | title: "Flagged URLs | Admin | ShortLink", |
| 19 | description: "Review potentially unsafe URLs in the ShortLink application", |
| 20 | }; |
| 21 | |
| 22 | export default async function FlaggedURLsPage({ |
| 23 | searchParams, |
| 24 | }: { |
| 25 | searchParams: Promise<{ |
| 26 | page?: string; |
| 27 | search?: string; |
| 28 | sortBy?: string; |
| 29 | sortOrder?: string; |
| 30 | }>; |
| 31 | }) { |
| 32 | const session = await auth(); |
| 33 | |
| 34 | if (!session?.user) { |
| 35 | redirect("/login"); |
| 36 | } |
| 37 | |
| 38 | if (session?.user.role !== "admin") { |
| 39 | redirect("/dashboard"); |
| 40 | } |
| 41 | |
| 42 | const params = await searchParams; |
| 43 | const page = params.page ? parseInt(params.page) : 1; |
| 44 | const search = params.search || ""; |
| 45 | const sortBy = (params.sortBy || "createdAt") as |
| 46 | | "originalUrl" |
| 47 | | "shortCode" |
| 48 | | "createdAt" |
| 49 | | "clicks" |
| 50 | | "userName"; |
| 51 | const sortOrder = (params.sortOrder || "desc") as "asc" | "desc"; |
| 52 | |
| 53 | const response = await getAllUrls({ |
| 54 | page, |
| 55 | limit: 10, |
| 56 | search, |
| 57 | sortBy, |
| 58 | sortOrder, |
| 59 | }); |
| 60 | |
| 61 | const urls = |
| 62 | response.success && response.data |
| 63 | ? response.data.urls.filter((url) => url.flagged) |
| 64 | : []; |
| 65 | |
| 66 | const total = urls.length; |
| 67 | |
| 68 | const categorizedUrls = urls.reduce((acc, url) => { |
| 69 | const reason = url.flagReason || "unknown"; |
| 70 | const category = reason.toLowerCase().includes("inappropriate") |
| 71 | ? "inappropriate" |
| 72 | : reason.toLowerCase().includes("security") || |
| 73 | reason.toLowerCase().includes("malware") || |
| 74 | reason.toLowerCase().includes("phishing") || |
| 75 | reason.toLowerCase().includes("suspicious") |
| 76 | ? "security" |
| 77 | : "other"; |
| 78 | |
| 79 | if (!acc[category]) { |
| 80 | acc[category] = []; |
| 81 | } |
| 82 | acc[category].push(url); |
| 83 | return acc; |
| 84 | }, {} as Record<string, typeof urls>); |
| 85 | |
| 86 | return ( |
| 87 | <> |
| 88 | <div className="flex items-center justify-between mb-6"> |
| 89 | <div className="flex items-center gap-3"> |
| 90 | <h1 className="text-3xl font-bold tracking-tight">Flagged URLs</h1> |
| 91 | <div className="bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-300 px-3 py-1 rounded-full text-sm flex items-center gap-1.5"> |
| 92 | <AlertTriangle className="size-4" /> |
| 93 | <span className="font-medium">{total} URLs</span> Requires Review |
| 94 | </div> |
| 95 | </div> |
| 96 | <Link href={"/admin/urls"} passHref> |
| 97 | <Button variant={"outline"} size={"sm"} className="gap-2"> |
| 98 | <ArrowLeft className="size-4" /> |
| 99 | Back to All URLs |
| 100 | </Button> |
| 101 | </Link> |
| 102 | </div> |
| 103 | |
| 104 | {total === 0 ? ( |
| 105 | <div> |
| 106 | <Card className="shadow-sm border-green-200 dark:border-green-800"> |
| 107 | <CardContent className="pt-6 pb-8 flex flex-col items-center justify-center"> |
| 108 | <div className="size-16 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center mb-4"> |
| 109 | <AlertTriangle className="size-8 text-green-500 dark:text-green-400" /> |
| 110 | </div> |
| 111 | <h3 className="text-xl font-medium mb-2">No Flagged URLs</h3> |
| 112 | <p className="text-muted-foreground text-center max-w-md"> |
| 113 | All flagged URLs have been reviewed and are safe to use. |
| 114 | </p> |
| 115 | </CardContent> |
| 116 | </Card> |
| 117 | </div> |
| 118 | ) : ( |
| 119 | <div className="grid gap-8"> |
| 120 | <Card className="shadow-sm border-yellow-200 dark:border-yellow-800"> |
| 121 | <CardHeader className="border-b px-3 py-1 border-yellow-100 dark:border-yellow-900/50 bg-yellow-50/50 dark:bg-yellow-900/20"> |
| 122 | <div className="flex items-center gap-2"> |
| 123 | <AlertTriangle className="size-5 text-yellow-600 dark:text-yellow-400" /> |
| 124 | <CardTitle className="text-yellow-800 dark:text-yellow-300"> |
| 125 | Flagged URLs |
| 126 | </CardTitle> |
| 127 | </div> |
| 128 | <CardDescription> |
| 129 | These URLs have been automatically flagged by our AI system as |
| 130 | potentially unsafe or inappropriate. Please review each URL |
| 131 | carefully before taking action. |
| 132 | </CardDescription> |
| 133 | </CardHeader> |
| 134 | <CardContent className="pt-6"> |
| 135 | {categorizedUrls.security && |
| 136 | categorizedUrls.security.length > 0 && ( |
| 137 | <div className="mb-8"> |
| 138 | <div className="flex items-center gap-2 mb-4"> |
| 139 | <ShieldAlert className="size-5 text-red-500 dark:text-red-400" /> |
| 140 | <h3 className="text-lg font-medium text-red-700 dark:text-red-400"> |
| 141 | Security Concerns |
| 142 | </h3> |
| 143 | </div> |
| 144 | <Card className="border-red-200 dark:border-red-900/50 bg-red-50/30 dark:bg-red-900/10 shadow-sm mb-6"> |
| 145 | <CardContent className="pt-4"> |
| 146 | <p className="text-sm text-red-700 dark:text-red-400 mb-2"> |
| 147 | These URLs have been flagged due to security concerns. |
| 148 | Please review each URL carefully before taking action. |
| 149 | </p> |
| 150 | </CardContent> |
| 151 | </Card> |
| 152 | <UrlsTable |
| 153 | urls={categorizedUrls.security} |
| 154 | total={categorizedUrls.security.length} |
| 155 | currentPage={page} |
| 156 | currentSearch={search} |
| 157 | currentSortBy={sortBy} |
| 158 | currentSortOrder={sortOrder} |
| 159 | highlightStyle="security" |
| 160 | /> |
| 161 | </div> |
| 162 | )} |
| 163 | |
| 164 | {categorizedUrls.inappropriate && |
| 165 | categorizedUrls.inappropriate.length > 0 && ( |
| 166 | <div className="mb-8"> |
| 167 | <div className="flex items-center gap-2 mb-4"> |
| 168 | <ShieldAlert className="size-5 text-red-500 dark:text-red-400" /> |
| 169 | <h3 className="text-lg font-medium text-red-700 dark:text-red-400"> |
| 170 | Security Concerns |
| 171 | </h3> |
| 172 | </div> |
| 173 | <Card className="border-red-200 dark:border-red-900/50 bg-red-50/30 dark:bg-red-900/10 shadow-sm mb-6"> |
| 174 | <CardContent className="pt-4"> |
| 175 | <p className="text-sm text-red-700 dark:text-red-400 mb-2"> |
| 176 | These URLs have been flagged due to inappropriate |
| 177 | content. Please review each URL carefully before |
| 178 | taking action. |
| 179 | </p> |
| 180 | </CardContent> |
| 181 | </Card> |
| 182 | <UrlsTable |
| 183 | urls={categorizedUrls.inappropriate} |
| 184 | total={categorizedUrls.inappropriate.length} |
| 185 | currentPage={page} |
| 186 | currentSearch={search} |
| 187 | currentSortBy={sortBy} |
| 188 | currentSortOrder={sortOrder} |
| 189 | highlightStyle="inappropriate" |
| 190 | /> |
| 191 | </div> |
| 192 | )} |
| 193 | |
| 194 | {categorizedUrls.other && categorizedUrls.other.length > 0 && ( |
| 195 | <div className="mb-8"> |
| 196 | <div className="flex items-center gap-2 mb-4"> |
| 197 | <AlertTriangle className="size-5 text-yellow-500 dark:text-yellow-400" /> |
| 198 | <h3 className="text-lg font-medium text-yellow-700 dark:text-yellow-400"> |
| 199 | Other Concerns |
| 200 | </h3> |
| 201 | </div> |
| 202 | <Card className="border-yellow-200 dark:border-yellow-900/50 bg-yellow-50/30 dark:bg-yellow-900/10 shadow-sm mb-6"> |
| 203 | <CardContent className="pt-4"> |
| 204 | <p className="text-sm text-yellow-700 dark:text-yellow-400 mb-2"> |
| 205 | These URLs have been flagged due to inappropriate |
| 206 | content. Please review each URL carefully before taking |
| 207 | action. |
| 208 | </p> |
| 209 | </CardContent> |
| 210 | </Card> |
| 211 | <UrlsTable |
| 212 | urls={categorizedUrls.other} |
| 213 | total={categorizedUrls.other.length} |
| 214 | currentPage={page} |
| 215 | currentSearch={search} |
| 216 | currentSortBy={sortBy} |
| 217 | currentSortOrder={sortOrder} |
| 218 | highlightStyle="other" |
| 219 | /> |
| 220 | </div> |
| 221 | )} |
| 222 | </CardContent> |
| 223 | </Card> |
| 224 | </div> |
| 225 | )} |
| 226 | </> |
| 227 | ); |
| 228 | } |