| 1 | "use client"; |
| 2 | |
| 3 | import { use, useEffect |
| 4 | import Link from "next/link |
| 5 | import { useQuery, useMutation |
| 6 | import { FileJson, Trash2 |
| 7 | import { useTranslations } |
| 8 | import { toast } |
| 9 | import { orpc, queryClient |
| 10 | import { ContextMenu, RowMenu |
| 11 | import { ConfirmDialog } |
| 12 | import { |
| 13 | Dialog, |
| 14 | DialogClose, |
| 15 | DialogPopup, |
| 16 | DialogHeader, |
| 17 | DialogTitle, |
| 18 | DialogFooter, |
| 19 | } from "@struxa/ui/components/dialog"; |
| 20 | |
| 21 | function invalidateNests() { |
| 22 | void queryClient.invalidateQueries( |
| 23 | } |
| 24 | |
| 25 | function invalidateEggs(nestId: |
| 26 | void queryClient.invalidateQueries( |
| 27 | } |
| 28 | |
| 29 | function inputClass() { |
| 30 | return "w-full rounded-lg border border-border bg-background px-3 py-1.5 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors"; |
| 31 | } |
| 32 | |
| 33 | const PAGE_SIZE = 15 |
| 34 | |
| 35 | function getPageNumbers(current: |
| 36 | if (total <= |
| 37 | if (current <= |
| 38 | if (current >= |
| 39 | return [1, " |
| 40 | } |
| 41 | |
| 42 | export default function NestDetailPage |
| 43 | const t = |
| 44 | const tc = |
| 45 | |
| 46 | const { id: |
| 47 | const { data: |
| 48 | const { data: |
| 49 | orpc.eggs.listByNest |
| 50 | ); |
| 51 | |
| 52 | const nest = |
| 53 | |
| 54 | const updateNestMutation = |
| 55 | orpc.nests.update |
| 56 | onSuccess: () => |
| 57 | }), |
| 58 | ); |
| 59 | const importMutation = |
| 60 | orpc.eggs.importJson |
| 61 | onSuccess: () => |
| 62 | }), |
| 63 | ); |
| 64 | const deleteMutation = |
| 65 | orpc.eggs.delete |
| 66 | onSuccess: () => |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | const [search, |
| 71 | const [page, |
| 72 | const [isFetchingUrl, |
| 73 | const [urlFetchError, |
| 74 | const [nestName, |
| 75 | const [nestAuthor, |
| 76 | const [nestDescription, |
| 77 | const [showImport, |
| 78 | const [importJson, |
| 79 | const [importUrl, |
| 80 | const [importMode, |
| 81 | const [isDragging, |
| 82 | const [confirmDelete, |
| 83 | const importFileRef = |
| 84 | |
| 85 | const filtered = |
| 86 | const q = |
| 87 | return e.name |
| 88 | }); |
| 89 | |
| 90 | const totalPages = |
| 91 | const paginated = |
| 92 | |
| 93 | useEffect(() => |
| 94 | useEffect(() => |
| 95 | if (totalPages > |
| 96 | }, [totalPages, |
| 97 | |
| 98 | useEffect(() => |
| 99 | if (nest) { |
| 100 | setNestName(nest.name |
| 101 | setNestAuthor(nest.author |
| 102 | setNestDescription(nest.description |
| 103 | } |
| 104 | }, [nest]); |
| 105 | |
| 106 | function readFile(file |
| 107 | const reader = |
| 108 | reader.onload = |
| 109 | const text = |
| 110 | if (typeof text |
| 111 | setImportJson(text); |
| 112 | setImportMode("paste"); |
| 113 | } |
| 114 | }; |
| 115 | reader.readAsText(file |
| 116 | } |
| 117 | |
| 118 | function handleDragOver(e |
| 119 | e.preventDefault(); |
| 120 | setIsDragging(true); |
| 121 | } |
| 122 | |
| 123 | function handleDragLeave(e |
| 124 | if (!e.currentTarget |
| 125 | } |
| 126 | |
| 127 | function handleDrop(e |
| 128 | e.preventDefault(); |
| 129 | setIsDragging(false); |
| 130 | const file = |
| 131 | if (file) readFile |
| 132 | } |
| 133 | |
| 134 | function handleFileChange(e |
| 135 | const file = |
| 136 | if (!file) |
| 137 | readFile(file); |
| 138 | if (importFileRef.current |
| 139 | } |
| 140 | |
| 141 | const isImporting = |
| 142 | |
| 143 | async function handleImport |
| 144 | let json = |
| 145 | setUrlFetchError(null); |
| 146 | if (importMode === |
| 147 | if (!importUrl.trim |
| 148 | setIsFetchingUrl(true); |
| 149 | try { |
| 150 | const res = |
| 151 | if (!res.ok |
| 152 | setUrlFetchError(t("urlFetchFailed |
| 153 | return; |
| 154 | } |
| 155 | json = await |
| 156 | } catch (e) |
| 157 | setUrlFetchError(e instanceof |
| 158 | return; |
| 159 | } finally { |
| 160 | setIsFetchingUrl(false); |
| 161 | } |
| 162 | } |
| 163 | if (!json) |
| 164 | try { |
| 165 | await importMutation.mutateAsync |
| 166 | setImportJson(""); |
| 167 | setImportUrl(""); |
| 168 | setShowImport(false); |
| 169 | } catch { |
| 170 | // error toasted globally via MutationCache.onError |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | async function handleSaveNest |
| 175 | await updateNestMutation.mutateAsync |
| 176 | id: nestId, |
| 177 | name: nestName, |
| 178 | author: nestAuthor, |
| 179 | description: nestDescription, |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | function eggActions(egg |
| 184 | return [ |
| 185 | { |
| 186 | label: tc("delete |
| 187 | icon: Trash2, |
| 188 | onClick: () => |
| 189 | destructive: true, |
| 190 | }, |
| 191 | ]; |
| 192 | } |
| 193 | |
| 194 | const pageNumbers = |
| 195 | |
| 196 | return ( |
| 197 | <> |
| 198 | <ConfirmDialog |
| 199 | open={confirmDelete !== |
| 200 | onOpenChange={(open) |
| 201 | title={t(" |
| 202 | description={t(" |
| 203 | confirmLabel={tc(" |
| 204 | destructive |
| 205 | loading={deleteMutation.isPending |
| 206 | onConfirm={async () |
| 207 | if (!confirmDelete) return |
| 208 | await deleteMutation.mutateAsync |
| 209 | setConfirmDelete(null); |
| 210 | }} |
| 211 | /> |
| 212 | |
| 213 | <Dialog open={ |
| 214 | <DialogPopup showCloseButton={ |
| 215 | <DialogHeader> |
| 216 | <DialogTitle>{t( |
| 217 | </DialogHeader> |
| 218 | |
| 219 | <div className="flex flex-col gap-4 px-5 py-4 |
| 220 | <input |
| 221 | ref={importFileRef} |
| 222 | id="egg-import-file" |
| 223 | type="file" |
| 224 | accept=".json" |
| 225 | className="hidden" |
| 226 | onChange={handleFileChange} |
| 227 | /> |
| 228 | <button |
| 229 | type="button" |
| 230 | onDragOver={handleDragOver} |
| 231 | onDragLeave={handleDragLeave} |
| 232 | onDrop={handleDrop} |
| 233 | onClick={() => |
| 234 | className={`flex w-full cursor-pointer flex-col items-center gap-2 rounded-lg border-2 border-dashed px-4 py-6 text-center transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ${ |
| 235 | isDragging |
| 236 | ? "border-ring bg-muted/60" |
| 237 | : "border-border bg-muted/20 hover:border-ring/50 hover:bg-muted/40" |
| 238 | }`} |
| 239 | > |
| 240 | <FileJson className=" |
| 241 | <span className="text-sm text-muted-foreground |
| 242 | </button> |
| 243 | |
| 244 | <div className="flex gap-0 border-b border-border |
| 245 | <button |
| 246 | type="button" |
| 247 | onClick={() => |
| 248 | className={`px-3 pb-2 text-xs font-medium transition-colors ${ |
| 249 | importMode === "paste" |
| 250 | ? "border-b-2 border-foreground text-foreground" |
| 251 | : "text-muted-foreground hover:text-foreground" |
| 252 | }`} |
| 253 | > |
| 254 | {t("pasteJsonTab") |
| 255 | </button> |
| 256 | <button |
| 257 | type="button" |
| 258 | onClick={() => |
| 259 | className={`px-3 pb-2 text-xs font-medium transition-colors ${ |
| 260 | importMode === "url" |
| 261 | ? "border-b-2 border-foreground text-foreground" |
| 262 | : "text-muted-foreground hover:text-foreground" |
| 263 | }`} |
| 264 | > |
| 265 | {t("fromUrlTab") |
| 266 | </button> |
| 267 | </div> |
| 268 | |
| 269 | {importMode === "paste |
| 270 | <textarea |
| 271 | autoFocus |
| 272 | className="w-full rounded-lg border border-border bg-background px-3 py-2 font-mono text-xs text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 273 | rows={8} |
| 274 | placeholder={t(" |
| 275 | value={importJson} |
| 276 | onChange={(e) |
| 277 | /> |
| 278 | )} |
| 279 | |
| 280 | {importMode === "url |
| 281 | <input |
| 282 | autoFocus |
| 283 | type="url" |
| 284 | className={inputClass()} |
| 285 | placeholder={t(" |
| 286 | value={importUrl} |
| 287 | onChange={(e) |
| 288 | /> |
| 289 | )} |
| 290 | |
| 291 | {importMode === "url |
| 292 | <span className="text-xs text-destructive |
| 293 | )} |
| 294 | </div> |
| 295 | |
| 296 | <DialogFooter> |
| 297 | <DialogClose className=" |
| 298 | {tc("cancel") |
| 299 | </DialogClose> |
| 300 | <button |
| 301 | type="button" |
| 302 | onClick={handleImport} |
| 303 | disabled={isImporting} |
| 304 | className="rounded-lg bg-foreground px-4 py-1.5 text-xs font-medium text-background transition-opacity hover:opacity-80 disabled:opacity-40" |
| 305 | > |
| 306 | {isImporting ? tc |
| 307 | </button> |
| 308 | </DialogFooter> |
| 309 | </DialogPopup> |
| 310 | </Dialog> |
| 311 | |
| 312 | <div className="flex-1 overflow-auto px-6 py-5 |
| 313 | <div className="mx-auto max-w-3xl flex flex-col gap-4 |
| 314 | <div className="mb-1 flex items-center justify-between |
| 315 | <div className="flex items-center gap-2 text-sm |
| 316 | <Link href={ |
| 317 | {t("nestsLink") |
| 318 | </Link> |
| 319 | <ChevronRight className=" |
| 320 | <span className="font-medium text-foreground |
| 321 | </div> |
| 322 | <button |
| 323 | type="button" |
| 324 | onClick={() => |
| 325 | className="flex items-center gap-1.5 rounded-lg bg-foreground px-3 py-1.5 text-sm font-medium text-background transition-opacity hover:opacity-80" |
| 326 | > |
| 327 | <FileJson className=" |
| 328 | {tc("import") |
| 329 | </button> |
| 330 | </div> |
| 331 | |
| 332 | <div className="rounded-xl border border-border bg-card |
| 333 | <div className="border-b border-border px-4 py-3 |
| 334 | <h2 className="text-sm font-semibold text-foreground |
| 335 | </div> |
| 336 | <div className="p-4 |
| 337 | <div className="grid grid-cols-3 gap-3 |
| 338 | <div className="flex flex-col gap-1.5 |
| 339 | <label className="text-xs font-medium text-foreground |
| 340 | <input className={inputClass |
| 341 | </div> |
| 342 | <div className="flex flex-col gap-1.5 |
| 343 | <label className="text-xs font-medium text-foreground |
| 344 | <input className={inputClass |
| 345 | </div> |
| 346 | <div className="flex flex-col gap-1.5 |
| 347 | <label className="text-xs font-medium text-foreground |
| 348 | <input className={inputClass |
| 349 | </div> |
| 350 | </div> |
| 351 | <div className="mt-3 flex items-center gap-2 |
| 352 | <button |
| 353 | type="button" |
| 354 | onClick={handleSaveNest} |
| 355 | disabled={updateNestMutation.isPending |
| 356 | className="rounded-lg bg-foreground px-4 py-1.5 text-sm font-medium text-background transition-opacity hover:opacity-80 disabled:opacity-40" |
| 357 | > |
| 358 | {updateNestMutation.isPending ? |
| 359 | </button> |
| 360 | </div> |
| 361 | </div> |
| 362 | </div> |
| 363 | |
| 364 | <div className="flex items-center justify-between |
| 365 | <h2 className="text-sm font-semibold text-foreground |
| 366 | <div className="relative flex items-center |
| 367 | <Search className=" |
| 368 | <input |
| 369 | 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/50 focus:border-ring transition-colors" |
| 370 | placeholder={t(" |
| 371 | value={search} |
| 372 | onChange={(e) |
| 373 | /> |
| 374 | </div> |
| 375 | </div> |
| 376 | |
| 377 | <div className="overflow-x-auto |
| 378 | <div className="min-w-[500px] overflow-hidden rounded-xl border border-border bg-card |
| 379 | <div className="grid grid-cols-[1fr_220px_36px] border-b border-border bg-muted/40 px-4 py-2.5 |
| 380 | <span className="text-xs font-medium text-muted-foreground |
| 381 | <span className="text-xs font-medium text-muted-foreground |
| 382 | <span /> |
| 383 | </div> |
| 384 | |
| 385 | {isLoading && ( |
| 386 | <div className="px-4 py-8 text-center text-sm text-muted-foreground |
| 387 | )} |
| 388 | {!isLoading && filtered |
| 389 | <div className="px-4 py-8 text-center text-sm text-muted-foreground |
| 390 | {search ? t |
| 391 | </div> |
| 392 | )} |
| 393 | {paginated.map(( |
| 394 | const actions = |
| 395 | const isLast = |
| 396 | return ( |
| 397 | <ContextMenu key={ |
| 398 | {({ onContextMenu } |
| 399 | <div |
| 400 | onContextMenu={onContextMenu} |
| 401 | className={`grid grid-cols-[1fr_220px_36px] cursor-pointer items-center px-4 py-3 transition-colors hover:bg-muted/40 ${! |
| 402 | > |
| 403 | <div className="flex min-w-0 flex-col gap-0.5 |
| 404 | <Link |
| 405 | href={`/admin/nests/${nestId |
| 406 | className="w-fit text-sm font-medium text-foreground transition-colors hover:text-blue-500" |
| 407 | onClick={(e) |
| 408 | > |
| 409 | {egg.name} |
| 410 | </Link> |
| 411 | {egg.description && |
| 412 | <span className="truncate text-xs text-muted-foreground |
| 413 | )} |
| 414 | </div> |
| 415 | <span className="truncate font-mono text-xs text-muted-foreground |
| 416 | <RowMenu items={ |
| 417 | </div> |
| 418 | )} |
| 419 | </ContextMenu> |
| 420 | ); |
| 421 | })} |
| 422 | </div> |
| 423 | </div> |
| 424 | |
| 425 | {totalPages > 1 |
| 426 | <div className="flex items-center justify-center gap-1 |
| 427 | <button |
| 428 | type="button" |
| 429 | aria-label={t(" |
| 430 | disabled={page <= |
| 431 | onClick={() => |
| 432 | className="flex h-7 w-7 items-center justify-center rounded-md border border-border bg-card text-xs font-medium text-foreground transition-colors hover:bg-muted disabled:opacity-30" |
| 433 | > |
| 434 | <ChevronLeft className=" |
| 435 | </button> |
| 436 | {pageNumbers.map(( |
| 437 | n === "…" |
| 438 | <span key={` |
| 439 | … |
| 440 | </span> |
| 441 | ) : ( |
| 442 | <button |
| 443 | key={n} |
| 444 | type="button" |
| 445 | aria-current={n === |
| 446 | onClick={() => |
| 447 | className={`flex h-7 w-7 items-center justify-center rounded-md text-xs font-medium transition-colors ${ |
| 448 | n === page |
| 449 | ? "bg-foreground text-background" |
| 450 | : "border border-border bg-card text-foreground hover:bg-muted" |
| 451 | }`} |
| 452 | > |
| 453 | {n} |
| 454 | </button> |
| 455 | ) |
| 456 | )} |
| 457 | <button |
| 458 | type="button" |
| 459 | aria-label={t(" |
| 460 | disabled={page >= |
| 461 | onClick={() => |
| 462 | className="flex h-7 w-7 items-center justify-center rounded-md border border-border bg-card text-xs font-medium text-foreground transition-colors hover:bg-muted disabled:opacity-30" |
| 463 | > |
| 464 | <ChevronRight className=" |
| 465 | </button> |
| 466 | </div> |
| 467 | )} |
| 468 | </div> |
| 469 | </div> |
| 470 | </> |
| 471 | ); |
| 472 | } |