import { ScreenTrackerContext, // eslint-disable-next-line @typescript-eslint/no-unused-vars ScreenTrackerProvider, } from '../objects/ScreenTrackerProvider'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import type {ScreenTracker} from '../objects/ScreenTracker.old'; import {useContext} from 'react'; /** * Hook used for manually tracking components throughout the app. * * WARNING: To be used only inside {@linkcode ScreenTrackerProvider}. * * The provided methods permit specifying the start and end of a screen. * This will be reflected as an app screen in the controller. * * Some navigation libraries permit subscribing to root level methods where * you only have to implement this API only once, instead of in every screen: * * 1. {@link https://github.com/underscopeio/react-native-navigation-hooks React Native navigation} * `useNavigationComponentDidAppear()` and * `useNavigationComponentDidDisappear()`. * * 2. {@link https://reactnavigation.org/docs/use-navigation-state React navigation} * `useNavigationState()`. * * WARNING: Use unique screen names. Duplicate names might result in unexpected * behavior. * * ```tsx * import {useScreenTracker} from '@appdynamics/react-native-agent'; * * export function LoginScreen() { * const {trackScreenStart, trackScreenEnd} = useScreenTracker(); * * useEffect(() => { * const markScreenStart = async () => { * await trackScreenStart('Login'); * }; * markScreenStart(); * * return () => { * const markScreenEnd = async () => { * await trackScreenEnd('Login'); * }; * markScreenEnd(); * }; * }, [trackScreenEnd, trackScreenStart]); * * return ( * * // rest of JSX tree * * ); * } * ``` * * @returns {object} `{trackScreenStart, trackScreenEnd}` Two methods to track screens. * * For `class` implementations: * @see {@linkcode ScreenTracker} */ const useScreenTracker = () => { const context = useContext(ScreenTrackerContext); if (!context) { throw new Error( 'useScreenTracker must be used within a ScreenTrackerProvider.', ); } return context; }; export default useScreenTracker;