cilantro-react 0.1.2 → 0.1.4

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.d.mts CHANGED
@@ -65,6 +65,7 @@ interface WalletContextType {
65
65
  selectWallet: (walletId: string) => void;
66
66
  refreshWallets: () => Promise<void>;
67
67
  isLoading: boolean;
68
+ error: string | null;
68
69
  }
69
70
  interface WalletProviderProps {
70
71
  children: ReactNode;
package/dist/index.d.ts CHANGED
@@ -65,6 +65,7 @@ interface WalletContextType {
65
65
  selectWallet: (walletId: string) => void;
66
66
  refreshWallets: () => Promise<void>;
67
67
  isLoading: boolean;
68
+ error: string | null;
68
69
  }
69
70
  interface WalletProviderProps {
70
71
  children: ReactNode;
package/dist/index.js CHANGED
@@ -244,7 +244,6 @@ function CilantroAuthProvider({
244
244
  const [isLoading, setIsLoading] = (0, import_react.useState)(true);
245
245
  (0, import_react.useEffect)(() => {
246
246
  setAuthConfig({ platformApiKey: platformApiKey2, apiUrl: apiUrl2 });
247
- setSdkAuth(null);
248
247
  const storedToken = typeof window !== "undefined" ? localStorage.getItem(jwtStorageKey) : null;
249
248
  if (storedToken) {
250
249
  setToken(storedToken);
@@ -258,6 +257,8 @@ function CilantroAuthProvider({
258
257
  });
259
258
  } catch {
260
259
  }
260
+ } else {
261
+ setSdkAuth(null);
261
262
  }
262
263
  setIsLoading(false);
263
264
  }, [platformApiKey2, apiUrl2, jwtStorageKey]);
@@ -265,16 +266,16 @@ function CilantroAuthProvider({
265
266
  try {
266
267
  const result = await (0, import_auth.login)({ usernameOrEmail, password });
267
268
  const data = result && typeof result === "object" && "data" in result ? result.data : void 0;
268
- if (!data?.jwt) throw new Error("No JWT token received from server");
269
- const jwt = data.jwt;
270
- const userType = data.userType;
269
+ const jwt = (data && "jwt" in data ? data.jwt : void 0) ?? (data && "accessToken" in data ? data.accessToken : void 0);
270
+ if (!jwt) throw new Error("No JWT token received from server");
271
+ const userType = data?.userType;
271
272
  setToken(jwt);
272
273
  setSdkAuth(jwt);
273
274
  if (typeof window !== "undefined") {
274
275
  localStorage.setItem(jwtStorageKey, jwt);
275
276
  document.cookie = `cilantro_jwt=${jwt}; path=/; max-age=${60 * 60 * 24 * 7}`;
276
277
  }
277
- if (data.user && typeof data.user === "object") {
278
+ if (data?.user && typeof data.user === "object") {
278
279
  const u = data.user;
279
280
  setUser({
280
281
  username: u.username,
@@ -356,12 +357,15 @@ function WalletProvider({ children, storageKey = DEFAULT_STORAGE_KEY }) {
356
357
  const [wallets, setWallets] = (0, import_react2.useState)([]);
357
358
  const [selectedWallet, setSelectedWallet] = (0, import_react2.useState)(null);
358
359
  const [isLoading, setIsLoading] = (0, import_react2.useState)(true);
360
+ const [error, setError] = (0, import_react2.useState)(null);
359
361
  (0, import_react2.useEffect)(() => {
360
362
  if (token) {
363
+ setSdkAuth(token);
361
364
  loadWallets();
362
365
  } else {
363
366
  setWallets([]);
364
367
  setSelectedWallet(null);
368
+ setError(null);
365
369
  setIsLoading(false);
366
370
  }
367
371
  }, [token]);
@@ -390,6 +394,7 @@ function WalletProvider({ children, storageKey = DEFAULT_STORAGE_KEY }) {
390
394
  const loadWallets = async () => {
391
395
  try {
392
396
  setIsLoading(true);
397
+ setError(null);
393
398
  if (token) setSdkAuth(token);
394
399
  const result = await (0, import_wallet.findAll)();
395
400
  const walletsList = extractResponseData(result) ?? [];
@@ -401,9 +406,10 @@ function WalletProvider({ children, storageKey = DEFAULT_STORAGE_KEY }) {
401
406
  active: w.isActive
402
407
  }));
403
408
  setWallets(formattedWallets);
404
- } catch (error) {
405
- console.error("Failed to load wallets:", error);
409
+ } catch (err) {
410
+ console.error("Failed to load wallets:", err);
406
411
  setWallets([]);
412
+ setError(extractErrorMessage(err));
407
413
  } finally {
408
414
  setIsLoading(false);
409
415
  }
@@ -423,7 +429,8 @@ function WalletProvider({ children, storageKey = DEFAULT_STORAGE_KEY }) {
423
429
  wallets,
424
430
  selectWallet,
425
431
  refreshWallets,
426
- isLoading
432
+ isLoading,
433
+ error
427
434
  };
428
435
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WalletContext.Provider, { value, children });
429
436
  }
@@ -1666,6 +1673,7 @@ function WalletSelector(props) {
1666
1673
  children
1667
1674
  } = props;
1668
1675
  const { wallets, selectedWallet, selectWallet, isLoading, refreshWallets } = useWallets();
1676
+ const isEmpty = !isLoading && wallets.length === 0;
1669
1677
  const effectiveValue = value ?? selectedWallet?.id ?? selectedWallet?.walletId ?? "";
1670
1678
  const selected = wallets.find((w) => w.id === effectiveValue || w.walletId === effectiveValue) ?? selectedWallet;
1671
1679
  const handleSelect = (wallet) => {
@@ -1717,6 +1725,23 @@ function WalletSelector(props) {
1717
1725
  }
1718
1726
  );
1719
1727
  }
1728
+ if (isEmpty) {
1729
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
1730
+ "div",
1731
+ {
1732
+ className: cn(
1733
+ "rounded-md border border-input bg-muted/30 px-4 py-3 text-sm text-muted-foreground",
1734
+ className,
1735
+ classNames?.root
1736
+ ),
1737
+ "data-cilantro-wallet-selector": true,
1738
+ children: [
1739
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "font-medium text-foreground", children: "No wallets" }),
1740
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "mt-1", children: "Wallets are created by the platform when you sign in. If you just logged in, try refreshing." })
1741
+ ]
1742
+ }
1743
+ );
1744
+ }
1720
1745
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: cn(className, classNames?.root), "data-cilantro-wallet-selector": true, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Select, { value: effectiveValue || void 0, onValueChange: handleValueChange, disabled: isLoading, children: [
1721
1746
  /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(SelectTrigger, { className: classNames?.trigger, "aria-label": "Select wallet", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(SelectValue, { placeholder: isLoading ? "Loading..." : placeholder }) }),
1722
1747
  /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(SelectContent, { className: classNames?.content, children: wallets.map((w) => {
@@ -3092,7 +3117,35 @@ function SignerList({
3092
3117
  i
3093
3118
  ))
3094
3119
  }
3095
- ) : isLoading ? /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: cn("text-sm text-muted-foreground", classNames?.message), children: "Loading signers..." }) : error ? /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: cn("text-sm text-destructive", classNames?.message), role: "alert", children: error }) : signers.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: cn("text-sm text-muted-foreground", classNames?.message), children: "No signers. Add one to get started." }) : /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("ul", { className: cn("space-y-1", classNames?.list), role: "list", children: signers.map((signer) => /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("li", { className: cn("text-sm", classNames?.item), children: [
3120
+ ) : isLoading ? /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: cn("text-sm text-muted-foreground", classNames?.message), children: "Loading signers..." }) : error ? /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("div", { className: cn("rounded-md border border-destructive/50 bg-destructive/10 px-3 py-2", classNames?.message), role: "alert", children: [
3121
+ /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: "text-sm text-destructive", children: error }),
3122
+ /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(
3123
+ Button,
3124
+ {
3125
+ type: "button",
3126
+ variant: "outline",
3127
+ size: "sm",
3128
+ className: "mt-2",
3129
+ onClick: () => refresh(),
3130
+ children: "Retry"
3131
+ }
3132
+ )
3133
+ ] }) : signers.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("div", { className: cn("rounded-md border border-input bg-muted/30 px-4 py-3 text-sm text-muted-foreground", classNames?.message), children: [
3134
+ /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: "font-medium text-foreground", children: "No signers" }),
3135
+ /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { className: "mt-1", children: "Add a signer to authorize transactions for this wallet." }),
3136
+ /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(
3137
+ Button,
3138
+ {
3139
+ type: "button",
3140
+ size: "touch",
3141
+ variant: "outline",
3142
+ className: cn("mt-3", classNames?.addButton),
3143
+ onClick: () => setAddSignerOpen(true),
3144
+ "aria-label": "Add signer",
3145
+ children: "Add signer"
3146
+ }
3147
+ )
3148
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("ul", { className: cn("space-y-1", classNames?.list), role: "list", children: signers.map((signer) => /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("li", { className: cn("text-sm", classNames?.item), children: [
3096
3149
  getSignerDisplayName(signer),
3097
3150
  " (",
3098
3151
  getSignerTypeLabel(signer.type || signer.signerType || ""),