@nairaramp/react 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,12 +1,38 @@
1
1
  "use client";
2
2
 
3
+ // src/NairaRampContext.tsx
4
+ import {
5
+ createContext,
6
+ useContext
7
+ } from "react";
8
+ import { jsx } from "react/jsx-runtime";
9
+ var NairaRampContext = createContext(void 0);
10
+ function NairaRampProvider({
11
+ ramp,
12
+ children
13
+ }) {
14
+ return /* @__PURE__ */ jsx(NairaRampContext.Provider, { value: ramp, children });
15
+ }
16
+ function useNairaRamp(overrideRamp) {
17
+ const contextRamp = useContext(NairaRampContext);
18
+ const resolved = overrideRamp ?? contextRamp;
19
+ if (!resolved) {
20
+ throw new Error(
21
+ "No Ramp instance found. Pass a `ramp` prop/option or wrap your component tree in <NairaRampProvider>."
22
+ );
23
+ }
24
+ return resolved;
25
+ }
26
+
3
27
  // src/usePayWithNaira.ts
4
28
  import { useState, useCallback, useRef, useEffect } from "react";
5
29
  import {
6
30
  TERMINAL_STATUSES,
7
- NairaRampError,
8
- isNairaRampError
31
+ NairaRampError as NairaRampError2
9
32
  } from "@nairaramp/core";
