|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { getFreighterPublicKey } from "@/lib/freighter"; |
| 5 | +import { splitClient } from "@/lib/stellar"; |
| 6 | + |
| 7 | +const STORAGE_KEY = "recipientOnboarded"; |
| 8 | +const TOTAL_STEPS = 3; |
| 9 | + |
| 10 | +export default function RecipientOnboarding() { |
| 11 | + const [show, setShow] = useState(false); |
| 12 | + const [step, setStep] = useState(1); |
| 13 | + const [invoiceCount, setInvoiceCount] = useState(0); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + if (localStorage.getItem(STORAGE_KEY)) return; |
| 17 | + |
| 18 | + async function check() { |
| 19 | + try { |
| 20 | + const address = await getFreighterPublicKey(); |
| 21 | + if (!address) return; |
| 22 | + |
| 23 | + const invoices = await splitClient.getInvoicesByRecipient(address); |
| 24 | + if (!invoices || invoices.length === 0) return; |
| 25 | + |
| 26 | + setInvoiceCount(invoices.length); |
| 27 | + setShow(true); |
| 28 | + } catch { |
| 29 | + // wallet not connected or SDK method unavailable — skip silently |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + check(); |
| 34 | + |
| 35 | + // Re-check when wallet connects later in the session |
| 36 | + const onWalletEvent = () => check(); |
| 37 | + window.addEventListener("wallet-connected", onWalletEvent); |
| 38 | + return () => window.removeEventListener("wallet-connected", onWalletEvent); |
| 39 | + }, []); |
| 40 | + |
| 41 | + const complete = () => { |
| 42 | + localStorage.setItem(STORAGE_KEY, "true"); |
| 43 | + setShow(false); |
| 44 | + }; |
| 45 | + |
| 46 | + const next = () => (step < TOTAL_STEPS ? setStep((s: number) => s + 1) : complete()); |
| 47 | + |
| 48 | + if (!show) return null; |
| 49 | + |
| 50 | + return ( |
| 51 | + <div |
| 52 | + role="dialog" |
| 53 | + aria-modal="true" |
| 54 | + aria-label="Recipient onboarding" |
| 55 | + className="fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" |
| 56 | + > |
| 57 | + <div className="bg-gray-900 border border-gray-800 rounded-xl w-full max-w-md p-6 flex flex-col gap-5"> |
| 58 | + {/* Progress bar */} |
| 59 | + <div className="flex gap-1.5" aria-label={`Step ${step} of ${TOTAL_STEPS}`}> |
| 60 | + {Array.from({ length: TOTAL_STEPS }, (_, i) => ( |
| 61 | + <div |
| 62 | + key={i} |
| 63 | + className={`h-1 flex-1 rounded-full transition-colors ${ |
| 64 | + i < step ? "bg-indigo-500" : "bg-gray-700" |
| 65 | + }`} |
| 66 | + /> |
| 67 | + ))} |
| 68 | + </div> |
| 69 | + |
| 70 | + {/* Step 1 — You have incoming payments */} |
| 71 | + {step === 1 && ( |
| 72 | + <> |
| 73 | + <div className="flex flex-col gap-2"> |
| 74 | + <h2 className="text-lg font-bold">You have incoming payments 🎉</h2> |
| 75 | + <p className="text-sm text-gray-400"> |
| 76 | + Your wallet is listed as a recipient on{" "} |
| 77 | + <span className="text-white font-semibold"> |
| 78 | + {invoiceCount} invoice{invoiceCount !== 1 ? "s" : ""} |
| 79 | + </span> |
| 80 | + . StellarSplit is an on-chain invoicing tool on Stellar — when an invoice is fully |
| 81 | + funded, USDC is automatically routed to you. |
| 82 | + </p> |
| 83 | + </div> |
| 84 | + <StepActions step={step} total={TOTAL_STEPS} onNext={next} onSkip={complete} /> |
| 85 | + </> |
| 86 | + )} |
| 87 | + |
| 88 | + {/* Step 2 — How release works */} |
| 89 | + {step === 2 && ( |
| 90 | + <> |
| 91 | + <div className="flex flex-col gap-2"> |
| 92 | + <h2 className="text-lg font-bold">How payment release works</h2> |
| 93 | + <ul className="text-sm text-gray-400 space-y-2 list-none"> |
| 94 | + <li className="flex gap-2"> |
| 95 | + <span className="text-indigo-400 shrink-0">1.</span> |
| 96 | + Payers fund their share of the invoice in USDC. |
| 97 | + </li> |
| 98 | + <li className="flex gap-2"> |
| 99 | + <span className="text-indigo-400 shrink-0">2.</span> |
| 100 | + Once the invoice is 100% funded, the smart contract releases funds automatically. |
| 101 | + </li> |
| 102 | + <li className="flex gap-2"> |
| 103 | + <span className="text-indigo-400 shrink-0">3.</span> |
| 104 | + Your share lands directly in your Stellar wallet — no action needed from you. |
| 105 | + </li> |
| 106 | + </ul> |
| 107 | + </div> |
| 108 | + <StepActions step={step} total={TOTAL_STEPS} onNext={next} onSkip={complete} /> |
| 109 | + </> |
| 110 | + )} |
| 111 | + |
| 112 | + {/* Step 3 — Go to recipient portal */} |
| 113 | + {step === 3 && ( |
| 114 | + <> |
| 115 | + <div className="flex flex-col gap-2"> |
| 116 | + <h2 className="text-lg font-bold">Track your payments</h2> |
| 117 | + <p className="text-sm text-gray-400"> |
| 118 | + Head to your recipient portal to see all invoices you're part of, track funding |
| 119 | + progress, and view your payment history. |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + <a |
| 123 | + href="/recipient" |
| 124 | + onClick={complete} |
| 125 | + className="w-full text-center px-4 py-2.5 rounded-lg bg-indigo-600 hover:bg-indigo-500 text-sm font-semibold transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-400" |
| 126 | + > |
| 127 | + Go to Recipient Portal → |
| 128 | + </a> |
| 129 | + <button |
| 130 | + onClick={complete} |
| 131 | + className="w-full px-4 py-2 rounded-lg bg-gray-800 hover:bg-gray-700 text-sm font-semibold transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-500" |
| 132 | + > |
| 133 | + Maybe later |
| 134 | + </button> |
| 135 | + </> |
| 136 | + )} |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +function StepActions({ |
| 143 | + step, |
| 144 | + total, |
| 145 | + onNext, |
| 146 | + onSkip, |
| 147 | +}: { |
| 148 | + step: number; |
| 149 | + total: number; |
| 150 | + onNext: () => void; |
| 151 | + onSkip: () => void; |
| 152 | +}) { |
| 153 | + return ( |
| 154 | + <div className="flex gap-2 mt-1"> |
| 155 | + <button |
| 156 | + onClick={onSkip} |
| 157 | + className="flex-1 px-4 py-2 rounded-lg bg-gray-800 hover:bg-gray-700 text-sm font-semibold transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-500" |
| 158 | + > |
| 159 | + Skip |
| 160 | + </button> |
| 161 | + <button |
| 162 | + onClick={onNext} |
| 163 | + className="flex-1 px-4 py-2 rounded-lg bg-indigo-600 hover:bg-indigo-500 text-sm font-semibold transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-400" |
| 164 | + > |
| 165 | + {step === total - 1 ? "Next" : "Next"} |
| 166 | + </button> |
| 167 | + </div> |
| 168 | + ); |
| 169 | +} |
0 commit comments