@alien_org/react 0.2.1 → 0.2.3

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.cjs CHANGED
@@ -4,6 +4,7 @@ let react = require("react");
4
4
  let react_jsx_runtime = require("react/jsx-runtime");
5
5
 
6
6
  //#region src/context.tsx
7
+ const useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
7
8
  const AlienContext = (0, react.createContext)(null);
8
9
  /**
9
10
  * Provider component that initializes the Alien miniapp context.
@@ -22,28 +23,36 @@ const AlienContext = (0, react.createContext)(null);
22
23
  * }
23
24
  * ```
24
25
  */
25
- function AlienProvider({ children, autoReady = true }) {
26
+ function AlienProvider({ children, autoReady = true, interceptLinks = true }) {
26
27
  const readySent = (0, react.useRef)(false);
27
28
  const ready = (0, react.useCallback)(() => {
28
29
  if (readySent.current) return;
29
30
  readySent.current = true;
30
31
  if ((0, _alien_org_bridge.isBridgeAvailable)()) (0, _alien_org_bridge.send)("app:ready", {});
31
32
  }, []);
32
- const value = (0, react.useMemo)(() => {
33
+ const [value, setValue] = (0, react.useState)(() => ({
34
+ authToken: void 0,
35
+ contractVersion: void 0,
36
+ isBridgeAvailable: false,
37
+ ready
38
+ }));
39
+ useIsomorphicLayoutEffect$1(() => {
33
40
  const launchParams = (0, _alien_org_bridge.getLaunchParams)();
34
- return {
41
+ const bridgeAvailable = (0, _alien_org_bridge.isBridgeAvailable)();
42
+ setValue({
35
43
  authToken: launchParams?.authToken,
36
44
  contractVersion: launchParams?.contractVersion,
37
- isBridgeAvailable: (0, _alien_org_bridge.isBridgeAvailable)(),
45
+ isBridgeAvailable: bridgeAvailable,
38
46
  ready
39
- };
47
+ });
48
+ if (!bridgeAvailable) console.warn("[@alien_org/react] Bridge is not available. Running in dev mode? The SDK will handle errors gracefully, but bridge communication will not work.");
40
49
  }, [ready]);
41
- (0, react.useEffect)(() => {
42
- if (!value.isBridgeAvailable) console.warn("[@alien_org/react] Bridge is not available. Running in dev mode? The SDK will handle errors gracefully, but bridge communication will not work.");
43
- }, [value.isBridgeAvailable]);
44
50
  (0, react.useEffect)(() => {
45
51
  if (autoReady) ready();
46
52
  }, [autoReady, ready]);
53
+ (0, react.useEffect)(() => {
54
+ if (interceptLinks) return (0, _alien_org_bridge.enableLinkInterceptor)();
55
+ }, [interceptLinks]);
47
56
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AlienContext.Provider, {
48
57
  value,
49
58
  children
@@ -259,6 +268,7 @@ function useIsMethodSupported(method) {
259
268
 
260
269
  //#endregion
261
270
  //#region src/hooks/useLaunchParams.ts
271
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
262
272
  /**
263
273
  * Hook to get launch params.
264
274
  * Returns undefined if params unavailable (use mockLaunchParamsForDev in dev).
@@ -279,7 +289,34 @@ function useIsMethodSupported(method) {
279
289
  * ```
280
290
  */
281
291
  function useLaunchParams() {
282
- return (0, react.useMemo)(() => (0, _alien_org_bridge.getLaunchParams)(), []);
292
+ const [params, setParams] = (0, react.useState)(void 0);
293
+ useIsomorphicLayoutEffect(() => {
294
+ setParams((0, _alien_org_bridge.getLaunchParams)());
295
+ }, []);
296
+ return params;
297
+ }
298
+
299
+ //#endregion
300
+ //#region src/hooks/useLinkInterceptor.ts
301
+ /**
302
+ * Intercepts external link clicks and routes them through the bridge.
303
+ * Activates when the bridge is available, cleans up on unmount.
304
+ *
305
+ * @example
306
+ * ```tsx
307
+ * function App() {
308
+ * useLinkInterceptor();
309
+ * return <a href="https://external.com">Opens via host app</a>;
310
+ * }
311
+ * ```
312
+ */
313
+ function useLinkInterceptor(options = {}) {
314
+ const { isBridgeAvailable: isBridgeAvailable$1 } = useAlien();
315
+ const { openMode } = options;
316
+ (0, react.useEffect)(() => {
317
+ if (!isBridgeAvailable$1) return;
318
+ return (0, _alien_org_bridge.enableLinkInterceptor)({ openMode });
319
+ }, [isBridgeAvailable$1, openMode]);
283
320
  }
284
321
 
285
322
  //#endregion
@@ -628,5 +665,6 @@ exports.useClipboard = useClipboard;
628
665
  exports.useEvent = useEvent;
629
666
  exports.useIsMethodSupported = useIsMethodSupported;
630
667
  exports.useLaunchParams = useLaunchParams;
668
+ exports.useLinkInterceptor = useLinkInterceptor;
631
669
  exports.useMethod = useMethod;
632
670
  exports.usePayment = usePayment;
package/dist/index.d.cts CHANGED
@@ -477,6 +477,17 @@ declare global {
477
477
  * Error thrown when launch params cannot be retrieved.
478
478
  */
479
479
  //#endregion
480
+ //#region ../bridge/src/link-interceptor.d.ts
481
+ interface LinkInterceptorOptions {
482
+ /**
483
+ * Where to open intercepted links.
484
+ * - `external` (default): System browser or app handler
485
+ * - `internal`: Within the host app
486
+ * @default 'external'
487
+ */
488
+ openMode?: 'external' | 'internal';
489
+ }
490
+ //#endregion
480
491
  //#region ../bridge/src/request.d.ts
481
492
  interface RequestOptions {
482
493
  reqId?: string;
@@ -565,6 +576,12 @@ interface AlienProviderProps {
565
576
  * ```
566
577
  */
567
578
  autoReady?: boolean;
579
+ /**
580
+ * Whether to intercept external link clicks and route them through the
581
+ * bridge's `link:open` method. Same-origin links are unaffected.
582
+ * @default true
583
+ */
584
+ interceptLinks?: boolean;
568
585
  }
569
586
  /**
570
587
  * Provider component that initializes the Alien miniapp context.
@@ -585,7 +602,8 @@ interface AlienProviderProps {
585
602
  */
586
603
  declare function AlienProvider({
587
604
  children,
588
- autoReady
605
+ autoReady,
606
+ interceptLinks
589
607
  }: AlienProviderProps): ReactNode;
590
608
  //#endregion
591
609
  //#region src/errors.d.ts
@@ -763,6 +781,21 @@ declare function useIsMethodSupported(method: MethodName): MethodSupportResult;
763
781
  */
764
782
  declare function useLaunchParams(): LaunchParams | undefined;
765
783
  //#endregion
784
+ //#region src/hooks/useLinkInterceptor.d.ts
785
+ /**
786
+ * Intercepts external link clicks and routes them through the bridge.
787
+ * Activates when the bridge is available, cleans up on unmount.
788
+ *
789
+ * @example
790
+ * ```tsx
791
+ * function App() {
792
+ * useLinkInterceptor();
793
+ * return <a href="https://external.com">Opens via host app</a>;
794
+ * }
795
+ * ```
796
+ */
797
+ declare function useLinkInterceptor(options?: LinkInterceptorOptions): void;
798
+ //#endregion
766
799
  //#region src/hooks/useMethod.d.ts
767
800
  interface UseMethodExecuteResult<E extends EventName> {
768
801
  data: EventPayload<E> | undefined;
@@ -955,4 +988,4 @@ interface UsePaymentReturn {
955
988
  */
956
989
  declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
957
990
  //#endregion
958
- export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
991
+ export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type LinkInterceptorOptions, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useLinkInterceptor, useMethod, usePayment };
package/dist/index.d.mts CHANGED
@@ -477,6 +477,17 @@ declare global {
477
477
  * Error thrown when launch params cannot be retrieved.
478
478
  */
479
479
  //#endregion
480
+ //#region ../bridge/src/link-interceptor.d.ts
481
+ interface LinkInterceptorOptions {
482
+ /**
483
+ * Where to open intercepted links.
484
+ * - `external` (default): System browser or app handler
485
+ * - `internal`: Within the host app
486
+ * @default 'external'
487
+ */
488
+ openMode?: 'external' | 'internal';
489
+ }
490
+ //#endregion
480
491
  //#region ../bridge/src/request.d.ts
481
492
  interface RequestOptions {
482
493
  reqId?: string;
@@ -565,6 +576,12 @@ interface AlienProviderProps {
565
576
  * ```
566
577
  */
567
578
  autoReady?: boolean;
579
+ /**
580
+ * Whether to intercept external link clicks and route them through the
581
+ * bridge's `link:open` method. Same-origin links are unaffected.
582
+ * @default true
583
+ */
584
+ interceptLinks?: boolean;
568
585
  }
569
586
  /**
570
587
  * Provider component that initializes the Alien miniapp context.
@@ -585,7 +602,8 @@ interface AlienProviderProps {
585
602
  */
586
603
  declare function AlienProvider({
587
604
  children,
588
- autoReady
605
+ autoReady,
606
+ interceptLinks
589
607
  }: AlienProviderProps): ReactNode;
590
608
  //#endregion
591
609
  //#region src/errors.d.ts
@@ -763,6 +781,21 @@ declare function useIsMethodSupported(method: MethodName): MethodSupportResult;
763
781
  */
764
782
  declare function useLaunchParams(): LaunchParams | undefined;
765
783
  //#endregion
784
+ //#region src/hooks/useLinkInterceptor.d.ts
785
+ /**
786
+ * Intercepts external link clicks and routes them through the bridge.
787
+ * Activates when the bridge is available, cleans up on unmount.
788
+ *
789
+ * @example
790
+ * ```tsx
791
+ * function App() {
792
+ * useLinkInterceptor();
793
+ * return <a href="https://external.com">Opens via host app</a>;
794
+ * }
795
+ * ```
796
+ */
797
+ declare function useLinkInterceptor(options?: LinkInterceptorOptions): void;
798
+ //#endregion
766
799
  //#region src/hooks/useMethod.d.ts
767
800
  interface UseMethodExecuteResult<E extends EventName> {
768
801
  data: EventPayload<E> | undefined;
@@ -955,4 +988,4 @@ interface UsePaymentReturn {
955
988
  */
956
989
  declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
957
990
  //#endregion
958
- export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
991
+ export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type LinkInterceptorOptions, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useLinkInterceptor, useMethod, usePayment };
package/dist/index.mjs CHANGED
@@ -1,9 +1,10 @@
1
- import { BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, getLaunchParams, isBridgeAvailable, on, request, send, send as send$1 } from "@alien_org/bridge";
1
+ import { BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, enableLinkInterceptor, getLaunchParams, isBridgeAvailable, on, request, send, send as send$1 } from "@alien_org/bridge";
2
2
  import { getMethodMinVersion, getMethodMinVersion as getMethodMinVersion$1, isMethodSupported, isMethodSupported as isMethodSupported$1 } from "@alien_org/contract";
3
- import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
3
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
 
6
6
  //#region src/context.tsx
7
+ const useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? useLayoutEffect : useEffect;
7
8
  const AlienContext = createContext(null);
8
9
  /**
9
10
  * Provider component that initializes the Alien miniapp context.
@@ -22,28 +23,36 @@ const AlienContext = createContext(null);
22
23
  * }
23
24
  * ```
24
25
  */
25
- function AlienProvider({ children, autoReady = true }) {
26
+ function AlienProvider({ children, autoReady = true, interceptLinks = true }) {
26
27
  const readySent = useRef(false);
27
28
  const ready = useCallback(() => {
28
29
  if (readySent.current) return;
29
30
  readySent.current = true;
30
31
  if (isBridgeAvailable()) send$1("app:ready", {});
31
32
  }, []);
32
- const value = useMemo(() => {
33
+ const [value, setValue] = useState(() => ({
34
+ authToken: void 0,
35
+ contractVersion: void 0,
36
+ isBridgeAvailable: false,
37
+ ready
38
+ }));
39
+ useIsomorphicLayoutEffect$1(() => {
33
40
  const launchParams = getLaunchParams();
34
- return {
41
+ const bridgeAvailable = isBridgeAvailable();
42
+ setValue({
35
43
  authToken: launchParams?.authToken,
36
44
  contractVersion: launchParams?.contractVersion,
37
- isBridgeAvailable: isBridgeAvailable(),
45
+ isBridgeAvailable: bridgeAvailable,
38
46
  ready
39
- };
47
+ });
48
+ if (!bridgeAvailable) console.warn("[@alien_org/react] Bridge is not available. Running in dev mode? The SDK will handle errors gracefully, but bridge communication will not work.");
40
49
  }, [ready]);
41
- useEffect(() => {
42
- if (!value.isBridgeAvailable) console.warn("[@alien_org/react] Bridge is not available. Running in dev mode? The SDK will handle errors gracefully, but bridge communication will not work.");
43
- }, [value.isBridgeAvailable]);
44
50
  useEffect(() => {
45
51
  if (autoReady) ready();
46
52
  }, [autoReady, ready]);
53
+ useEffect(() => {
54
+ if (interceptLinks) return enableLinkInterceptor();
55
+ }, [interceptLinks]);
47
56
  return /* @__PURE__ */ jsx(AlienContext.Provider, {
48
57
  value,
49
58
  children
@@ -259,6 +268,7 @@ function useIsMethodSupported(method) {
259
268
 
260
269
  //#endregion
261
270
  //#region src/hooks/useLaunchParams.ts
271
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
262
272
  /**
263
273
  * Hook to get launch params.
264
274
  * Returns undefined if params unavailable (use mockLaunchParamsForDev in dev).
@@ -279,7 +289,34 @@ function useIsMethodSupported(method) {
279
289
  * ```
280
290
  */
281
291
  function useLaunchParams() {
282
- return useMemo(() => getLaunchParams(), []);
292
+ const [params, setParams] = useState(void 0);
293
+ useIsomorphicLayoutEffect(() => {
294
+ setParams(getLaunchParams());
295
+ }, []);
296
+ return params;
297
+ }
298
+
299
+ //#endregion
300
+ //#region src/hooks/useLinkInterceptor.ts
301
+ /**
302
+ * Intercepts external link clicks and routes them through the bridge.
303
+ * Activates when the bridge is available, cleans up on unmount.
304
+ *
305
+ * @example
306
+ * ```tsx
307
+ * function App() {
308
+ * useLinkInterceptor();
309
+ * return <a href="https://external.com">Opens via host app</a>;
310
+ * }
311
+ * ```
312
+ */
313
+ function useLinkInterceptor(options = {}) {
314
+ const { isBridgeAvailable: isBridgeAvailable$1 } = useAlien();
315
+ const { openMode } = options;
316
+ useEffect(() => {
317
+ if (!isBridgeAvailable$1) return;
318
+ return enableLinkInterceptor({ openMode });
319
+ }, [isBridgeAvailable$1, openMode]);
283
320
  }
284
321
 
285
322
  //#endregion
@@ -598,4 +635,4 @@ function usePayment(options = {}) {
598
635
  }
599
636
 
600
637
  //#endregion
601
- export { AlienProvider, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, MethodNotSupportedError, ReactSDKError, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
638
+ export { AlienProvider, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, MethodNotSupportedError, ReactSDKError, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useLinkInterceptor, useMethod, usePayment };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alien_org/react",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -34,7 +34,7 @@
34
34
  "prepublishOnly": "bun run build"
35
35
  },
36
36
  "dependencies": {
37
- "@alien_org/bridge": "0.2.1",
37
+ "@alien_org/bridge": "0.2.2",
38
38
  "@alien_org/contract": "0.2.1"
39
39
  },
40
40
  "peerDependencies": {