33
+
34
+ // src/utils.ts
35
+ import { NairaRampError, isNairaRampError } from "@nairaramp/core";
10
36
  function ensureNairaRampError(err) {
11
37
  if (isNairaRampError(err)) {
12
38
  return err;
@@ -14,18 +40,39 @@ function ensureNairaRampError(err) {
14
40
  const message = err instanceof Error ? err.message : String(err);
15
41
  return new NairaRampError("TRANSACTION_FAILED", message, { cause: err });
16
42
  }
43
+ function isSafeHttpUrl(rawUrl) {
44
+ try {
45
+ const parsed = new URL(rawUrl);
46
+ return parsed.protocol === "https:" || parsed.protocol === "http:";
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+ function getStellarExplorerUrl(txHash, network = "testnet") {
52
+ if (!txHash || !/^[0-9a-fA-F]{64}$/.test(txHash)) {
53
+ return void 0;
54
+ }
55
+ const segment = network === "mainnet" ? "public" : "testnet";
56
+ return `https://stellar.expert/explorer/${segment}/tx/${txHash}`;
57
+ }
58
+
59
+ // src/usePayWithNaira.ts
17
60
  function usePayWithNaira({
18
- ramp,
61
+ ramp: rampOption,
19
62
  assetCode
20
63
  }) {
64
+ const ramp = useNairaRamp(rampOption);
21
65
  const [phase, setPhase] = useState("idle");
22
66
  const [status, setStatus] = useState(void 0);
23
67
  const [transactionId, setTransactionId] = useState(void 0);
68
+ const [transaction, setTransaction] = useState(void 0);
69
+ const [stellarTransactionId, setStellarTransactionId] = useState(void 0);
24
70
  const [url, setUrl] = useState(void 0);
25
71
  const [error, setError] = useState(void 0);
26
72
  const phaseRef = useRef("idle");
27
73
  const subscriptionRef = useRef(void 0);
28
74
  const isMountedRef = useRef(true);
75
+ const attemptIdRef = useRef(0);
29
76
  useEffect(() => {
30
77
  phaseRef.current = phase;
31
78
  }, [phase]);
@@ -36,12 +83,15 @@ function usePayWithNaira({
36
83
  }
37
84
  }, []);
38
85
  const reset = useCallback(() => {
86
+ attemptIdRef.current += 1;
39
87
  stopWatcher();
40
88
  phaseRef.current = "idle";
41
89
  if (isMountedRef.current) {
42
90
  setPhase("idle");
43
91
  setStatus(void 0);
44
92
  setTransactionId(void 0);
93
+ setTransaction(void 0);
94
+ setStellarTransactionId(void 0);
45
95
  setUrl(void 0);
46
96
  setError(void 0);
47
97
  }
@@ -51,9 +101,12 @@ function usePayWithNaira({
51
101
  if (phaseRef.current !== "idle") {
52
102
  return void 0;
53
103
  }
104
+ const attemptId = ++attemptIdRef.current;
54
105
  phaseRef.current = "starting";
55
106
  if (isMountedRef.current) {
56
107
  setPhase("starting");
108
+ setTransaction(void 0);
109
+ setStellarTransactionId(void 0);
57
110
  setError(void 0);
58
111
  }
59
112
  let depositResult;
@@ -63,15 +116,16 @@ function usePayWithNaira({
63
116
  ...amount !== void 0 ? { amount } : {}
64
117
  });
65
118
  } catch (err) {
119
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
120
+ return void 0;
121
+ }
66
122
  const wrapped = ensureNairaRampError(err);
67
123
  phaseRef.current = "failed";
68
- if (isMountedRef.current) {
69
- setError(wrapped);
70
- setPhase("failed");
71
- }
124
+ setError(wrapped);
125
+ setPhase("failed");
72
126
  return void 0;
73
127
  }
74
- if (!isMountedRef.current) {
128
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
75
129
  return void 0;
76
130
  }
77
131
  const { id, url: hostedUrl } = depositResult;
@@ -81,13 +135,23 @@ function usePayWithNaira({
81
135
  setPhase("awaiting_user");
82
136
  stopWatcher();
83
137
  const subscription = ramp.watch(id, {
84
- onStatus: (nextStatus) => {
85
- if (!isMountedRef.current) return;
138
+ onStatus: (nextStatus, tx) => {
139
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
86
140
  setStatus(nextStatus);
141
+ if (tx) {
142
+ setTransaction(tx);
143
+ if (tx.stellar_transaction_id) {
144
+ setStellarTransactionId(tx.stellar_transaction_id);
145
+ }
146
+ }
87
147
  if (nextStatus === "completed") {
148
+ stopWatcher();
88
149
  phaseRef.current = "completed";
89
150
  setPhase("completed");
90
151
  } else if (TERMINAL_STATUSES.includes(nextStatus)) {
152
+ stopWatcher();
153
+ const detail = tx?.message ? `Transaction ${nextStatus}: ${tx.message}` : `Transaction ${nextStatus}`;
154
+ setError(new NairaRampError2("TRANSACTION_FAILED", detail));
91
155
  phaseRef.current = "failed";
92
156
  setPhase("failed");
93
157
  } else if (nextStatus === "pending_user_transfer_start" || nextStatus === "incomplete") {
@@ -98,14 +162,22 @@ function usePayWithNaira({
98
162
  setPhase("processing");
99
163
  }
100
164
  },
101
- onComplete: () => {
102
- if (!isMountedRef.current) return;
165
+ onComplete: (tx) => {
166
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
167
+ stopWatcher();
168
+ if (tx) {
169
+ setTransaction(tx);
170
+ if (tx.stellar_transaction_id) {
171
+ setStellarTransactionId(tx.stellar_transaction_id);
172
+ }
173
+ }
103
174
  setStatus("completed");
104
175
  phaseRef.current = "completed";
105
176
  setPhase("completed");
106
177
  },
107
178
  onError: (err) => {
108
- if (!isMountedRef.current) return;
179
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
180
+ stopWatcher();
109
181
  const wrapped = ensureNairaRampError(err);
110
182
  setError(wrapped);
111
183
  phaseRef.current = "failed";
@@ -121,6 +193,7 @@ function usePayWithNaira({
121
193
  isMountedRef.current = true;
122
194
  return () => {
123
195
  isMountedRef.current = false;
196
+ attemptIdRef.current += 1;
124
197
  stopWatcher();
125
198
  };
126
199
  }, [stopWatcher]);
@@ -129,6 +202,8 @@ function usePayWithNaira({
129
202
  phase,
130
203
  status,
131
204
  transactionId,
205
+ transaction,
206
+ stellarTransactionId,
132
207
  url,
133
208
  error,
134
209
  reset
@@ -137,16 +212,101 @@ function usePayWithNaira({
137
212
 
138
213
  // src/PayWithNaira.tsx
139
214
  import {
140
- useState as useState2,
141
- useRef as useRef2,
142
- useEffect as useEffect2,
143
- useCallback as useCallback2
215
+ useState as useState4,
216
+ useRef as useRef3,
217
+ useEffect as useEffect4,
218
+ useCallback as useCallback3
144
219
  } from "react";
145
220
  import {
146
221
  NairaRampError as NairaRampErrorClass
147
222
  } from "@nairaramp/core";
148
- import { jsx, jsxs } from "react/jsx-runtime";
149
- var containerStyle = {
223
+
224
+ // src/theme.ts
225
+ import { useState as useState2, useEffect as useEffect2 } from "react";
226
+ var LIGHT_THEME = {
227
+ text: "#111827",
228
+ secondaryText: "#1f2937",
229
+ mutedText: "#4b5563",
230
+ pendingText: "#6b7280",
231
+ doneText: "#059669",
232
+ primaryBtnBg: "#059669",
233
+ primaryBtnText: "#ffffff",
234
+ disabledBtnBg: "#6b7280",
235
+ disabledBtnText: "#ffffff",
236
+ retryBtnBg: "#f3f4f6",
237
+ retryBtnText: "#1f2937",
238
+ retryBtnBorder: "#d1d5db",
239
+ errorText: "#b91c1c",
240
+ linkText: "#2563eb",
241
+ failureBg: "#fef2f2",
242
+ failureBorder: "#fee2e2",
243
+ failureTitle: "#991b1b",
244
+ activeBadgeBg: "#2563eb",
245
+ doneBadgeBg: "#059669",
246
+ activeOrDoneBadgeText: "#ffffff",
247
+ pendingBadgeBg: "#e5e7eb",
248
+ pendingBadgeText: "#374151",
249
+ focusPrimary: "#047857",
250
+ focusAccent: "#2563eb"
251
+ };
252
+ var DARK_THEME = {
253
+ text: "#f9fafb",
254
+ secondaryText: "#e5e7eb",
255
+ mutedText: "#d1d5db",
256
+ pendingText: "#9ca3af",
257
+ doneText: "#34d399",
258
+ primaryBtnBg: "#059669",
259
+ primaryBtnText: "#ffffff",
260
+ disabledBtnBg: "#4b5563",
261
+ disabledBtnText: "#ffffff",
262
+ retryBtnBg: "#1f2937",
263
+ retryBtnText: "#f9fafb",
264
+ retryBtnBorder: "#374151",
265
+ errorText: "#fca5a5",
266
+ linkText: "#60a5fa",
267
+ failureBg: "#450a0a",
268
+ failureBorder: "#7f1d1d",
269
+ failureTitle: "#fecaca",
270
+ activeBadgeBg: "#3b82f6",
271
+ doneBadgeBg: "#059669",
272
+ activeOrDoneBadgeText: "#ffffff",
273
+ pendingBadgeBg: "#374151",
274
+ pendingBadgeText: "#e5e7eb",
275
+ focusPrimary: "#34d399",
276
+ focusAccent: "#60a5fa"
277
+ };
278
+ function getSystemDarkPreference() {
279
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
280
+ return false;
281
+ }
282
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
283
+ }
284
+ function useResolvedTheme(theme = "system") {
285
+ const [systemDark, setSystemDark] = useState2(
286
+ () => theme === "system" ? getSystemDarkPreference() : false
287
+ );
288
+ useEffect2(() => {
289
+ if (theme !== "system" || typeof window === "undefined" || typeof window.matchMedia !== "function") {
290
+ return;
291
+ }
292
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
293
+ setSystemDark(mediaQuery.matches);
294
+ const handleChange = (event) => {
295
+ setSystemDark(event.matches);
296
+ };
297
+ mediaQuery.addEventListener("change", handleChange);
298
+ return () => {
299
+ mediaQuery.removeEventListener("change", handleChange);
300
+ };
301
+ }, [theme]);
302
+ if (theme === "dark") return DARK_THEME;
303
+ if (theme === "light") return LIGHT_THEME;
304
+ return systemDark ? DARK_THEME : LIGHT_THEME;
305
+ }
306
+
307
+ // src/useAnchorPopup.ts
308
+ import { useState as useState3, useRef as useRef2, useCallback as useCallback2, useEffect as useEffect3 } from "react";
309
+ var rampContainerStyle = {
150
310
  width: "100%",
151
311
  maxWidth: "420px",
152
312
  boxSizing: "border-box",
@@ -169,19 +329,19 @@ var baseButtonStyle = {
169
329
  display: "inline-block",
170
330
  transition: "outline 0.15s ease"
171
331
  };
172
- var primaryButtonStyle = {
332
+ var rampPrimaryButtonStyle = {
173
333
  ...baseButtonStyle,
174
334
  color: "#ffffff",
175
335
  backgroundColor: "#059669"
176
336
  };
177
- var disabledButtonStyle = {
337
+ var rampDisabledButtonStyle = {
178
338
  ...baseButtonStyle,
179
339
  color: "#ffffff",
180
340
  backgroundColor: "#9ca3af",
181
341
  cursor: "not-allowed",
182
342
  opacity: 0.8
183
343
  };
184
- var retryButtonStyle = {
344
+ var rampRetryButtonStyle = {
185
345
  ...baseButtonStyle,
186
346
  marginTop: "12px",
187
347
  padding: "10px 16px",
@@ -190,20 +350,20 @@ var retryButtonStyle = {
190
350
  backgroundColor: "#f3f4f6",
191
351
  border: "1px solid #d1d5db"
192
352
  };
193
- var statusTextStyle = {
353
+ var rampStatusTextStyle = {
194
354
  margin: "8px 0",
195
355
  fontSize: "16px",
196
356
  fontWeight: 500,
197
357
  color: "#1f2937"
198
358
  };
199
- var errorTextStyle = {
359
+ var rampErrorTextStyle = {
200
360
  margin: "8px 0",
201
361
  fontSize: "15px",
202
362
  fontWeight: 500,
203
363
  color: "#b91c1c",
204
364
  wordBreak: "break-word"
205
365
  };
206
- var linkStyle = {
366
+ var rampLinkStyle = {
207
367
  display: "inline-block",
208
368
  fontSize: "15px",
209
369
  fontWeight: 600,
@@ -212,33 +372,9 @@ var linkStyle = {
212
372
  wordBreak: "break-all",
213
373
  cursor: "pointer"
214
374
  };
215
- function PayWithNaira({
216
- ramp,
217
- assetCode,
218
- amount,
219
- onComplete,
220
- onError,
221
- label = "Pay with Naira"
222
- }) {
223
- const { start, phase, error, url, reset } = usePayWithNaira({
224
- ramp,
225
- assetCode
226
- });
227
- const [popupBlocked, setPopupBlocked] = useState2(false);
228
- const [isPrimaryFocused, setIsPrimaryFocused] = useState2(false);
229
- const [isRetryFocused, setIsRetryFocused] = useState2(false);
230
- const [isLinkFocused, setIsLinkFocused] = useState2(false);
375
+ function useAnchorPopup() {
376
+ const [popupBlocked, setPopupBlocked] = useState3(false);
231
377
  const popupRef = useRef2(null);
232
- const onCompleteRef = useRef2(onComplete);
233
- const onErrorRef = useRef2(onError);
234
- const completedFiredRef = useRef2(false);
235
- const errorFiredRef = useRef2(false);
236
- useEffect2(() => {
237
- onCompleteRef.current = onComplete;
238
- }, [onComplete]);
239
- useEffect2(() => {
240
- onErrorRef.current = onError;
241
- }, [onError]);
242
378
  const closePopup = useCallback2(() => {
243
379
  if (popupRef.current && !popupRef.current.closed) {
244
380
  try {
@@ -248,39 +384,102 @@ function PayWithNaira({
248
384
  popupRef.current = null;
249
385
  }
250
386
  }, []);
251
- const handleClick = useCallback2(async () => {
387
+ const openPopup = useCallback2(() => {
252
388
  setPopupBlocked(false);
253
389
  let popup = null;
254
- try {
255
- popup = window.open("", "nairaramp_anchor");
256
- } catch {
257
- popup = null;
390
+ if (typeof window !== "undefined" && typeof window.open === "function") {
391
+ try {
392
+ popup = window.open("", "nairaramp_anchor");
393
+ } catch {
394
+ popup = null;
395
+ }
258
396
  }
259
397
  popupRef.current = popup;
260
398
  if (!popup) {
261
399
  setPopupBlocked(true);
262
400
  }
263
- try {
264
- const result = await start(amount);
265
- if (result?.url) {
401
+ return popup;
402
+ }, []);
403
+ const navigatePopup = useCallback2(
404
+ (popup, targetUrl) => {
405
+ if (targetUrl && isSafeHttpUrl(targetUrl)) {
266
406
  if (popup && !popup.closed) {
267
- popup.location.href = result.url;
407
+ popup.location.href = targetUrl;
268
408
  }
269
409
  } else {
270
410
  closePopup();
271
411
  }
272
- } catch {
273
- closePopup();
274
- }
275
- }, [start, amount, closePopup]);
276
- const handleRetry = useCallback2(() => {
412
+ },
413
+ [closePopup]
414
+ );
415
+ const clearPopupBlocked = useCallback2(() => {
277
416
  setPopupBlocked(false);
417
+ }, []);
418
+ useEffect3(() => {
419
+ return () => {
420
+ closePopup();
421
+ };
422
+ }, [closePopup]);
423
+ return {
424
+ popupBlocked,
425
+ openPopup,
426
+ navigatePopup,
427
+ closePopup,
428
+ clearPopupBlocked
429
+ };
430
+ }
431
+
432
+ // src/PayWithNaira.tsx
433
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
434
+ function PayWithNaira({
435
+ ramp,
436
+ assetCode,
437
+ amount,
438
+ onComplete,
439
+ onError,
440
+ label = "Pay with Naira",
441
+ theme = "system",
442
+ className,
443
+ style
444
+ }) {
445
+ const tokens = useResolvedTheme(theme);
446
+ const { start, phase, error, url, reset } = usePayWithNaira({
447
+ ramp,
448
+ assetCode
449
+ });
450
+ const {
451
+ popupBlocked,
452
+ openPopup,
453
+ navigatePopup,
454
+ closePopup,
455
+ clearPopupBlocked
456
+ } = useAnchorPopup();
457
+ const [isPrimaryFocused, setIsPrimaryFocused] = useState4(false);
458
+ const [isRetryFocused, setIsRetryFocused] = useState4(false);
459
+ const [isLinkFocused, setIsLinkFocused] = useState4(false);
460
+ const onCompleteRef = useRef3(onComplete);
461
+ const onErrorRef = useRef3(onError);
462
+ const completedFiredRef = useRef3(false);
463
+ const errorFiredRef = useRef3(false);
464
+ useEffect4(() => {
465
+ onCompleteRef.current = onComplete;
466
+ }, [onComplete]);
467
+ useEffect4(() => {
468
+ onErrorRef.current = onError;
469
+ }, [onError]);
470
+ const handleClick = useCallback3(async () => {
471
+ const popup = openPopup();
472
+ const result = await start(amount);
473
+ navigatePopup(popup, result?.url);
474
+ }, [openPopup, start, amount, navigatePopup]);
475
+ const handleRetry = useCallback3(() => {
476
+ clearPopupBlocked();
278
477
  closePopup();
279
478
  completedFiredRef.current = false;
280
479
  errorFiredRef.current = false;
281
480
  reset();
282
- }, [reset, closePopup]);
283
- useEffect2(() => {
481
+ }, [clearPopupBlocked, closePopup, reset]);
482
+ useEffect4(() => {
284
483
  if (phase === "completed") {
285
484
  if (!completedFiredRef.current) {
286
485
  completedFiredRef.current = true;
@@ -298,8 +497,8 @@ function PayWithNaira({
298
497
  errorFiredRef.current = false;
299
498
  }
300
499
  }, [phase, error, closePopup]);
301
- return /* @__PURE__ */ jsxs("div", { style: containerStyle, children: [
302
- phase === "idle" && /* @__PURE__ */ jsx(
500
+ return /* @__PURE__ */ jsxs("div", { className, style: { ...rampContainerStyle, color: tokens.text, ...style }, children: [
501
+ phase === "idle" && /* @__PURE__ */ jsx2(
303
502
  "button",
304
503
  {
305
504
  type: "button",
@@ -307,26 +506,32 @@ function PayWithNaira({
307
506
  onFocus: () => setIsPrimaryFocused(true),
308
507
  onBlur: () => setIsPrimaryFocused(false),
309
508
  style: {
310
- ...primaryButtonStyle,
311
- outline: isPrimaryFocused ? "3px solid #047857" : "2px solid transparent",
509
+ ...rampPrimaryButtonStyle,
510
+ color: tokens.primaryBtnText,
511
+ backgroundColor: tokens.primaryBtnBg,
512
+ outline: isPrimaryFocused ? `3px solid ${tokens.focusPrimary}` : "2px solid transparent",
312
513
  outlineOffset: "2px"
313
514
  },
314
515
  children: label
315
516
  }
316
517
  ),
317
518
  /* @__PURE__ */ jsxs("div", { "aria-live": "polite", "aria-atomic": "true", children: [
318
- phase === "starting" && /* @__PURE__ */ jsx(
519
+ phase === "starting" && /* @__PURE__ */ jsx2(
319
520
  "button",
320
521
  {
321
522
  type: "button",
322
523
  disabled: true,
323
- style: disabledButtonStyle,
524
+ style: {
525
+ ...rampDisabledButtonStyle,
526
+ color: tokens.disabledBtnText,
527
+ backgroundColor: tokens.disabledBtnBg
528
+ },
324
529
  children: "Connecting..."
325
530
  }
326
531
  ),
327
532
  phase === "awaiting_user" && /* @__PURE__ */ jsxs("div", { children: [
328
- /* @__PURE__ */ jsx("p", { style: statusTextStyle, children: "Complete the steps in the anchor window" }),
329
- popupBlocked && url && /* @__PURE__ */ jsx("p", { style: { margin: "8px 0 0 0" }, children: /* @__PURE__ */ jsx(
533
+ /* @__PURE__ */ jsx2("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Complete the steps in the anchor window" }),
534
+ popupBlocked && url && isSafeHttpUrl(url) && /* @__PURE__ */ jsx2("p", { style: { margin: "8px 0 0 0" }, children: /* @__PURE__ */ jsx2(
330
535
  "a",
331
536
  {
332
537
  href: url,
@@ -335,19 +540,364 @@ function PayWithNaira({
335
540
  onFocus: () => setIsLinkFocused(true),
336
541
  onBlur: () => setIsLinkFocused(false),
337
542
  style: {
338
- ...linkStyle,
339
- outline: isLinkFocused ? "2px solid #2563eb" : "2px solid transparent",
543
+ ...rampLinkStyle,
544
+ color: tokens.linkText,
545
+ outline: isLinkFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
340
546
  outlineOffset: "2px"
341
547
  },
342
548
  children: "Continue to the anchor"
343
549
  }
344
550
  ) })
345
551
  ] }),
346
- phase === "processing" && /* @__PURE__ */ jsx("p", { style: statusTextStyle, children: "Processing your payment" }),
347
- phase === "completed" && /* @__PURE__ */ jsx("p", { style: statusTextStyle, children: "Payment completed" }),
552
+ phase === "processing" && /* @__PURE__ */ jsx2("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Processing your payment" }),
553
+ phase === "completed" && /* @__PURE__ */ jsx2("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Payment completed" }),
348
554
  phase === "failed" && /* @__PURE__ */ jsxs("div", { children: [
349
- /* @__PURE__ */ jsx("p", { style: errorTextStyle, children: error?.message ?? "Payment failed" }),
350
- /* @__PURE__ */ jsx(
555
+ /* @__PURE__ */ jsx2("p", { style: { ...rampErrorTextStyle, color: tokens.errorText }, children: error?.message ?? "Payment failed" }),
556
+ /* @__PURE__ */ jsx2(
557
+ "button",
558
+ {
559
+ type: "button",
560
+ onClick: handleRetry,
561
+ onFocus: () => setIsRetryFocused(true),
562
+ onBlur: () => setIsRetryFocused(false),
563
+ style: {
564
+ ...rampRetryButtonStyle,
565
+ color: tokens.retryBtnText,
566
+ backgroundColor: tokens.retryBtnBg,
567
+ border: `1px solid ${tokens.retryBtnBorder}`,
568
+ outline: isRetryFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
569
+ outlineOffset: "2px"
570
+ },
571
+ children: "Try again"
572
+ }
573
+ )
574
+ ] })
575
+ ] })
576
+ ] });
577
+ }
578
+
579
+ // src/useCashOutToNaira.ts
580
+ import { useState as useState5, useCallback as useCallback4, useRef as useRef4, useEffect as useEffect5 } from "react";
581
+ import {
582
+ TERMINAL_STATUSES as TERMINAL_STATUSES2,
583
+ NairaRampError as NairaRampError3
584
+ } from "@nairaramp/core";
585
+ function useCashOutToNaira({
586
+ ramp: rampOption,
587
+ assetCode
588
+ }) {
589
+ const ramp = useNairaRamp(rampOption);
590
+ const [phase, setPhase] = useState5("idle");
591
+ const [status, setStatus] = useState5(void 0);
592
+ const [transactionId, setTransactionId] = useState5(void 0);
593
+ const [transaction, setTransaction] = useState5(void 0);
594
+ const [url, setUrl] = useState5(void 0);
595
+ const [txHash, setTxHash] = useState5(void 0);
596
+ const [error, setError] = useState5(void 0);
597
+ const phaseRef = useRef4("idle");
598
+ const subscriptionRef = useRef4(void 0);
599
+ const isMountedRef = useRef4(true);
600
+ const completionStartedRef = useRef4(false);
601
+ const attemptIdRef = useRef4(0);
602
+ useEffect5(() => {
603
+ phaseRef.current = phase;
604
+ }, [phase]);
605
+ const stopWatcher = useCallback4(() => {
606
+ if (subscriptionRef.current) {
607
+ subscriptionRef.current.stop();
608
+ subscriptionRef.current = void 0;
609
+ }
610
+ }, []);
611
+ const reset = useCallback4(() => {
612
+ attemptIdRef.current += 1;
613
+ stopWatcher();
614
+ phaseRef.current = "idle";
615
+ completionStartedRef.current = false;
616
+ if (isMountedRef.current) {
617
+ setPhase("idle");
618
+ setStatus(void 0);
619
+ setTransactionId(void 0);
620
+ setTransaction(void 0);
621
+ setUrl(void 0);
622
+ setTxHash(void 0);
623
+ setError(void 0);
624
+ }
625
+ }, [stopWatcher]);
626
+ const start = useCallback4(
627
+ async (amount) => {
628
+ if (phaseRef.current !== "idle") {
629
+ return void 0;
630
+ }
631
+ const attemptId = ++attemptIdRef.current;
632
+ completionStartedRef.current = false;
633
+ phaseRef.current = "starting";
634
+ if (isMountedRef.current) {
635
+ setPhase("starting");
636
+ setTransaction(void 0);
637
+ setError(void 0);
638
+ setTxHash(void 0);
639
+ }
640
+ let withdrawResult;
641
+ try {
642
+ withdrawResult = await ramp.withdraw({
643
+ assetCode,
644
+ ...amount !== void 0 ? { amount } : {}
645
+ });
646
+ } catch (err) {
647
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
648
+ return void 0;
649
+ }
650
+ const wrapped = ensureNairaRampError(err);
651
+ phaseRef.current = "failed";
652
+ setError(wrapped);
653
+ setPhase("failed");
654
+ return void 0;
655
+ }
656
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
657
+ return void 0;
658
+ }
659
+ const { id, url: hostedUrl } = withdrawResult;
660
+ setTransactionId(id);
661
+ setUrl(hostedUrl);
662
+ phaseRef.current = "awaiting_user";
663
+ setPhase("awaiting_user");
664
+ stopWatcher();
665
+ const subscription = ramp.watch(id, {
666
+ onStatus: (nextStatus, tx) => {
667
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
668
+ setStatus(nextStatus);
669
+ if (tx) {
670
+ setTransaction(tx);
671
+ if (tx.stellar_transaction_id) {
672
+ setTxHash((prev) => prev ?? tx.stellar_transaction_id);
673
+ }
674
+ }
675
+ if (nextStatus === "completed") {
676
+ stopWatcher();
677
+ phaseRef.current = "completed";
678
+ setPhase("completed");
679
+ return;
680
+ }
681
+ if (TERMINAL_STATUSES2.includes(nextStatus)) {
682
+ stopWatcher();
683
+ const detail = tx?.message ? `Transaction ${nextStatus}: ${tx.message}` : `Transaction ${nextStatus}`;
684
+ setError(new NairaRampError3("TRANSACTION_FAILED", detail));
685
+ phaseRef.current = "failed";
686
+ setPhase("failed");
687
+ return;
688
+ }
689
+ if (nextStatus === "pending_user_transfer_start") {
690
+ if (!completionStartedRef.current) {
691
+ completionStartedRef.current = true;
692
+ phaseRef.current = "completing";
693
+ setPhase("completing");
694
+ ramp.completeWithdrawal(id).then(({ hash }) => {
695
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
696
+ setTxHash(hash);
697
+ if (phaseRef.current === "completing") {
698
+ phaseRef.current = "processing";
699
+ setPhase("processing");
700
+ }
701
+ }).catch((err) => {
702
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
703
+ stopWatcher();
704
+ setError(ensureNairaRampError(err));
705
+ phaseRef.current = "failed";
706
+ setPhase("failed");
707
+ });
708
+ }
709
+ return;
710
+ }
711
+ if (nextStatus === "incomplete" || nextStatus === "pending_user") {
712
+ phaseRef.current = "awaiting_user";
713
+ setPhase("awaiting_user");
714
+ } else {
715
+ phaseRef.current = "processing";
716
+ setPhase("processing");
717
+ }
718
+ },
719
+ onComplete: (tx) => {
720
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
721
+ stopWatcher();
722
+ if (tx) {
723
+ setTransaction(tx);
724
+ if (tx.stellar_transaction_id) {
725
+ setTxHash((prev) => prev ?? tx.stellar_transaction_id);
726
+ }
727
+ }
728
+ setStatus("completed");
729
+ phaseRef.current = "completed";
730
+ setPhase("completed");
731
+ },
732
+ onError: (err) => {
733
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
734
+ stopWatcher();
735
+ setError(ensureNairaRampError(err));
736
+ phaseRef.current = "failed";
737
+ setPhase("failed");
738
+ }
739
+ });
740
+ subscriptionRef.current = subscription;
741
+ return withdrawResult;
742
+ },
743
+ [ramp, assetCode, stopWatcher]
744
+ );
745
+ useEffect5(() => {
746
+ isMountedRef.current = true;
747
+ return () => {
748
+ isMountedRef.current = false;
749
+ attemptIdRef.current += 1;
750
+ stopWatcher();
751
+ };
752
+ }, [stopWatcher]);
753
+ return {
754
+ start,
755
+ phase,
756
+ status,
757
+ transactionId,
758
+ transaction,
759
+ url,
760
+ txHash,
761
+ error,
762
+ reset
763
+ };
764
+ }
765
+
766
+ // src/CashOutToNaira.tsx
767
+ import {
768
+ useState as useState6,
769
+ useRef as useRef5,
770
+ useEffect as useEffect6,
771
+ useCallback as useCallback5
772
+ } from "react";
773
+ import {
774
+ NairaRampError as NairaRampErrorClass2
775
+ } from "@nairaramp/core";
776
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
777
+ function CashOutToNaira({
778
+ ramp,
779
+ assetCode,
780
+ amount,
781
+ onComplete,
782
+ onError,
783
+ label = "Cash out to Naira",
784
+ theme = "system",
785
+ className,
786
+ style
787
+ }) {
788
+ const tokens = useResolvedTheme(theme);
789
+ const { start, phase, error, url, txHash, reset } = useCashOutToNaira({
790
+ ramp,
791
+ assetCode
792
+ });
793
+ const {
794
+ popupBlocked,
795
+ openPopup,
796
+ navigatePopup,
797
+ closePopup,
798
+ clearPopupBlocked
799
+ } = useAnchorPopup();
800
+ const [isPrimaryFocused, setIsPrimaryFocused] = useState6(false);
801
+ const [isRetryFocused, setIsRetryFocused] = useState6(false);
802
+ const [isLinkFocused, setIsLinkFocused] = useState6(false);
803
+ const onCompleteRef = useRef5(onComplete);
804
+ const onErrorRef = useRef5(onError);
805
+ const completedFiredRef = useRef5(false);
806
+ const errorFiredRef = useRef5(false);
807
+ useEffect6(() => {
808
+ onCompleteRef.current = onComplete;
809
+ }, [onComplete]);
810
+ useEffect6(() => {
811
+ onErrorRef.current = onError;
812
+ }, [onError]);
813
+ const handleClick = useCallback5(async () => {
814
+ const popup = openPopup();
815
+ const result = await start(amount);
816
+ navigatePopup(popup, result?.url);
817
+ }, [openPopup, start, amount, navigatePopup]);
818
+ const handleRetry = useCallback5(() => {
819
+ clearPopupBlocked();
820
+ closePopup();
821
+ completedFiredRef.current = false;
822
+ errorFiredRef.current = false;
823
+ reset();
824
+ }, [clearPopupBlocked, closePopup, reset]);
825
+ useEffect6(() => {
826
+ if (phase === "completed") {
827
+ if (!completedFiredRef.current) {
828
+ completedFiredRef.current = true;
829
+ onCompleteRef.current?.(txHash);
830
+ }
831
+ } else if (phase === "failed") {
832
+ closePopup();
833
+ if (!errorFiredRef.current) {
834
+ errorFiredRef.current = true;
835
+ const finalError = error ?? new NairaRampErrorClass2("TRANSACTION_FAILED", "Cash out failed");
836
+ onErrorRef.current?.(finalError);
837
+ }
838
+ } else if (phase === "idle") {
839
+ completedFiredRef.current = false;
840
+ errorFiredRef.current = false;
841
+ }
842
+ }, [phase, error, txHash, closePopup]);
843
+ return /* @__PURE__ */ jsxs2("div", { className, style: { ...rampContainerStyle, color: tokens.text, ...style }, children: [
844
+ phase === "idle" && /* @__PURE__ */ jsx3(
845
+ "button",
846
+ {
847
+ type: "button",
848
+ onClick: handleClick,
849
+ onFocus: () => setIsPrimaryFocused(true),
850
+ onBlur: () => setIsPrimaryFocused(false),
851
+ style: {
852
+ ...rampPrimaryButtonStyle,
853
+ color: tokens.primaryBtnText,
854
+ backgroundColor: tokens.primaryBtnBg,
855
+ outline: isPrimaryFocused ? `3px solid ${tokens.focusPrimary}` : "2px solid transparent",
856
+ outlineOffset: "2px"
857
+ },
858
+ children: label
859
+ }
860
+ ),
861
+ /* @__PURE__ */ jsxs2("div", { "aria-live": "polite", "aria-atomic": "true", children: [
862
+ phase === "starting" && /* @__PURE__ */ jsx3(
863
+ "button",
864
+ {
865
+ type: "button",
866
+ disabled: true,
867
+ style: {
868
+ ...rampDisabledButtonStyle,
869
+ color: tokens.disabledBtnText,
870
+ backgroundColor: tokens.disabledBtnBg
871
+ },
872
+ children: "Connecting..."
873
+ }
874
+ ),
875
+ phase === "awaiting_user" && /* @__PURE__ */ jsxs2("div", { children: [
876
+ /* @__PURE__ */ jsx3("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Complete the steps in the anchor window" }),
877
+ popupBlocked && url && isSafeHttpUrl(url) && /* @__PURE__ */ jsx3("p", { style: { margin: "8px 0 0 0" }, children: /* @__PURE__ */ jsx3(
878
+ "a",
879
+ {
880
+ href: url,
881
+ target: "_blank",
882
+ rel: "noopener noreferrer",
883
+ onFocus: () => setIsLinkFocused(true),
884
+ onBlur: () => setIsLinkFocused(false),
885
+ style: {
886
+ ...rampLinkStyle,
887
+ color: tokens.linkText,
888
+ outline: isLinkFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
889
+ outlineOffset: "2px"
890
+ },
891
+ children: "Continue to the anchor"
892
+ }
893
+ ) })
894
+ ] }),
895
+ phase === "completing" && /* @__PURE__ */ jsx3("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Submitting Stellar transfer..." }),
896
+ phase === "processing" && /* @__PURE__ */ jsx3("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Processing your cash out" }),
897
+ phase === "completed" && /* @__PURE__ */ jsx3("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Cash out completed" }),
898
+ phase === "failed" && /* @__PURE__ */ jsxs2("div", { children: [
899
+ /* @__PURE__ */ jsx3("p", { style: { ...rampErrorTextStyle, color: tokens.errorText }, children: error?.message ?? "Cash out failed" }),
900
+ /* @__PURE__ */ jsx3(
351
901
  "button",
352
902
  {
353
903
  type: "button",
@@ -355,8 +905,11 @@ function PayWithNaira({
355
905
  onFocus: () => setIsRetryFocused(true),
356
906
  onBlur: () => setIsRetryFocused(false),
357
907
  style: {
358
- ...retryButtonStyle,
359
- outline: isRetryFocused ? "2px solid #2563eb" : "2px solid transparent",
908
+ ...rampRetryButtonStyle,
909
+ color: tokens.retryBtnText,
910
+ backgroundColor: tokens.retryBtnBg,
911
+ border: `1px solid ${tokens.retryBtnBorder}`,
912
+ outline: isRetryFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
360
913
  outlineOffset: "2px"
361
914
  },
362
915
  children: "Try again"
@@ -368,7 +921,7 @@ function PayWithNaira({
368
921
  }
369
922
 
370
923
  // src/StatusTimeline.tsx
371
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
924
+ import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
372
925
  var TIMELINE_STEPS = [
373
926
  "Started",
374
927
  "Waiting for your payment",
@@ -377,6 +930,38 @@ var TIMELINE_STEPS = [
377
930
  "Sending on Stellar",
378
931
  "Completed"
379
932
  ];
933
+ var YORUBA_LABELS = {
934
+ Started: "B\u1EB9r\u1EB9",
935
+ "Waiting for your payment": "Nduro de owo r\u1EB9",
936
+ "Payment received": "A ti gba owo",
937
+ "Anchor processing": "Anchor n \u1E63i\u1E63\u1EB9 lori r\u1EB9",
938
+ "Sending on Stellar": "Nfi ran\u1E63\u1EB9 lori Stellar",
939
+ Completed: "Pari"
940
+ };
941
+ var HAUSA_LABELS = {
942
+ Started: "An fara",
943
+ "Waiting for your payment": "Ana jiran biyan ku",
944
+ "Payment received": "An kar\u0253i biya",
945
+ "Anchor processing": "Anchor yana sarrafawa",
946
+ "Sending on Stellar": "Ana aikawa ta Stellar",
947
+ Completed: "An kammala"
948
+ };
949
+ var IGBO_LABELS = {
950
+ Started: "Amalitela",
951
+ "Waiting for your payment": "Na-eche ugwo gi",
952
+ "Payment received": "Anatala ugwo",
953
+ "Anchor processing": "Anchor na-ar\u1EE5 \u1ECDr\u1EE5",
954
+ "Sending on Stellar": "Na-eziga na Stellar",
955
+ Completed: "Emechala"
956
+ };
957
+ var PIDGIN_LABELS = {
958
+ Started: "Don start",
959
+ "Waiting for your payment": "Dey wait for your money",
960
+ "Payment received": "We don collect the money",
961
+ "Anchor processing": "Anchor dey process am",
962
+ "Sending on Stellar": "Dey send am on Stellar",
963
+ Completed: "Don finish"
964
+ };
380
965
  var STATUS_MAP = {
381
966
  incomplete: {
382
967
  kind: "step",
@@ -448,7 +1033,7 @@ var STATUS_MAP = {
448
1033
  label: "Error"
449
1034
  }
450
1035
  };
451
- var containerStyle2 = {
1036
+ var containerStyle = {
452
1037
  width: "100%",
453
1038
  maxWidth: "420px",
454
1039
  boxSizing: "border-box",
@@ -466,7 +1051,7 @@ var listStyle = {
466
1051
  gap: "12px"
467
1052
  };
468
1053
  var failureContainerStyle = {
469
- ...containerStyle2,
1054
+ ...containerStyle,
470
1055
  padding: "16px",
471
1056
  borderRadius: "8px",
472
1057
  backgroundColor: "#fef2f2",
@@ -484,25 +1069,57 @@ var messageTextStyle = {
484
1069
  fontSize: "14px",
485
1070
  color: "#4b5563"
486
1071
  };
1072
+ function resolveStatusMapping(status) {
1073
+ if (status === void 0) {
1074
+ return {
1075
+ kind: "step",
1076
+ stepIndex: 0,
1077
+ label: "Started"
1078
+ };
1079
+ }
1080
+ if (Object.prototype.hasOwnProperty.call(STATUS_MAP, status)) {
1081
+ return STATUS_MAP[status];
1082
+ }
1083
+ return {
1084
+ kind: "failure",
1085
+ label: "Unknown status"
1086
+ };
1087
+ }
487
1088
  function StatusTimeline({
488
1089
  status,
489
1090
  message,
490
- labels
1091
+ labels,
1092
+ failureLabels,
1093
+ theme = "system",
1094
+ className,
1095
+ style
491
1096
  }) {
492
- const mapping = status !== void 0 ? STATUS_MAP[status] : {
493
- kind: "step",
494
- stepIndex: 0,
495
- label: "Started"
496
- };
1097
+ const tokens = useResolvedTheme(theme);
1098
+ const mapping = resolveStatusMapping(status);
497
1099
  if (mapping.kind === "failure") {
498
- return /* @__PURE__ */ jsxs2("div", { style: failureContainerStyle, role: "alert", children: [
499
- /* @__PURE__ */ jsx2("p", { style: failureTitleStyle, children: mapping.label }),
500
- message && /* @__PURE__ */ jsx2("p", { style: messageTextStyle, children: message })
501
- ] });
1100
+ const failureText = failureLabels?.[mapping.label] ?? mapping.label;
1101
+ return /* @__PURE__ */ jsxs3(
1102
+ "div",
1103
+ {
1104
+ className,
1105
+ style: {
1106
+ ...failureContainerStyle,
1107
+ color: tokens.text,
1108
+ backgroundColor: tokens.failureBg,
1109
+ border: `1px solid ${tokens.failureBorder}`,
1110
+ ...style
1111
+ },
1112
+ role: "alert",
1113
+ children: [
1114
+ /* @__PURE__ */ jsx4("p", { style: { ...failureTitleStyle, color: tokens.failureTitle }, children: failureText }),
1115
+ message && /* @__PURE__ */ jsx4("p", { style: { ...messageTextStyle, color: tokens.mutedText }, children: message })
1116
+ ]
1117
+ }
1118
+ );
502
1119
  }
503
1120
  const activeIndex = mapping.stepIndex;
504
- return /* @__PURE__ */ jsxs2("div", { style: containerStyle2, children: [
505
- /* @__PURE__ */ jsx2("ol", { style: listStyle, "aria-label": "Payment status timeline", children: TIMELINE_STEPS.map((step, index) => {
1121
+ return /* @__PURE__ */ jsxs3("div", { className, style: { ...containerStyle, color: tokens.text, ...style }, children: [
1122
+ /* @__PURE__ */ jsx4("ol", { style: listStyle, "aria-label": "Payment status timeline", children: TIMELINE_STEPS.map((step, index) => {
506
1123
  const isDone = index < activeIndex;
507
1124
  const isActive = index === activeIndex;
508
1125
  const displayLabel = labels?.[step] ?? step;
@@ -510,7 +1127,7 @@ function StatusTimeline({
510
1127
  display: "flex",
511
1128
  alignItems: "center",
512
1129
  gap: "12px",
513
- color: isActive ? "#111827" : isDone ? "#059669" : "#9ca3af",
1130
+ color: isActive ? tokens.text : isDone ? tokens.doneText : tokens.pendingText,
514
1131
  fontWeight: isActive ? 600 : isDone ? 500 : 400
515
1132
  };
516
1133
  const badgeStyle = {
@@ -523,33 +1140,215 @@ function StatusTimeline({
523
1140
  fontSize: "12px",
524
1141
  fontWeight: 700,
525
1142
  boxSizing: "border-box",
526
- backgroundColor: isActive ? "#2563eb" : isDone ? "#059669" : "#e5e7eb",
527
- color: isActive || isDone ? "#ffffff" : "#6b7280"
1143
+ backgroundColor: isActive ? tokens.activeBadgeBg : isDone ? tokens.doneBadgeBg : tokens.pendingBadgeBg,
1144
+ color: isActive || isDone ? tokens.activeOrDoneBadgeText : tokens.pendingBadgeText
528
1145
  };
529
- return /* @__PURE__ */ jsxs2(
1146
+ return /* @__PURE__ */ jsxs3(
530
1147
  "li",
531
1148
  {
532
1149
  "aria-current": isActive ? "step" : void 0,
533
1150
  style: itemStyle,
534
1151
  children: [
535
- /* @__PURE__ */ jsx2("span", { style: badgeStyle, "aria-hidden": "true", children: isDone ? "\u2713" : index + 1 }),
536
- /* @__PURE__ */ jsx2("span", { children: displayLabel })
1152
+ /* @__PURE__ */ jsx4("span", { style: badgeStyle, "aria-hidden": "true", children: isDone ? "\u2713" : index + 1 }),
1153
+ /* @__PURE__ */ jsx4("span", { children: displayLabel })
537
1154
  ]
538
1155
  },
539
1156
  step
540
1157
  );
541
1158
  }) }),
542
- message && /* @__PURE__ */ jsx2("p", { style: messageTextStyle, children: message })
1159
+ message && /* @__PURE__ */ jsx4("p", { style: { ...messageTextStyle, color: tokens.mutedText }, children: message })
543
1160
  ] });
544
1161
  }
545
1162
 
1163
+ // src/useRampQuote.ts
1164
+ import { useState as useState7, useCallback as useCallback6, useRef as useRef6, useEffect as useEffect7 } from "react";
1165
+ import {
1166
+ NairaRampError as NairaRampError4,
1167
+ isNairaRampError as isNairaRampError2
1168
+ } from "@nairaramp/core";
1169
+ function toQuoteError(err) {
1170
+ if (isNairaRampError2(err)) {
1171
+ return err;
1172
+ }
1173
+ const message = err instanceof Error ? err.message : String(err);
1174
+ return new NairaRampError4("QUOTE_UNAVAILABLE", message, { cause: err });
1175
+ }
1176
+ function useRampQuote({
1177
+ ramp: rampOption,
1178
+ sellAsset,
1179
+ buyAsset,
1180
+ sellAmount,
1181
+ buyAmount,
1182
+ context,
1183
+ debounceMs = 300,
1184
+ pollIntervalMs,
1185
+ enabled = true
1186
+ }) {
1187
+ const ramp = useNairaRamp(rampOption);
1188
+ const [quote, setQuote] = useState7(void 0);
1189
+ const [loading, setLoading] = useState7(false);
1190
+ const [error, setError] = useState7(void 0);
1191
+ const isMountedRef = useRef6(true);
1192
+ const requestIdRef = useRef6(0);
1193
+ const hasAmount = sellAmount !== void 0 && sellAmount.trim() !== "" || buyAmount !== void 0 && buyAmount.trim() !== "";
1194
+ const fetchQuote = useCallback6(async () => {
1195
+ if (!enabled || !hasAmount) {
1196
+ return;
1197
+ }
1198
+ const reqId = ++requestIdRef.current;
1199
+ if (isMountedRef.current) {
1200
+ setLoading(true);
1201
+ setError(void 0);
1202
+ }
1203
+ const options = {
1204
+ sellAsset,
1205
+ buyAsset,
1206
+ ...sellAmount !== void 0 && sellAmount.trim() !== "" ? { sellAmount } : {},
1207
+ ...buyAmount !== void 0 && buyAmount.trim() !== "" ? { buyAmount } : {},
1208
+ ...context !== void 0 ? { context } : {}
1209
+ };
1210
+ try {
1211
+ const nextQuote = await ramp.getPrice(options);
1212
+ if (!isMountedRef.current || reqId !== requestIdRef.current) {
1213
+ return;
1214
+ }
1215
+ setQuote(nextQuote);
1216
+ setError(void 0);
1217
+ } catch (err) {
1218
+ if (!isMountedRef.current || reqId !== requestIdRef.current) {
1219
+ return;
1220
+ }
1221
+ setError(toQuoteError(err));
1222
+ } finally {
1223
+ if (isMountedRef.current && reqId === requestIdRef.current) {
1224
+ setLoading(false);
1225
+ }
1226
+ }
1227
+ }, [ramp, sellAsset, buyAsset, sellAmount, buyAmount, context, enabled, hasAmount]);
1228
+ useEffect7(() => {
1229
+ isMountedRef.current = true;
1230
+ return () => {
1231
+ isMountedRef.current = false;
1232
+ };
1233
+ }, []);
1234
+ useEffect7(() => {
1235
+ if (!enabled || !hasAmount) {
1236
+ requestIdRef.current++;
1237
+ setQuote(void 0);
1238
+ setError(void 0);
1239
+ setLoading(false);
1240
+ return;
1241
+ }
1242
+ const delay = Math.max(0, debounceMs);
1243
+ const debounceTimer = setTimeout(() => {
1244
+ void fetchQuote();
1245
+ }, delay);
1246
+ return () => {
1247
+ clearTimeout(debounceTimer);
1248
+ };
1249
+ }, [enabled, hasAmount, debounceMs, fetchQuote]);
1250
+ useEffect7(() => {
1251
+ if (!enabled || !hasAmount || !pollIntervalMs || pollIntervalMs <= 0) {
1252
+ return;
1253
+ }
1254
+ const interval = setInterval(() => {
1255
+ void fetchQuote();
1256
+ }, pollIntervalMs);
1257
+ return () => {
1258
+ clearInterval(interval);
1259
+ };
1260
+ }, [enabled, hasAmount, pollIntervalMs, fetchQuote]);
1261
+ return {
1262
+ quote,
1263
+ loading,
1264
+ error,
1265
+ refresh: fetchQuote
1266
+ };
1267
+ }
1268
+
1269
+ // src/useAnchors.ts
1270
+ import { useMemo, useCallback as useCallback7 } from "react";
1271
+ import {
1272
+ listAnchors
1273
+ } from "@nairaramp/core";
1274
+ function useAnchors(options = {}) {
1275
+ const { network, country, assetCode, sep } = options;
1276
+ const anchors = useMemo(() => {
1277
+ const coreFilter = network !== void 0 || country !== void 0 ? {
1278
+ ...network !== void 0 ? { network } : {},
1279
+ ...country !== void 0 ? { country } : {}
1280
+ } : void 0;
1281
+ const baseAnchors = listAnchors(coreFilter);
1282
+ return baseAnchors.filter((anchor) => {
1283
+ if (assetCode !== void 0 && !anchor.assets.some(
1284
+ (asset) => asset.code.toUpperCase() === assetCode.toUpperCase()
1285
+ )) {
1286
+ return false;
1287
+ }
1288
+ if (sep !== void 0 && !anchor.seps.includes(sep)) {
1289
+ return false;
1290
+ }
1291
+ return true;
1292
+ });
1293
+ }, [network, country, assetCode, sep]);
1294
+ const getAnchorById = useCallback7(
1295
+ (id) => anchors.find((anchor) => anchor.id === id),
1296
+ [anchors]
1297
+ );
1298
+ return { anchors, getAnchorById };
1299
+ }
1300
+
1301
+ // src/errorMessages.ts
1302
+ var ERROR_MESSAGES = {
1303
+ AUTH_FAILED: "Authentication with the anchor failed. Please try signing in again.",
1304
+ ANCHOR_UNREACHABLE: "Could not reach the anchor server. Check your connection and try again.",
1305
+ ANCHOR_NOT_FOUND: "The selected Stellar anchor is not recognized.",
1306
+ KYC_REQUIRED: "Identity verification (KYC) is required by the anchor before continuing.",
1307
+ QUOTE_EXPIRED: "Your exchange rate quote expired. Request a fresh quote to continue.",
1308
+ QUOTE_UNAVAILABLE: "Exchange rate quote is currently unavailable for this pair or amount.",
1309
+ UNSUPPORTED_ASSET: "This Stellar asset is not supported by the selected anchor.",
1310
+ SIGNER_REJECTED: "Wallet signature was cancelled or rejected.",
1311
+ TRANSACTION_FAILED: "The transaction could not be completed. Please try again.",
1312
+ MAINNET_NOT_ALLOWED: "Mainnet transactions are disabled in the current configuration.",
1313
+ ANCHOR_NOT_VERIFIED: "This anchor's mainnet verification is outdated and cannot be used."
1314
+ };
1315
+ function getErrorMessage(error, overrides) {
1316
+ if (!error) {
1317
+ return void 0;
1318
+ }
1319
+ const code = error.code;
1320
+ if (overrides && Object.prototype.hasOwnProperty.call(overrides, code)) {
1321
+ const custom = overrides[code];
1322
+ if (custom !== void 0) {
1323
+ return custom;
1324
+ }
1325
+ }
1326
+ if (Object.prototype.hasOwnProperty.call(ERROR_MESSAGES, code)) {
1327
+ return ERROR_MESSAGES[code];
1328
+ }
1329
+ return error.message;
1330
+ }
1331
+
546
1332
  // src/index.ts
547
- var VERSION = "0.1.0";
1333
+ var VERSION = "0.2.0";
548
1334
  export {
1335
+ CashOutToNaira,
1336
+ ERROR_MESSAGES,
1337
+ HAUSA_LABELS,
1338
+ IGBO_LABELS,
1339
+ NairaRampProvider,
1340
+ PIDGIN_LABELS,
549
1341
  PayWithNaira,
550
1342
  STATUS_MAP,
551
1343
  StatusTimeline,
552
1344
  TIMELINE_STEPS,
553
1345
  VERSION,
554
- usePayWithNaira
1346
+ YORUBA_LABELS,
1347
+ getErrorMessage,
1348
+ getStellarExplorerUrl,
1349
+ useAnchors,
1350
+ useCashOutToNaira,
1351
+ useNairaRamp,
1352
+ usePayWithNaira,
1353
+ useRampQuote
555
1354
  };