| 1 | /* eslint-disable @next/next/no-img-element */ |
| 2 | "use client"; |
| 3 | |
| 4 | import { useState, useRef |
| 5 | import { useRouter } |
| 6 | import { useQuery, useMutation |
| 7 | import { ChevronLeft, ChevronRight |
| 8 | import { useTranslations } |
| 9 | import { toast } |
| 10 | import { |
| 11 | Dialog, |
| 12 | DialogPopup, |
| 13 | DialogHeader, |
| 14 | DialogFooter, |
| 15 | DialogTitle, |
| 16 | DialogDescription, |
| 17 | DialogClose, |
| 18 | } from "@struxa/ui/components/dialog"; |
| 19 | import { orpc, queryClient |
| 20 | import { ContextMenu, RowMenu |
| 21 | import { DitherAvatar } |
| 22 | |
| 23 | function invalidateUsers() { |
| 24 | void queryClient.invalidateQueries( |
| 25 | } |
| 26 | |
| 27 | type DialogState |
| 28 | | null |
| 29 | | { type |
| 30 | | { type |
| 31 | |
| 32 | export default function AdminUsersPage |
| 33 | const t = |
| 34 | const tc = |
| 35 | const router = |
| 36 | const [page, |
| 37 | const [search, |
| 38 | const [debouncedSearch, |
| 39 | const [dialog, |
| 40 | const [banReason, |
| 41 | const debounceRef = |
| 42 | |
| 43 | function handleSearch(val |
| 44 | setSearch(val); |
| 45 | if (debounceRef.current |
| 46 | debounceRef.current = |
| 47 | setDebouncedSearch(val); |
| 48 | setPage(1); |
| 49 | }, 350); |
| 50 | } |
| 51 | |
| 52 | const { data, |
| 53 | orpc.users.list |
| 54 | ); |
| 55 | |
| 56 | const setRoleMutation = |
| 57 | onSuccess: () => |
| 58 | })); |
| 59 | const banMutation = |
| 60 | onSuccess: () => |
| 61 | })); |
| 62 | const unbanMutation = |
| 63 | onSuccess: () => |
| 64 | })); |
| 65 | const deleteMutation = |
| 66 | onSuccess: () => |
| 67 | })); |
| 68 | |
| 69 | const totalPages = |
| 70 | |
| 71 | function userActions(u |
| 72 | const displayName = |
| 73 | return [ |
| 74 | u.role !== |
| 75 | ? { label: t |
| 76 | : { label: t |
| 77 | u.banned |
| 78 | ? { label: t |
| 79 | : { label: t |
| 80 | "separator", |
| 81 | { label: tc( |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | return ( |
| 86 | <> |
| 87 | <Dialog open={ |
| 88 | <DialogPopup showCloseButton={ |
| 89 | <DialogHeader> |
| 90 | <DialogTitle>{t( |
| 91 | <DialogDescription>{t( |
| 92 | </DialogHeader> |
| 93 | <div className="px-5 py-4 |
| 94 | <label className="mb-1.5 block text-xs font-medium text-foreground |
| 95 | <input |
| 96 | autoFocus |
| 97 | className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 98 | placeholder={t(" |
| 99 | value={banReason} |
| 100 | onChange={(e) |
| 101 | onKeyDown={(e) |
| 102 | /> |
| 103 | </div> |
| 104 | <DialogFooter> |
| 105 | <DialogClose |
| 106 | className="rounded-lg px-4 py-1.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground" |
| 107 | disabled={banMutation.isPending |
| 108 | > |
| 109 | {tc("cancel") |
| 110 | </DialogClose> |
| 111 | <button |
| 112 | type="button" |
| 113 | disabled={banMutation.isPending |
| 114 | onClick={async () |
| 115 | if (dialog?.type |
| 116 | await banMutation.mutateAsync |
| 117 | setDialog(null); |
| 118 | setBanReason(""); |
| 119 | }} |
| 120 | className="rounded-lg bg-destructive px-4 py-1.5 text-xs font-medium text-white transition-opacity hover:opacity-90 disabled:opacity-40" |
| 121 | > |
| 122 | {banMutation.isPending ? |
| 123 | </button> |
| 124 | </DialogFooter> |
| 125 | </DialogPopup> |
| 126 | </Dialog> |
| 127 | |
| 128 | <Dialog open={ |
| 129 | <DialogPopup showCloseButton={ |
| 130 | <DialogHeader> |
| 131 | <DialogTitle>{t( |
| 132 | <DialogDescription>{t( |
| 133 | </DialogHeader> |
| 134 | <DialogFooter> |
| 135 | <DialogClose |
| 136 | className="rounded-lg px-4 py-1.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground" |
| 137 | disabled={deleteMutation.isPending |
| 138 | > |
| 139 | {tc("cancel") |
| 140 | </DialogClose> |
| 141 | <button |
| 142 | type="button" |
| 143 | disabled={deleteMutation.isPending |
| 144 | onClick={async () |
| 145 | if (dialog?.type |
| 146 | await deleteMutation.mutateAsync |
| 147 | setDialog(null); |
| 148 | }} |
| 149 | className="rounded-lg bg-destructive px-4 py-1.5 text-xs font-medium text-white transition-opacity hover:opacity-90 disabled:opacity-40" |
| 150 | > |
| 151 | {deleteMutation.isPending ? |
| 152 | </button> |
| 153 | </DialogFooter> |
| 154 | </DialogPopup> |
| 155 | </Dialog> |
| 156 | |
| 157 | <div className="flex-1 overflow-auto px-6 py-5 |
| 158 | <div className="mx-auto max-w-5xl |
| 159 | <div className="mb-5 flex items-center justify-between |
| 160 | <div className="flex items-center gap-2.5 |
| 161 | <h1 className="text-sm font-semibold text-foreground |
| 162 | </div> |
| 163 | <div className="relative flex items-center |
| 164 | <Search className=" |
| 165 | <input |
| 166 | className="rounded-lg border border-border bg-background py-1.5 pl-8 pr-3 text-sm text-foreground outline-none placeholder:text-muted-foreground focus:border-ring transition-colors" |
| 167 | placeholder={t(" |
| 168 | value={search} |
| 169 | onChange={(e) |
| 170 | /> |
| 171 | </div> |
| 172 | </div> |
| 173 | |
| 174 | {/* Table */} |
| 175 | <div className="overflow-x-auto |
| 176 | <div className="min-w-[560px] overflow-hidden rounded-xl border border-border bg-card |
| 177 | {/* Header */} |
| 178 | <div className="grid grid-cols-[1fr_1fr_100px_80px_120px_36px] border-b border-border bg-muted/40 px-4 py-2.5 |
| 179 | <span className="text-xs font-medium text-muted-foreground |
| 180 | <span className="text-xs font-medium text-muted-foreground |
| 181 | <span className="text-xs font-medium text-muted-foreground |
| 182 | <span className="text-xs font-medium text-muted-foreground |
| 183 | <span className="text-xs font-medium text-muted-foreground |
| 184 | <span /> |
| 185 | </div> |
| 186 | |
| 187 | {isLoading && ( |
| 188 | <div className="px-4 py-3 text-sm text-muted-foreground |
| 189 | )} |
| 190 | {!isLoading && data |
| 191 | <div className="px-4 py-8 text-center text-sm text-muted-foreground |
| 192 | )} |
| 193 | {data?.users.map |
| 194 | const actions = |
| 195 | const isLast = |
| 196 | return ( |
| 197 | <ContextMenu key={ |
| 198 | {({ onContextMenu } |
| 199 | <div |
| 200 | onContextMenu={onContextMenu} |
| 201 | onClick={() => |
| 202 | className={`grid grid-cols-[1fr_1fr_100px_80px_120px_36px] cursor-pointer items-center px-4 py-3 transition-colors hover:bg-muted/40 ${! |
| 203 | > |
| 204 | <div className="flex items-center gap-3 |
| 205 | <div className="h-7 w-7 shrink-0 overflow-hidden rounded-full bg-muted flex items-center justify-center text-xs font-semibold text-foreground |
| 206 | {u.image ? |
| 207 | <img src={u |
| 208 | ) : ( |
| 209 | <DitherAvatar name={ |
| 210 | )} |
| 211 | </div> |
| 212 | <span className="text-sm font-medium text-foreground |
| 213 | </div> |
| 214 | |
| 215 | <span className="font-mono text-xs text-muted-foreground |
| 216 | |
| 217 | <span |
| 218 | className={`w-fit rounded-full px-2 py-0.5 text-xs font-medium ${ |
| 219 | u.role === |
| 220 | ? "bg-blue-500/10 text-blue-600 dark:text-blue-400" |
| 221 | : "bg-muted text-muted-foreground" |
| 222 | }`} |
| 223 | > |
| 224 | {u.role ?? |
| 225 | </span> |
| 226 | |
| 227 | <div className="flex items-center gap-1.5 |
| 228 | {u.banned ? |
| 229 | <span className="rounded-full bg-red-500/10 px-2 py-0.5 text-xs font-medium text-red-600 dark:text-red-400 |
| 230 | {t("banned") |
| 231 | </span> |
| 232 | ) : ( |
| 233 | <span className="rounded-full bg-green-500/10 px-2 py-0.5 text-xs font-medium text-green-600 dark:text-green-400 |
| 234 | {t("active") |
| 235 | </span> |
| 236 | )} |
| 237 | </div> |
| 238 | |
| 239 | <span className="text-xs text-muted-foreground |
| 240 | {u.createdAt |
| 241 | ? new Date( |
| 242 | : "—"} |
| 243 | </span> |
| 244 | |
| 245 | <RowMenu items={ |
| 246 | </div> |
| 247 | )} |
| 248 | </ContextMenu> |
| 249 | ); |
| 250 | })} |
| 251 | </div> |
| 252 | </div> |
| 253 | |
| 254 | {data && totalPages |
| 255 | <div className="mt-3 flex items-center justify-between |
| 256 | <span className="text-xs text-muted-foreground |
| 257 | {t("pageInfo", |
| 258 | </span> |
| 259 | <div className="flex items-center gap-1.5 |
| 260 | <button |
| 261 | type="button" |
| 262 | disabled={page <= |
| 263 | onClick={() => |
| 264 | className="flex items-center gap-1 rounded-lg border border-border bg-card px-3 py-1.5 text-xs font-medium text-foreground transition-colors hover:bg-muted disabled:opacity-30" |
| 265 | > |
| 266 | <ChevronLeft className=" |
| 267 | {tc("prev") |
| 268 | </button> |
| 269 | <button |
| 270 | type="button" |
| 271 | disabled={page >= |
| 272 | onClick={() => |
| 273 | className="flex items-center gap-1 rounded-lg border border-border bg-card px-3 py-1.5 text-xs font-medium text-foreground transition-colors hover:bg-muted disabled:opacity-30" |
| 274 | > |
| 275 | {tc("next") |
| 276 | <ChevronRight className=" |
| 277 | </button> |
| 278 | </div> |
| 279 | </div> |
| 280 | )} |
| 281 | </div> |
| 282 | </div> |
| 283 | </> |
| 284 | ); |
| 285 | } |