@@ -2,10 +2,42 @@ import { useEffect } from 'react';
22import { useSnapshot } from 'valtio' ;
33import { EventsController , type EventsControllerState } from '@reown/appkit-core-react-native' ;
44import { type EventName } from '@reown/appkit-common-react-native' ;
5- import { useAppKit } from './useAppKit ' ;
5+ import { useAppKitContext } from './useAppKitContext ' ;
66
7+ /**
8+ * Hook to subscribe to all AppKit events
9+ *
10+ * @remarks
11+ * This hook provides reactive access to AppKit's event system, allowing you to
12+ * monitor all events that occur within the AppKit lifecycle (connections, disconnections,
13+ * network changes, etc.). The callback is invoked whenever a new event is emitted.
14+ *
15+ * @param callback - Optional callback function invoked when any event occurs
16+ *
17+ * @returns An object containing:
18+ * - `data`: The most recent event data
19+ * - `timestamp`: The timestamp of the most recent event
20+ *
21+ * @throws {Error } If used outside of an AppKitProvider
22+ *
23+ * @example
24+ * ```tsx
25+ * function MyComponent() {
26+ * const { data, timestamp } = useAppKitEvents((event) => {
27+ * console.log('Event occurred:', event.data.event);
28+ * });
29+ *
30+ * return (
31+ * <View>
32+ * <Text>Last event: {data?.event}</Text>
33+ * <Text>Time: {timestamp}</Text>
34+ * </View>
35+ * );
36+ * }
37+ * ```
38+ */
739export function useAppKitEvents ( callback ?: ( newEvent : EventsControllerState ) => void ) {
8- useAppKit ( ) ; // Use the hook for checks
40+ useAppKitContext ( ) ;
941 const { data, timestamp } = useSnapshot ( EventsController . state ) ;
1042
1143 useEffect ( ( ) => {
@@ -21,11 +53,39 @@ export function useAppKitEvents(callback?: (newEvent: EventsControllerState) =>
2153 return { data, timestamp } ;
2254}
2355
56+ /**
57+ * Hook to subscribe to a specific AppKit event
58+ *
59+ * @remarks
60+ * This hook allows you to listen for a specific event type rather than all events.
61+ * It's more efficient than `useAppKitEvents` when you only care about a particular event.
62+ *
63+ * @param event - The specific event name to subscribe to (e.g., 'MODAL_OPEN', 'CONNECT_SUCCESS')
64+ * @param callback - Callback function invoked when the specified event occurs
65+ *
66+ * @throws {Error } If used outside of an AppKitProvider
67+ *
68+ * @example
69+ * ```tsx
70+ * function MyComponent() {
71+ * useAppKitEventSubscription('CONNECT_SUCCESS', (event) => {
72+ * console.log('Wallet connected!', event.data);
73+ * });
74+ *
75+ * useAppKitEventSubscription('DISCONNECT_SUCCESS', (event) => {
76+ * console.log('Wallet disconnected!', event.data);
77+ * });
78+ *
79+ * return <View>{/ Your component /}</View>;
80+ * }
81+ * ```
82+ */
83+
2484export function useAppKitEventSubscription (
2585 event : EventName ,
2686 callback : ( newEvent : EventsControllerState ) => void
2787) {
28- useAppKit ( ) ; // Use the hook for checks
88+ useAppKitContext ( ) ;
2989
3090 useEffect ( ( ) => {
3191 const unsubscribe = EventsController ?. subscribeEvent ( event , callback ) ;
0 commit comments