@calimero-network/mero-react 10.2.0 → 10.3.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.d.cts CHANGED
@@ -140,6 +140,8 @@ interface MeroContextValue {
140
140
  * and events only for contexts it is a member of.
141
141
  */
142
142
  isDelegated: boolean;
143
+ /** True when the node serves this page from the app's own origin (see `MeroProviderConfig.servedByNode`). */
144
+ servedByNode?: boolean;
143
145
  /**
144
146
  * What this connection may do, so an app asks WHAT IS ALLOWED instead of
145
147
  * which transport it runs on. A node login can do everything; an account on
@@ -200,6 +202,13 @@ interface MeroProviderConfig {
200
202
  * rejected and a security error is logged.
201
203
  */
202
204
  allowedNodeUrls?: string[];
205
+ /**
206
+ * Set when a node serves this page from the app's own origin
207
+ * (`{label}.localhost:2428`). The node is then `window.location.origin`: the
208
+ * client talks to it there whatever the callback's `node_url` says, and
209
+ * `ConnectButton` skips the node dialog and goes straight to login.
210
+ */
211
+ servedByNode?: boolean;
203
212
  /**
204
213
  * Token store for access/refresh tokens. Defaults to a localStorage-backed
205
214
  * store that persists across reloads.
@@ -261,7 +270,7 @@ declare function getPermissionsForMode(mode: AppMode): string[];
261
270
  interface MeroProviderProps extends MeroProviderConfig {
262
271
  children: React__default.ReactNode;
263
272
  }
264
- declare function MeroProvider({ children, mode, packageName, packageVersion, registryUrl, cloudBaseUrl, timeoutMs, allowedNodeUrls, tokenStore: tokenStoreProp, }: MeroProviderProps): react_jsx_runtime.JSX.Element;
273
+ declare function MeroProvider({ children, mode, packageName, packageVersion, registryUrl, cloudBaseUrl, timeoutMs, allowedNodeUrls, servedByNode, tokenStore: tokenStoreProp, }: MeroProviderProps): react_jsx_runtime.JSX.Element;
265
274
  declare function useMero(): MeroContextValue;
266
275
 
267
276
  /**
@@ -342,8 +351,9 @@ declare function cssVar(theme: ResolvedMeroTheme, key: keyof MeroTheme): string;
342
351
  interface ConnectButtonProps {
343
352
  /**
344
353
  * Connection behaviour. `Custom` (object form) connects straight to the
345
- * given URL, skipping the modal; every other value opens the LoginModal,
346
- * which always shows node discovery + manual URL entry.
354
+ * given URL, skipping the modal; every other value opens the LoginModal
355
+ * (local node discovery + manual URL entry). A page the node serves
356
+ * (`servedByNode`) skips the modal and logs in at its own origin.
347
357
  */
348
358
  connectionType?: ConnectionType | CustomConnectionConfig;
349
359
  /** Custom class name */
package/dist/index.d.ts CHANGED
@@ -140,6 +140,8 @@ interface MeroContextValue {
140
140
  * and events only for contexts it is a member of.
141
141
  */
142
142
  isDelegated: boolean;
143
+ /** True when the node serves this page from the app's own origin (see `MeroProviderConfig.servedByNode`). */
144
+ servedByNode?: boolean;
143
145
  /**
144
146
  * What this connection may do, so an app asks WHAT IS ALLOWED instead of
145
147
  * which transport it runs on. A node login can do everything; an account on
@@ -200,6 +202,13 @@ interface MeroProviderConfig {
200
202
  * rejected and a security error is logged.
201
203
  */
202
204
  allowedNodeUrls?: string[];
205
+ /**
206
+ * Set when a node serves this page from the app's own origin
207
+ * (`{label}.localhost:2428`). The node is then `window.location.origin`: the
208
+ * client talks to it there whatever the callback's `node_url` says, and
209
+ * `ConnectButton` skips the node dialog and goes straight to login.
210
+ */
211
+ servedByNode?: boolean;
203
212
  /**
204
213
  * Token store for access/refresh tokens. Defaults to a localStorage-backed
205
214
  * store that persists across reloads.
@@ -261,7 +270,7 @@ declare function getPermissionsForMode(mode: AppMode): string[];
261
270
  interface MeroProviderProps extends MeroProviderConfig {
262
271
  children: React__default.ReactNode;
263
272
  }
264
- declare function MeroProvider({ children, mode, packageName, packageVersion, registryUrl, cloudBaseUrl, timeoutMs, allowedNodeUrls, tokenStore: tokenStoreProp, }: MeroProviderProps): react_jsx_runtime.JSX.Element;
273
+ declare function MeroProvider({ children, mode, packageName, packageVersion, registryUrl, cloudBaseUrl, timeoutMs, allowedNodeUrls, servedByNode, tokenStore: tokenStoreProp, }: MeroProviderProps): react_jsx_runtime.JSX.Element;
265
274
  declare function useMero(): MeroContextValue;
266
275
 
267
276
  /**
@@ -342,8 +351,9 @@ declare function cssVar(theme: ResolvedMeroTheme, key: keyof MeroTheme): string;
342
351
  interface ConnectButtonProps {
343
352
  /**
344
353
  * Connection behaviour. `Custom` (object form) connects straight to the
345
- * given URL, skipping the modal; every other value opens the LoginModal,
346
- * which always shows node discovery + manual URL entry.
354
+ * given URL, skipping the modal; every other value opens the LoginModal
355
+ * (local node discovery + manual URL entry). A page the node serves
356
+ * (`servedByNode`) skips the modal and logs in at its own origin.
347
357
  */
348
358
  connectionType?: ConnectionType | CustomConnectionConfig;
349
359
  /** Custom class name */
package/dist/index.js CHANGED
@@ -346,8 +346,10 @@ function MeroProvider({
346
346
  cloudBaseUrl,
347
347
  timeoutMs = 3e4,
348
348
  allowedNodeUrls,
349
+ servedByNode,
349
350
  tokenStore: tokenStoreProp
350
351
  }) {
352
+ const ownOrigin = servedByNode && isBrowser ? window.location.origin : null;
351
353
  const tokenStore = useMemo(
352
354
  () => tokenStoreProp ?? new LocalStorageTokenStore(),
353
355
  [tokenStoreProp]
@@ -359,7 +361,7 @@ function MeroProvider({
359
361
  const [isAuthenticated, setIsAuthenticated] = useState(false);
360
362
  const [isOnline, setIsOnline] = useState(true);
361
363
  const [isLoading, setIsLoading] = useState(true);
362
- const [nodeUrl, setNodeUrlState] = useState(() => getNodeUrl());
364
+ const [nodeUrl, setNodeUrlState] = useState(() => ownOrigin ?? getNodeUrl());
363
365
  const [applicationId, setApplicationIdState] = useState(
364
366
  () => getApplicationId() || null
365
367
  );
@@ -377,14 +379,14 @@ function MeroProvider({
377
379
  meroRef.current.close();
378
380
  }
379
381
  const instance = new MeroJs({
380
- baseUrl: url,
382
+ baseUrl: ownOrigin ?? url,
381
383
  tokenStore,
382
384
  timeoutMs
383
385
  });
384
386
  meroRef.current = instance;
385
387
  return instance;
386
388
  },
387
- [timeoutMs, tokenStore]
389
+ [timeoutMs, tokenStore, ownOrigin]
388
390
  );
389
391
  const checkAuth = useCallback(
390
392
  async (instance) => {
@@ -442,12 +444,12 @@ function MeroProvider({
442
444
  setDelegated(null);
443
445
  setMero(null);
444
446
  setIsAuthenticated(false);
445
- setNodeUrlState(null);
447
+ setNodeUrlState(ownOrigin);
446
448
  setApplicationIdState(null);
447
449
  setContextIdState(null);
448
450
  setContextIdentityState(null);
449
451
  meroRef.current = null;
450
- }, [tokenStore]);
452
+ }, [tokenStore, ownOrigin]);
451
453
  useEffect(() => {
452
454
  if (!delegated) return;
453
455
  const relayUrl = (contextId ? relayForContext(delegated.account, contextId) : null) ?? delegated.relayUrl;
@@ -462,8 +464,9 @@ function MeroProvider({
462
464
  let active = true;
463
465
  const stop = new AbortController();
464
466
  (async () => {
465
- if (!client || !relayUrl) return;
466
- if (awaitingKey) {
467
+ const hasRelay = Boolean(client && relayUrl);
468
+ if (!hasRelay && !packageName) return;
469
+ if (awaitingKey && relayUrl) {
467
470
  const nodeKey = await learnRelayNodeKey(relayUrl, { signal: stop.signal });
468
471
  if (!active) return;
469
472
  setMero(nodeKey ? buildDelegatedClient(routed, contextId) ?? client : client);
@@ -484,6 +487,7 @@ function MeroProvider({
484
487
  if (!active) return;
485
488
  }
486
489
  }
490
+ if (!hasRelay) return;
487
491
  try {
488
492
  const contexts = await listDelegatedContexts(delegated);
489
493
  if (!active) return;
@@ -512,7 +516,7 @@ function MeroProvider({
512
516
  if (callback) {
513
517
  const initiatedNodeUrl = getNodeUrl();
514
518
  const tokenNodeUrl = getTokenNodeUrl() ?? initiatedNodeUrl;
515
- const { url, rejected } = resolveTrustedNodeUrl({
519
+ const { url, rejected } = ownOrigin ? { url: ownOrigin, rejected: false } : resolveTrustedNodeUrl({
516
520
  candidate: callback.nodeUrl,
517
521
  initiated: initiatedNodeUrl,
518
522
  allowedNodeUrls
@@ -556,7 +560,7 @@ function MeroProvider({
556
560
  nodeFromCallback = url;
557
561
  }
558
562
  }
559
- const savedUrl = nodeFromCallback || getNodeUrl();
563
+ const savedUrl = nodeFromCallback || ownOrigin || getNodeUrl();
560
564
  if (!savedUrl) {
561
565
  if (active) setIsLoading(false);
562
566
  return;
@@ -580,7 +584,7 @@ function MeroProvider({
580
584
  return () => {
581
585
  active = false;
582
586
  };
583
- }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated]);
587
+ }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated, ownOrigin]);
584
588
  useEffect(() => {
585
589
  if (!isAuthenticated || !meroRef.current) return;
586
590
  let active = true;
@@ -671,6 +675,7 @@ function MeroProvider({
671
675
  connectToNode,
672
676
  connectWithAccount,
673
677
  isDelegated: delegated !== null,
678
+ servedByNode: ownOrigin !== null,
674
679
  admin,
675
680
  can: delegated !== null ? { createNamespace: Boolean(packageName), createContext: true, invite: true, upgrade: false } : { createNamespace: true, createContext: true, invite: true, upgrade: true },
676
681
  app: { packageName, packageVersion, registryUrl },
@@ -678,7 +683,7 @@ function MeroProvider({
678
683
  logout,
679
684
  isLoading
680
685
  }),
681
- [mero, admin, isAuthenticated, isOnline, nodeUrl, applicationId, contextId, contextIdentity, connectToNode, connectWithAccount, delegated, logout, isLoading, packageName, packageVersion, registryUrl, cloudBaseUrl]
686
+ [mero, admin, isAuthenticated, isOnline, nodeUrl, applicationId, contextId, contextIdentity, connectToNode, connectWithAccount, delegated, ownOrigin, logout, isLoading, packageName, packageVersion, registryUrl, cloudBaseUrl]
682
687
  );
683
688
  return /* @__PURE__ */ jsx(MeroContext.Provider, { value: contextValue, children });
684
689
  }
@@ -1330,7 +1335,8 @@ function LoginModal({
1330
1335
  cloudOnDesktop = false,
1331
1336
  initialTab: initialTabProp
1332
1337
  }) {
1333
- const cloudAllowed = cloudOnDesktop || !isDesktopWindow();
1338
+ const servedByNode = useContext(MeroContext)?.servedByNode ?? false;
1339
+ const cloudAllowed = !servedByNode && (cloudOnDesktop || !isDesktopWindow());
1334
1340
  const ownCloud = cloud === void 0 && cloudAllowed;
1335
1341
  const own = useAccountEnrolment({ enabled: ownCloud });
1336
1342
  const cloudTab = useMemo(
@@ -1372,10 +1378,13 @@ function LoginModal({
1372
1378
  setCustomUrl(savedUrl);
1373
1379
  }
1374
1380
  }, []);
1381
+ useEffect(() => {
1382
+ if (isOpen && servedByNode) onConnect(window.location.origin);
1383
+ }, [isOpen, servedByNode]);
1375
1384
  const [scanNonce, setScanNonce] = useState(0);
1376
1385
  const portsKey = useMemo(() => localNodePorts.join(","), [localNodePorts]);
1377
1386
  useEffect(() => {
1378
- if (!isOpen) {
1387
+ if (!isOpen || servedByNode) {
1379
1388
  return;
1380
1389
  }
1381
1390
  const controller2 = new AbortController();
@@ -1396,7 +1405,7 @@ function LoginModal({
1396
1405
  active = false;
1397
1406
  controller2.abort();
1398
1407
  };
1399
- }, [isOpen, portsKey, scanNonce]);
1408
+ }, [isOpen, servedByNode, portsKey, scanNonce]);
1400
1409
  const isCustom = selected === CUSTOM_SELECTION;
1401
1410
  const hasDiscovered = discovered.length > 0;
1402
1411
  const canConnect = !loading && !discovering && (isCustom ? isValidUrl(customUrl) : true);
@@ -1431,7 +1440,7 @@ function LoginModal({
1431
1440
  setLoading(false);
1432
1441
  }
1433
1442
  }, [selected, customUrl, onConnect]);
1434
- if (!isOpen) {
1443
+ if (!isOpen || servedByNode) {
1435
1444
  return null;
1436
1445
  }
1437
1446
  const showManualInput = isCustom;
@@ -1709,8 +1718,8 @@ function ConnectButton({
1709
1718
  cloud: cloudProp,
1710
1719
  accountDefaults
1711
1720
  }) {
1712
- const cloud = cloudProp ?? !isDesktopWindow();
1713
- const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated } = useMero();
1721
+ const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated, servedByNode } = useMero();
1722
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1714
1723
  const enrolment = useAccountEnrolment({
1715
1724
  walletUrl: accountDefaults?.walletUrl,
1716
1725
  enabled: cloud
@@ -1753,6 +1762,10 @@ function ConnectButton({
1753
1762
  }, [label]);
1754
1763
  const buttonClassName = logoOnly ? "mero-logo-only" : "";
1755
1764
  const handleConnect = () => {
1765
+ if (servedByNode) {
1766
+ connectToNode(window.location.origin);
1767
+ return;
1768
+ }
1756
1769
  if (typeof connectionType === "object" && connectionType.type === "custom" /* Custom */) {
1757
1770
  connectToNode(connectionType.url);
1758
1771
  return;
@@ -1891,8 +1904,8 @@ function ConnectButtonAccount({
1891
1904
  defaults = {},
1892
1905
  cloud: cloudProp
1893
1906
  }) {
1894
- const cloud = cloudProp ?? !isDesktopWindow();
1895
- const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout } = useMero();
1907
+ const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout, servedByNode } = useMero();
1908
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1896
1909
  const [isDropdownOpen, setIsDropdownOpen] = useState(false);
1897
1910
  const [isNodeModalOpen, setIsNodeModalOpen] = useState(false);
1898
1911
  const [isAccountOpen, setIsAccountOpen] = useState(false);
@@ -1986,7 +1999,7 @@ function ConnectButtonAccount({
1986
1999
  {
1987
2000
  className: ["mero-connect-button", className].filter(Boolean).join(" "),
1988
2001
  style,
1989
- onClick: () => setIsNodeModalOpen(true),
2002
+ onClick: () => servedByNode ? connectToNode(window.location.origin) : setIsNodeModalOpen(true),
1990
2003
  children: [
1991
2004
  /* @__PURE__ */ jsx(CalimeroLogo, { size: 18, className: "mero-logo" }),
1992
2005
  "I run a node"