| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef, useState } from "react"; |
| 4 | import { useRouter, useSearchParams |
| 5 | import { useQuery, useMutation |
| 6 | import { useTranslations } |
| 7 | import { |
| 8 | ArrowUpRight, ArrowDownLeft, |
| 9 | SlidersHorizontal, Clock, |
| 10 | Wallet, ChevronLeft, |
| 11 | } from "lucide-react"; |
| 12 | import { toast } |
| 13 | import { orpc } |
| 14 | import { cn } |
| 15 | import { Button } |
| 16 | import { useMediaQuery } |
| 17 | import { Input } |
| 18 | import { |
| 19 | Dialog, |
| 20 | DialogPopup, |
| 21 | DialogHeader, |
| 22 | DialogTitle, |
| 23 | DialogDescription, |
| 24 | } from "@struxa/ui/components/dialog"; |
| 25 | import { |
| 26 | Sheet, |
| 27 | SheetContent, |
| 28 | SheetHeader, |
| 29 | SheetTitle, |
| 30 | SheetDescription, |
| 31 | } from "@struxa/ui/components/sheet"; |
| 32 | |
| 33 | type TxType |
| 34 | |
| 35 | function formatBalance(cents: |
| 36 | try { |
| 37 | return new Intl |
| 38 | } catch { |
| 39 | return (cents / |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | function formatAmount(cents: |
| 44 | try { |
| 45 | return new Intl |
| 46 | } catch { |
| 47 | return (Math.abs |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function formatDate(date: |
| 52 | return new Intl |
| 53 | } |
| 54 | |
| 55 | const TYPE_ICON: Record |
| 56 | topup: ArrowUpRight, |
| 57 | refund: RotateCcw, |
| 58 | charge: ArrowDownLeft, |
| 59 | adjustment: SlidersHorizontal, |
| 60 | expired: Clock, |
| 61 | }; |
| 62 | |
| 63 | const TYPE_POSITIVE: Record |
| 64 | topup: true, |
| 65 | refund: true, |
| 66 | charge: false, |
| 67 | adjustment: false, |
| 68 | expired: false, |
| 69 | }; |
| 70 | |
| 71 | const PROVIDER_ICON: Record |
| 72 | stripe: CreditCard, |
| 73 | simpay: Banknote, |
| 74 | paypal: CreditCard, |
| 75 | przelewy24: Banknote, |
| 76 | }; |
| 77 | |
| 78 | const PRESET_AMOUNTS = [ |
| 79 | |
| 80 | |
| 81 | function TopUpForm({ |
| 82 | currency, |
| 83 | isPending, |
| 84 | onSubmit, |
| 85 | }: { |
| 86 | currency: string |
| 87 | isPending: boolean |
| 88 | onSubmit: (amountCents |
| 89 | }) { |
| 90 | const t = |
| 91 | const [selectedPreset, |
| 92 | const [customAmount, |
| 93 | |
| 94 | const resolvedAmount = |
| 95 | |
| 96 | function handleSubmit() { |
| 97 | if (!resolvedAmount || |
| 98 | onSubmit(Math.round |
| 99 | } |
| 100 | |
| 101 | return ( |
| 102 | <div className="flex flex-col gap-3 p-5 |
| 103 | <div className="grid grid-cols-2 gap-2 |
| 104 | {PRESET_AMOUNTS.map(( |
| 105 | <button |
| 106 | key={amount} |
| 107 | type="button" |
| 108 | onClick={() => |
| 109 | className={cn( |
| 110 | "rounded-xl border py-2.5 text-sm font-medium transition-all", |
| 111 | selectedPreset === amount |
| 112 | ? "border-foreground bg-foreground text-background" |
| 113 | : "border-border bg-transparent text-muted-foreground hover:border-foreground/40 hover:text-foreground", |
| 114 | )} |
| 115 | > |
| 116 | {formatBalance(amount * |
| 117 | </button> |
| 118 | ))} |
| 119 | </div> |
| 120 | |
| 121 | <Input |
| 122 | type="number" |
| 123 | min="1" |
| 124 | step="0.01" |
| 125 | placeholder={t(" |
| 126 | value={customAmount} |
| 127 | onChange={(e) |
| 128 | /> |
| 129 | |
| 130 | <Button |
| 131 | className="w-full" |
| 132 | disabled={!resolvedAmount |
| 133 | onClick={handleSubmit} |
| 134 | > |
| 135 | {isPending ? t |
| 136 | </Button> |
| 137 | </div> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | const DURATION_KEYS = new |
| 142 | const DURATION_LEGACY: Record |
| 143 | "7 Days": "7day", |
| 144 | "1 Month": "1month", |
| 145 | "3 Months": "3months", |
| 146 | "6 Months": "6months", |
| 147 | "1 Year": "1year", |
| 148 | }; |
| 149 | const TX_DESC_KEYS = new |
| 150 | const TX_DESC_LEGACY: Record |
| 151 | "Wallet top-up via payment provider": "wallet_topup", |
| 152 | }; |
| 153 | const PROVIDER_DISPLAY: Record |
| 154 | stripe: "Stripe", |
| 155 | simpay: "SimPay", |
| 156 | paypal: "PayPal", |
| 157 | przelewy24: "Przelewy24", |
| 158 | }; |
| 159 | |
| 160 | export default function WalletPage |
| 161 | const t = |
| 162 | const tBilling = |
| 163 | const tr = |
| 164 | const tc = |
| 165 | const router = |
| 166 | const searchParams = |
| 167 | const isMobile = |
| 168 | const [open, |
| 169 | const [topupStep, |
| 170 | const [selectedGatewayId, |
| 171 | const successHandled = |
| 172 | |
| 173 | const [inputCode, |
| 174 | const [newCode, |
| 175 | const [editingCode, |
| 176 | const [editCode, |
| 177 | const [txPage, |
| 178 | const queryClient = |
| 179 | |
| 180 | const TX_PAGE_SIZE = |
| 181 | |
| 182 | const { data: |
| 183 | const { data: |
| 184 | orpc.billing.listWalletTransactions |
| 185 | ); |
| 186 | const hasNextPage = |
| 187 | const transactions = |
| 188 | const { data: |
| 189 | const { data: |
| 190 | const { data: |
| 191 | ...orpc.billing. |
| 192 | enabled: !!billingConfig?.referralEnabled |
| 193 | }); |
| 194 | |
| 195 | const generateCodeMutation = |
| 196 | ...orpc.billing. |
| 197 | meta: { customError: true |
| 198 | onSuccess: () => |
| 199 | void queryClient.invalidateQueries( |
| 200 | setNewCode(""); |
| 201 | toast.success(tr |
| 202 | }, |
| 203 | onError: (err: |
| 204 | const code = |
| 205 | if (code === |
| 206 | else toast.error( |
| 207 | }, |
| 208 | }); |
| 209 | |
| 210 | const updateCodeMutation = |
| 211 | ...orpc.billing. |
| 212 | meta: { customError: true |
| 213 | onSuccess: () => |
| 214 | void queryClient.invalidateQueries( |
| 215 | setEditingCode(false); |
| 216 | setEditCode(""); |
| 217 | toast.success(tr |
| 218 | }, |
| 219 | onError: (err: |
| 220 | const code = |
| 221 | if (code === |
| 222 | else toast.error( |
| 223 | }, |
| 224 | }); |
| 225 | |
| 226 | const applyCodeMutation = |
| 227 | ...orpc.billing. |
| 228 | meta: { customError: true |
| 229 | onSuccess: () => |
| 230 | toast.success(tr |
| 231 | setInputCode(""); |
| 232 | }, |
| 233 | onError: (err: |
| 234 | const code = |
| 235 | if (code === |
| 236 | else if (code |
| 237 | else if (code |
| 238 | else toast.error( |
| 239 | }, |
| 240 | }); |
| 241 | |
| 242 | const topupMutation = |
| 243 | ...orpc.billing. |
| 244 | meta: { customError: true |
| 245 | onSuccess: ({ url |
| 246 | router.push(url |
| 247 | }, |
| 248 | onError: () => |
| 249 | toast.error(t |
| 250 | }, |
| 251 | }); |
| 252 | |
| 253 | useEffect(() => |
| 254 | if (searchParams.get |
| 255 | successHandled.current = |
| 256 | toast.success(t |
| 257 | void refetchWallet(); |
| 258 | void refetchTransactions(); |
| 259 | const url = |
| 260 | url.searchParams.delete |
| 261 | router.replace((url |
| 262 | } |
| 263 | }, [searchParams, |
| 264 | |
| 265 | const currency = |
| 266 | |
| 267 | function openTopup() { |
| 268 | if (gateways.length |
| 269 | toast.error(t |
| 270 | return; |
| 271 | } |
| 272 | if (gateways.length |
| 273 | setSelectedGatewayId(gateways[0] |
| 274 | setTopupStep("amount"); |
| 275 | } else { |
| 276 | setSelectedGatewayId(null); |
| 277 | setTopupStep("provider"); |
| 278 | } |
| 279 | setOpen(true); |
| 280 | } |
| 281 | |
| 282 | function handleClose(v |
| 283 | setOpen(v); |
| 284 | if (!v) |
| 285 | setSelectedGatewayId(null); |
| 286 | setTopupStep("provider"); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | function handleTopup(amountCents |
| 291 | const base = |
| 292 | topupMutation.mutate({ |
| 293 | gatewayId: selectedGatewayId!, |
| 294 | amountCents, |
| 295 | successUrl: `${base} |
| 296 | cancelUrl: `${base} |
| 297 | }); |
| 298 | } |
| 299 | |
| 300 | function applyCode() { |
| 301 | if (!inputCode.trim |
| 302 | applyCodeMutation.mutate({ |
| 303 | } |
| 304 | |
| 305 | const isProviderStep = |
| 306 | const modalTitle = |
| 307 | const modalDesc = |
| 308 | |
| 309 | const BANNER_PROVIDERS = |
| 310 | |
| 311 | const modalContent = |
| 312 | <div className="flex flex-col gap-2 p-5 |
| 313 | {gateways.map(( |
| 314 | const hasBanner = |
| 315 | const Icon = |
| 316 | return ( |
| 317 | <button |
| 318 | key={gw.id |
| 319 | type="button" |
| 320 | onClick={() => |
| 321 | className="overflow-hidden rounded-xl border border-border transition-all hover:border-foreground/40 hover:bg-muted/30" |
| 322 | > |
| 323 | {hasBanner ? ( |
| 324 | <div className="flex items-center justify-center px-4 py-3 |
| 325 | <img |
| 326 | src={`/providers/${gw |
| 327 | alt={gw.name |
| 328 | className="h-7 w-auto object-contain" |
| 329 | /> |
| 330 | </div> |
| 331 | ) : ( |
| 332 | <div className="flex items-center gap-3 px-4 py-3 text-left |
| 333 | <Icon className=" |
| 334 | <span className="text-sm font-medium text-foreground |
| 335 | </div> |
| 336 | )} |
| 337 | </button> |
| 338 | ); |
| 339 | })} |
| 340 | </div> |
| 341 | ) : ( |
| 342 | <> |
| 343 | {gateways.length > |
| 344 | <button |
| 345 | type="button" |
| 346 | onClick={() => |
| 347 | className="flex items-center gap-1 px-5 pt-3 text-xs text-muted-foreground hover:text-foreground transition-colors" |
| 348 | > |
| 349 | <ChevronLeft className=" |
| 350 | {t("back") |
| 351 | </button> |
| 352 | )} |
| 353 | <TopUpForm currency={ |
| 354 | </> |
| 355 | ); |
| 356 | |
| 357 | return ( |
| 358 | <div className="flex-1 overflow-auto px-4 py-5 sm:px-6 |
| 359 | <div className="mx-auto max-w-3xl flex flex-col gap-6 |
| 360 | |
| 361 | {/* Balance card */} |
| 362 | <div className="rounded-2xl border border-border bg-card px-6 py-6 flex items-center justify-between gap-4 |
| 363 | <div className="flex flex-col gap-1 |
| 364 | <p className="text-xs text-muted-foreground |
| 365 | <p className="text-4xl font-bold tabular-nums text-foreground leading-none |
| 366 | {wallet ? formatBalance |
| 367 | </p> |
| 368 | </div> |
| 369 | <Button onClick={ |
| 370 | {t("topUp") |
| 371 | </Button> |
| 372 | </div> |
| 373 | |
| 374 | {/* Transaction history */} |
| 375 | <div className="flex flex-col gap-3 |
| 376 | <h2 className="text-sm font-semibold text-foreground |
| 377 | |
| 378 | {txLoading ? ( |
| 379 | <div className="flex flex-col gap-2 |
| 380 | {Array.from({ |
| 381 | <div key={i |
| 382 | ))} |
| 383 | </div> |
| 384 | ) : transactions.length |
| 385 | <div className="flex flex-col items-center gap-2 py-14 text-center |
| 386 | <Wallet className=" |
| 387 | <p className="text-sm text-muted-foreground |
| 388 | <p className="text-xs text-muted-foreground/60 |
| 389 | </div> |
| 390 | ) : ( |
| 391 | <div className="overflow-hidden rounded-xl border border-border bg-card divide-y divide-border/60 |
| 392 | {transactions.map(( |
| 393 | const Icon = |
| 394 | const isPositive = |
| 395 | return ( |
| 396 | <div key={tx |
| 397 | <div className={cn |
| 398 | "flex h-8 w-8 shrink-0 items-center justify-center rounded-lg", |
| 399 | isPositive ? "bg-emerald-500/10 text-emerald-500" |
| 400 | )}> |
| 401 | <Icon className=" |
| 402 | </div> |
| 403 | <div className="min-w-0 flex-1 |
| 404 | <p className="truncate text-sm font-medium text-foreground |
| 405 | {t(`types.${tx |
| 406 | {tx.description && |
| 407 | const desc = |
| 408 | if (desc.startsWith( |
| 409 | const providerKey = |
| 410 | const providerName = |
| 411 | return <span className |
| 412 | } |
| 413 | if (TX_DESC_KEYS.has( |
| 414 | return <span className |
| 415 | } |
| 416 | const sep = |
| 417 | if (sep !== - |
| 418 | const rawSuffix = |
| 419 | if (rawSuffix.startsWith( |
| 420 | const durKey = |
| 421 | const dur = |
| 422 | if (dur) { |
| 423 | return <span className |
| 424 | } |
| 425 | } |
| 426 | const suffix = |
| 427 | if (suffix) { |
| 428 | return <span className |
| 429 | } |
| 430 | } |
| 431 | return <span className |
| 432 | })()} |
| 433 | </p> |
| 434 | <p className="text-xs text-muted-foreground |
| 435 | </div> |
| 436 | <div className="text-right shrink-0 |
| 437 | <p className={cn |
| 438 | {isPositive ? "+ |
| 439 | </p> |
| 440 | <p className="text-xs text-muted-foreground tabular-nums |
| 441 | {formatBalance(tx.balanceAfterCents |
| 442 | </p> |
| 443 | </div> |
| 444 | </div> |
| 445 | ); |
| 446 | })} |
| 447 | </div> |
| 448 | )} |
| 449 | |
| 450 | {(txPage > 0 |
| 451 | <div className="flex items-center justify-between |
| 452 | <button |
| 453 | type="button" |
| 454 | onClick={() => |
| 455 | disabled={txPage === |
| 456 | className="flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-foreground disabled:pointer-events-none disabled:opacity-40" |
| 457 | > |
| 458 | <ChevronLeft className=" |
| 459 | {tc("prev") |
| 460 | </button> |
| 461 | <span className="text-xs text-muted-foreground |
| 462 | <button |
| 463 | type="button" |
| 464 | onClick={() => |
| 465 | disabled={!hasNextPage} |
| 466 | className="flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-foreground disabled:pointer-events-none disabled:opacity-40" |
| 467 | > |
| 468 | {tc("next") |
| 469 | <ChevronRight className=" |
| 470 | </button> |
| 471 | </div> |
| 472 | )} |
| 473 | </div> |
| 474 | |
| 475 | {/* Referral program */} |
| 476 | {billingConfig?.referralEnabled && |
| 477 | <div> |
| 478 | <h2 className="text-sm font-semibold text-foreground |
| 479 | <p className="text-xs text-muted-foreground mt-0.5 |
| 480 | </div> |
| 481 | |
| 482 | <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 |
| 483 | {/* Your referral code */} |
| 484 | <div className="rounded-xl border border-border bg-card p-4 flex flex-col gap-3 |
| 485 | <div> |
| 486 | <p className="text-xs font-semibold text-foreground |
| 487 | <p className="text-[11px] text-muted-foreground mt-0.5 |
| 488 | </div> |
| 489 | {referralCode ? ( |
| 490 | <div className="flex flex-col gap-2 |
| 491 | {editingCode ? ( |
| 492 | <div className="flex gap-2 |
| 493 | <Input |
| 494 | value={editCode} |
| 495 | onChange={(e) |
| 496 | placeholder={referralCode.code |
| 497 | className="font-mono text-xs h-8" |
| 498 | maxLength={30} |
| 499 | autoFocus |
| 500 | /> |
| 501 | <Button |
| 502 | type="button" |
| 503 | size="sm" |
| 504 | onClick={() => |
| 505 | disabled={editCode.length |
| 506 | className="shrink-0 h-8" |
| 507 | > |
| 508 | {tr("save") |
| 509 | </Button> |
| 510 | <Button |
| 511 | type="button" |
| 512 | size="sm" |
| 513 | variant="outline" |
| 514 | onClick={() => |
| 515 | className="shrink-0 h-8" |
| 516 | > |
| 517 | {tr("cancel") |
| 518 | </Button> |
| 519 | </div> |
| 520 | ) : ( |
| 521 | <div className="flex gap-2 |
| 522 | <Input |
| 523 | readOnly |
| 524 | value={referralCode.code |
| 525 | className="font-mono text-xs h-8 bg-muted/40" |
| 526 | /> |
| 527 | <Button |
| 528 | type="button" |
| 529 | size="icon-sm" |
| 530 | variant="ghost" |
| 531 | aria-label={tr(" |
| 532 | onClick={() => |
| 533 | className="shrink-0 h-8 w-8" |
| 534 | > |
| 535 | <Pencil className=" |
| 536 | </Button> |
| 537 | </div> |
| 538 | )} |
| 539 | <p className="text-[10px] text-muted-foreground |
| 540 | </div> |
| 541 | ) : ( |
| 542 | <div className="flex gap-2 |
| 543 | <Input |
| 544 | value={newCode} |
| 545 | onChange={(e) |
| 546 | placeholder={tr(" |
| 547 | className="font-mono text-xs h-8" |
| 548 | maxLength={30} |
| 549 | /> |
| 550 | <Button |
| 551 | type="button" |
| 552 | size="sm" |
| 553 | variant="outline" |
| 554 | onClick={() => |
| 555 | disabled={newCode.length |
| 556 | className="shrink-0 h-8" |
| 557 | > |
| 558 | {tr("create") |
| 559 | </Button> |
| 560 | </div> |
| 561 | )} |
| 562 | </div> |
| 563 | |
| 564 | {/* Apply a code */} |
| 565 | <div className="rounded-xl border border-border bg-card p-4 flex flex-col gap-3 |
| 566 | <div> |
| 567 | <p className="text-xs font-semibold text-foreground |
| 568 | <p className="text-[11px] text-muted-foreground mt-0.5 |
| 569 | </div> |
| 570 | <div className="flex gap-2 |
| 571 | <Input |
| 572 | value={inputCode} |
| 573 | onChange={(e) |
| 574 | placeholder={tr(" |
| 575 | className="text-xs h-8 font-mono" |
| 576 | disabled={applyCodeMutation.isPending |
| 577 | /> |
| 578 | <Button |
| 579 | type="button" |
| 580 | size="sm" |
| 581 | onClick={applyCode} |
| 582 | disabled={!inputCode. |
| 583 | className="shrink-0 h-8" |
| 584 | > |
| 585 | {tr("apply") |
| 586 | </Button> |
| 587 | </div> |
| 588 | </div> |
| 589 | </div> |
| 590 | </div>} |
| 591 | |
| 592 | </div> |
| 593 | |
| 594 | {isMobile ? ( |
| 595 | <Sheet open={ |
| 596 | <SheetContent side=" |
| 597 | <SheetHeader> |
| 598 | <SheetTitle>{modalTitle} |
| 599 | <SheetDescription>{modalDesc} |
| 600 | </SheetHeader> |
| 601 | {modalContent} |
| 602 | </SheetContent> |
| 603 | </Sheet> |
| 604 | ) : ( |
| 605 | <Dialog open={ |
| 606 | <DialogPopup className=" |
| 607 | <DialogHeader> |
| 608 | <DialogTitle>{modalTitle} |
| 609 | <DialogDescription>{modalDesc} |
| 610 | </DialogHeader> |
| 611 | {modalContent} |
| 612 | </DialogPopup> |
| 613 | </Dialog> |
| 614 | )} |
| 615 | </div> |
| 616 | ); |
| 617 | } |