| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import { useRouter } |
| 5 | import { useMutation, useQuery |
| 6 | import { useTranslations } |
| 7 | import Image from "next/image |
| 8 | import { toast } |
| 9 | import { AnimatePresence, motion |
| 10 | import { |
| 11 | Check, |
| 12 | ChevronDown, |
| 13 | ChevronRight, |
| 14 | Loader2, |
| 15 | } from "lucide-react"; |
| 16 | import { |
| 17 | DropdownMenu, |
| 18 | DropdownMenuContent, |
| 19 | DropdownMenuItem, |
| 20 | DropdownMenuTrigger, |
| 21 | } from "@struxa/ui/components/dropdown-menu"; |
| 22 | import { orpc } |
| 23 | import { authClient } |
| 24 | import { syncLocaleFromDB } |
| 25 | |
| 26 | // ─── Step order ─────────────────────────────────────────────────────────────── |
| 27 | // 1. Admin Account |
| 28 | // 2. Import Eggs |
| 29 | // 3. Location |
| 30 | // 4. Node |
| 31 | // 5. Complete |
| 32 | |
| 33 | type Step |
| 34 | |
| 35 | // ─── Step indicator ─────────────────────────────────────────────────────────── |
| 36 | |
| 37 | function StepBar({ current |
| 38 | const shouldReduceMotion = |
| 39 | return ( |
| 40 | <div className="flex flex-col gap-3 |
| 41 | <div className="flex items-center gap-1 |
| 42 | {labels.map(( |
| 43 | const num = ( |
| 44 | const isDone = |
| 45 | const isActive = |
| 46 | return ( |
| 47 | <div key={num |
| 48 | <div className={` |
| 49 | isDone |
| 50 | ? "bg-green-500/10 text-green-600 dark:text-green-400" |
| 51 | : isActive |
| 52 | ? "bg-foreground text-background" |
| 53 | : "text-muted-foreground" |
| 54 | }`}> |
| 55 | {isDone ? <Check |
| 56 | {label} |
| 57 | </div> |
| 58 | {i < labels |
| 59 | <ChevronRight className=" |
| 60 | )} |
| 61 | </div> |
| 62 | ); |
| 63 | })} |
| 64 | </div> |
| 65 | <div className="h-1 w-full overflow-hidden rounded-full bg-muted |
| 66 | <motion.div |
| 67 | className="h-full rounded-full bg-foreground" |
| 68 | initial={false} |
| 69 | animate={{ width: |
| 70 | transition={shouldReduceMotion ? |
| 71 | /> |
| 72 | </div> |
| 73 | </div> |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | // ─── Shared helpers ─────────────────────────────────────────────────────────── |
| 78 | |
| 79 | function inputClass() { |
| 80 | return "h-8.5 w-full rounded-lg border border-input bg-background px-[calc(--spacing(3)-1px)] text-base leading-8.5 text-foreground outline-none shadow-xs/5 transition-shadow placeholder:text-muted-foreground/70 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/25 sm:h-7.5 sm:text-sm sm:leading-7.5"; |
| 81 | } |
| 82 | |
| 83 | function Field({ label |
| 84 | return ( |
| 85 | <div className="flex flex-col items-start gap-2 |
| 86 | <label className="text-sm font-medium text-foreground |
| 87 | {children} |
| 88 | </div> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | function ErrorBanner({ msg |
| 93 | return ( |
| 94 | <div className="rounded-lg border border-destructive/36 bg-destructive/4 px-3 py-2 text-xs text-destructive |
| 95 | {msg} |
| 96 | </div> |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | function PrimaryButton({ children |
| 101 | children: React |
| 102 | disabled?: boolean |
| 103 | type?: " |
| 104 | onClick?: () |
| 105 | }) { |
| 106 | return ( |
| 107 | <button |
| 108 | type={type} |
| 109 | disabled={disabled} |
| 110 | onClick={onClick} |
| 111 | className="flex h-9 items-center gap-2 rounded-lg border border-primary bg-primary px-[calc(--spacing(3)-1px)] text-sm font-medium text-primary-foreground shadow-xs shadow-primary/25 transition-colors hover:bg-primary/90 disabled:pointer-events-none disabled:opacity-64 sm:h-8" |
| 112 | > |
| 113 | {children} |
| 114 | </button> |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | function SecondaryButton({ children |
| 119 | children: React |
| 120 | onClick?: () |
| 121 | }) { |
| 122 | return ( |
| 123 | <button |
| 124 | type="button" |
| 125 | onClick={onClick} |
| 126 | className="flex h-9 items-center rounded-lg border border-input bg-popover px-[calc(--spacing(3)-1px)] text-sm font-medium text-foreground shadow-xs/5 transition-colors hover:bg-accent/50 disabled:pointer-events-none disabled:opacity-64 sm:h-8" |
| 127 | > |
| 128 | {children} |
| 129 | </button> |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | function FlatSelect<T |
| 134 | value, |
| 135 | options, |
| 136 | onChange, |
| 137 | }: { |
| 138 | value: T |
| 139 | options: { |
| 140 | onChange: (v |
| 141 | }) { |
| 142 | const selected = |
| 143 | return ( |
| 144 | <DropdownMenu> |
| 145 | <DropdownMenuTrigger className=" |
| 146 | <span>{selected?.label |
| 147 | <ChevronDown className=" |
| 148 | </DropdownMenuTrigger> |
| 149 | <DropdownMenuContent align=" |
| 150 | {options.map(( |
| 151 | <DropdownMenuItem |
| 152 | key={opt.value |
| 153 | onClick={() => |
| 154 | className="flex cursor-pointer items-center gap-2.5 px-3 py-2 text-sm" |
| 155 | > |
| 156 | <span |
| 157 | className={`h-1.5 w-1.5 shrink-0 rounded-full ${opt |
| 158 | /> |
| 159 | {opt.label} |
| 160 | </DropdownMenuItem> |
| 161 | ))} |
| 162 | </DropdownMenuContent> |
| 163 | </DropdownMenu> |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | // ─── Step 1: Admin Account ──────────────────────────────────────────────────── |
| 168 | |
| 169 | function Step1({ onDone |
| 170 | const t = |
| 171 | const [form, |
| 172 | const [err, |
| 173 | const [busy, |
| 174 | const promote = |
| 175 | |
| 176 | async function submit |
| 177 | e.preventDefault(); |
| 178 | setErr(null); |
| 179 | if (!form.email |
| 180 | if (form.password |
| 181 | if (form.password |
| 182 | setBusy(true); |
| 183 | try { |
| 184 | const { error: |
| 185 | email: form.email |
| 186 | password: form.password |
| 187 | name: form.name |
| 188 | }); |
| 189 | if (signUpErr) throw |
| 190 | const { error: |
| 191 | email: form.email |
| 192 | password: form.password |
| 193 | }); |
| 194 | if (signInErr) throw |
| 195 | await promote.mutateAsync |
| 196 | await syncLocaleFromDB(); |
| 197 | onDone(); |
| 198 | } catch (e) |
| 199 | toast.error(e |
| 200 | } finally { |
| 201 | setBusy(false); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return ( |
| 206 | <form onSubmit={submit |
| 207 | <p className="text-sm text-muted-foreground |
| 208 | {err && <ErrorBanner |
| 209 | <Field label={ |
| 210 | <input className={inputClass |
| 211 | </Field> |
| 212 | <Field label={ |
| 213 | <input type="email |
| 214 | </Field> |
| 215 | <div className="grid grid-cols-2 gap-3 |
| 216 | <Field label={ |
| 217 | <input type="password |
| 218 | </Field> |
| 219 | <Field label={ |
| 220 | <input type="password |
| 221 | </Field> |
| 222 | </div> |
| 223 | <div className="pt-1 |
| 224 | <PrimaryButton type=" |
| 225 | {busy && <Loader2 |
| 226 | {busy ? t |
| 227 | </PrimaryButton> |
| 228 | </div> |
| 229 | </form> |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | // ─── Step 2: Import Eggs ────────────────────────────────────────────────────── |
| 234 | |
| 235 | type EggEntry |
| 236 | type RepoResult |
| 237 | |
| 238 | function Step2({ onDone |
| 239 | const t = |
| 240 | const ts = |
| 241 | const { data: |
| 242 | orpc.onboarding.listEggRepositories |
| 243 | ); |
| 244 | const importMut = |
| 245 | |
| 246 | const [selected, |
| 247 | const [expandedRepos, |
| 248 | const [expandedCats, |
| 249 | const [search, |
| 250 | const [result, |
| 251 | const q = |
| 252 | |
| 253 | const SKIP_REASONS: |
| 254 | download: t("skippedDownload |
| 255 | parse: t("skippedParse |
| 256 | save: t("skippedSave |
| 257 | }; |
| 258 | |
| 259 | function toggleEgg(egg |
| 260 | setSelected(prev => |
| 261 | const n = |
| 262 | n.has(egg |
| 263 | return n; |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | function toggleRepo(repo |
| 268 | setSelected(prev => |
| 269 | const n = |
| 270 | for (const c |
| 271 | for (const e |
| 272 | } |
| 273 | return n; |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | async function doImport |
| 278 | const eggs = |
| 279 | const r = |
| 280 | setResult(r); |
| 281 | toast.success(ts |
| 282 | if (r.skipped |
| 283 | } |
| 284 | |
| 285 | if (isLoading) { |
| 286 | return ( |
| 287 | <div className="flex flex-col gap-4 |
| 288 | <p className="text-sm text-muted-foreground |
| 289 | <div className="flex items-center gap-2 text-sm text-muted-foreground |
| 290 | <Loader2 className=" |
| 291 | {t("loading") |
| 292 | </div> |
| 293 | <SecondaryButton onClick={ |
| 294 | </div> |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | if (isError) { |
| 299 | return ( |
| 300 | <div className="flex flex-col gap-4 |
| 301 | <ErrorBanner msg={ |
| 302 | <SecondaryButton onClick={ |
| 303 | </div> |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | if (result) { |
| 308 | return ( |
| 309 | <div className="flex flex-col gap-4 |
| 310 | <p className="text-sm text-muted-foreground |
| 311 | {result.skipped.length |
| 312 | <div className="rounded-lg border border-warning/30 bg-warning/5 px-3 py-2 |
| 313 | <p className="text-xs font-medium text-warning-foreground |
| 314 | <ul className="mt-1.5 max-h-36 space-y-1 overflow-y-auto pr-1 |
| 315 | {result.skipped.map |
| 316 | <li key={` |
| 317 | <span className="truncate text-foreground |
| 318 | <span className="shrink-0 text-muted-foreground |
| 319 | </li> |
| 320 | ))} |
| 321 | </ul> |
| 322 | </div> |
| 323 | )} |
| 324 | <PrimaryButton onClick={ |
| 325 | <Check className=" |
| 326 | </PrimaryButton> |
| 327 | </div> |
| 328 | ); |
| 329 | } |
| 330 | |
| 331 | return ( |
| 332 | <div className="flex flex-col gap-4 |
| 333 | <p className="text-sm text-muted-foreground |
| 334 | |
| 335 | <input className={inputClass |
| 336 | |
| 337 | <div className="max-h-[45vh] overflow-y-auto overscroll-contain rounded-xl border border-border bg-muted/20 |
| 338 | {repos?.map(( |
| 339 | const allEggs = |
| 340 | const selectedCount = |
| 341 | const open = |
| 342 | |
| 343 | return ( |
| 344 | <div key={repo |
| 345 | <div className="flex items-center justify-between px-3 py-2.5 |
| 346 | <button |
| 347 | type="button" |
| 348 | onClick={() => |
| 349 | className="flex items-center gap-2 text-sm font-medium text-foreground hover:text-muted-foreground transition-colors" |
| 350 | > |
| 351 | {open ? <ChevronDown |
| 352 | {repo.label} |
| 353 | <span className="rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium text-muted-foreground |
| 354 | {selectedCount}/{allEggs |
| 355 | </span> |
| 356 | </button> |
| 357 | <div className="flex items-center gap-2 |
| 358 | <button type="button |
| 359 | <button type="button |
| 360 | </div> |
| 361 | </div> |
| 362 | |
| 363 | {open && repo |
| 364 | const key = |
| 365 | const catOpen = |
| 366 | const eggs = |
| 367 | if (eggs.length === |
| 368 | const showEggs = |
| 369 | |
| 370 | return ( |
| 371 | <div key={cat |
| 372 | <button |
| 373 | type="button" |
| 374 | onClick={() => |
| 375 | className="flex w-full items-center gap-2 px-5 py-2 text-xs text-muted-foreground hover:text-foreground hover:bg-muted/40 transition-colors" |
| 376 | > |
| 377 | {catOpen ? <ChevronDown |
| 378 | <span className="font-mono |
| 379 | <span className="ml-auto text-muted-foreground/60 |
| 380 | </button> |
| 381 | {showEggs && eggs |
| 382 | <label key={egg |
| 383 | <input |
| 384 | type="checkbox" |
| 385 | className="accent-primary" |
| 386 | checked={selected.has |
| 387 | onChange={() => |
| 388 | /> |
| 389 | <span className="text-xs text-foreground |
| 390 | </label> |
| 391 | ))} |
| 392 | </div> |
| 393 | ); |
| 394 | })} |
| 395 | </div> |
| 396 | ); |
| 397 | })} |
| 398 | </div> |
| 399 | |
| 400 | <div className="flex items-center justify-between |
| 401 | <span className="text-xs text-muted-foreground |
| 402 | <div className="flex items-center gap-2 |
| 403 | <SecondaryButton onClick={ |
| 404 | <PrimaryButton disabled={ |
| 405 | {importMut.isPending && |
| 406 | {importMut.isPending ? |
| 407 | </PrimaryButton> |
| 408 | </div> |
| 409 | </div> |
| 410 | </div> |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | // ─── Step 3: Location ───────────────────────────────────────────────────────── |
| 415 | |
| 416 | function Step3({ onDone |
| 417 | const t = |
| 418 | const [form, |
| 419 | const [err, |
| 420 | const create = |
| 421 | |
| 422 | async function submit |
| 423 | e.preventDefault(); |
| 424 | setErr(null); |
| 425 | if (!form.name |
| 426 | try { |
| 427 | const r = |
| 428 | if (r?.id |
| 429 | } catch { |
| 430 | toast.error(t |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return ( |
| 435 | <form onSubmit={submit |
| 436 | <p className="text-sm text-muted-foreground |
| 437 | {err && <ErrorBanner |
| 438 | <div className="grid grid-cols-2 gap-3 |
| 439 | <Field label={ |
| 440 | <input className={inputClass |
| 441 | </Field> |
| 442 | <Field label={ |
| 443 | <input className={inputClass |
| 444 | </Field> |
| 445 | </div> |
| 446 | <Field label={ |
| 447 | <input className={inputClass |
| 448 | </Field> |
| 449 | <div className="pt-1 |
| 450 | <PrimaryButton type=" |
| 451 | {create.isPending && |
| 452 | {create.isPending ? |
| 453 | </PrimaryButton> |
| 454 | </div> |
| 455 | </form> |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | // ─── Step 4: Node ───────────────────────────────────────────────────────────── |
| 460 | |
| 461 | function Step4({ locationId |
| 462 | const t = |
| 463 | const [form, |
| 464 | name: "", fqdn: |
| 465 | memory: "4096", disk |
| 466 | }); |
| 467 | const [err, |
| 468 | const create = |
| 469 | |
| 470 | async function submit |
| 471 | e.preventDefault(); |
| 472 | setErr(null); |
| 473 | if (!form.name |
| 474 | const memory = |
| 475 | const disk = |
| 476 | if (!memory || |
| 477 | if (!disk || |
| 478 | try { |
| 479 | await create.mutateAsync |
| 480 | name: form.name |
| 481 | scheme: form.scheme |
| 482 | daemonListen: parseInt(form |
| 483 | daemonSFTP: parseInt(form |
| 484 | }); |
| 485 | onDone(); |
| 486 | } catch { |
| 487 | toast.error(t |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return ( |
| 492 | <form onSubmit={submit |
| 493 | <p className="text-sm text-muted-foreground |
| 494 | {err && <ErrorBanner |
| 495 | <div className="grid grid-cols-2 gap-3 |
| 496 | <Field label={ |
| 497 | <input className={inputClass |
| 498 | </Field> |
| 499 | <Field label={ |
| 500 | <input className={inputClass |
| 501 | </Field> |
| 502 | </div> |
| 503 | <div className="grid grid-cols-3 gap-3 |
| 504 | <Field label={ |
| 505 | <FlatSelect |
| 506 | value={form.scheme |
| 507 | options={[{ value |
| 508 | onChange={v => |
| 509 | /> |
| 510 | </Field> |
| 511 | <Field label={ |
| 512 | <input type="number |
| 513 | </Field> |
| 514 | <Field label={ |
| 515 | <input type="number |
| 516 | </Field> |
| 517 | </div> |
| 518 | <div className="grid grid-cols-2 gap-3 |
| 519 | <Field label={ |
| 520 | <input type="number |
| 521 | </Field> |
| 522 | <Field label={ |
| 523 | <input type="number |
| 524 | </Field> |
| 525 | </div> |
| 526 | <div className="pt-1 |
| 527 | <PrimaryButton type=" |
| 528 | {create.isPending && |
| 529 | {create.isPending ? |
| 530 | </PrimaryButton> |
| 531 | </div> |
| 532 | </form> |
| 533 | ); |
| 534 | } |
| 535 | |
| 536 | // ─── Step 5: Complete ───────────────────────────────────────────────────────── |
| 537 | |
| 538 | function Step5({ onDone |
| 539 | const t = |
| 540 | |
| 541 | const NEXT_ITEMS = |
| 542 | [t("addAllocations") |
| 543 | [t("createServers") |
| 544 | [t("inviteUsers") |
| 545 | [t("configureSettings") |
| 546 | ] as const; |
| 547 | |
| 548 | return ( |
| 549 | <div className="flex flex-col gap-4 |
| 550 | <p className="text-sm text-muted-foreground |
| 551 | |
| 552 | <div className="rounded-xl border border-border bg-muted/20 overflow-hidden |
| 553 | <div className="border-b border-border px-4 py-2.5 |
| 554 | <p className="text-xs font-medium text-muted-foreground uppercase tracking-widest |
| 555 | </div> |
| 556 | {NEXT_ITEMS.map(( |
| 557 | <div key={title |
| 558 | <div className="text-sm font-medium text-foreground |
| 559 | <div className="text-xs text-muted-foreground |
| 560 | </div> |
| 561 | ))} |
| 562 | </div> |
| 563 | |
| 564 | <div className="pt-1 |
| 565 | <PrimaryButton disabled={ |
| 566 | {busy && <Loader2 |
| 567 | {busy ? t |
| 568 | </PrimaryButton> |
| 569 | </div> |
| 570 | </div> |
| 571 | ); |
| 572 | } |
| 573 | |
| 574 | // ─── Main ───────────────────────────────────────────────────────────────────── |
| 575 | |
| 576 | export default function SetupPage |
| 577 | const t = |
| 578 | const ta = |
| 579 | const router = |
| 580 | const [step, |
| 581 | const [completed, |
| 582 | const [locationId, |
| 583 | const [finishing, |
| 584 | const shouldReduceMotion = |
| 585 | const complete = |
| 586 | |
| 587 | function advance(s |
| 588 | setCompleted(prev => |
| 589 | setStep((s + |
| 590 | } |
| 591 | |
| 592 | async function finish |
| 593 | setFinishing(true); |
| 594 | try { |
| 595 | await complete.mutateAsync |
| 596 | window.location.href |
| 597 | } catch (e) |
| 598 | toast.error(e |
| 599 | setFinishing(false); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | const STEP_LABELS = |
| 604 | t("steps.adminAccount"), |
| 605 | t("steps.importEggs"), |
| 606 | t("steps.location"), |
| 607 | t("steps.node"), |
| 608 | t("steps.complete"), |
| 609 | ]; |
| 610 | |
| 611 | const TITLES: |
| 612 | 1: [t(" |
| 613 | 2: [t(" |
| 614 | 3: [t(" |
| 615 | 4: [t(" |
| 616 | 5: [t(" |
| 617 | }; |
| 618 | |
| 619 | const [title, |
| 620 | |
| 621 | return ( |
| 622 | <main className="min-h-svh bg-background lg:grid lg:grid-cols-2 |
| 623 | {/* Branding panel - desktop only */ |
| 624 | <div className="relative hidden lg:flex lg:flex-col lg:justify-between lg:items-start lg:order-1 bg-muted/40 border-border px-12 py-10 border-r |
| 625 | <Image src=" |
| 626 | <Image src=" |
| 627 | <div className="flex flex-col gap-2 items-start text-left |
| 628 | <p className="text-2xl font-semibold tracking-tight text-foreground |
| 629 | <p className="text-sm text-muted-foreground max-w-xs |
| 630 | {ta("tagline") |
| 631 | </p> |
| 632 | </div> |
| 633 | <p className="text-xs text-muted-foreground/60 |
| 634 | </div> |
| 635 | |
| 636 | {/* Wizard panel */} |
| 637 | <div className="flex min-h-svh flex-col items-center justify-center px-4 py-10 lg:min-h-0 lg:py-16 lg:order-2 |
| 638 | <div className="flex w-full max-w-xl flex-col gap-6 |
| 639 | {/* Logo shown only on mobile */ |
| 640 | <div className="flex justify-center lg:hidden |
| 641 | <Image src=" |
| 642 | <Image src=" |
| 643 | </div> |
| 644 | |
| 645 | <StepBar current={ |
| 646 | |
| 647 | <AnimatePresence mode=" |
| 648 | <motion.div |
| 649 | key={step} |
| 650 | initial={shouldReduceMotion ? |
| 651 | animate={{ opacity: |
| 652 | exit={shouldReduceMotion ? |
| 653 | transition={shouldReduceMotion ? |
| 654 | > |
| 655 | <div className="rounded-2xl border border-border bg-card p-6 |
| 656 | <div className="mb-5 border-b border-border pb-5 |
| 657 | <p className="text-xs font-medium text-muted-foreground |
| 658 | <h2 className="mt-1 text-xl font-semibold text-foreground |
| 659 | <p className="mt-0.5 text-sm text-muted-foreground |
| 660 | </div> |
| 661 | |
| 662 | {step === 1 |
| 663 | {step === 2 |
| 664 | {step === 3 |
| 665 | {step === 4 |
| 666 | {step === 5 |
| 667 | </div> |
| 668 | </motion.div> |
| 669 | </AnimatePresence> |
| 670 | </div> |
| 671 | </div> |
| 672 | </main> |
| 673 | ); |
| 674 | } |