Download wp-calypso/client/lib/jetpack/use-track-callback.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/jetpack/use-track-callback.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/lib/jetpack/use-track-callback.ts
-
curl -L -o use-track-callback.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/jetpack/use-track-callback.ts
1.49 kB
| import { useCallback } from 'react'; | |
| import { useDispatch, useSelector } from 'calypso/state'; | |
| import { recordTracksEvent } from 'calypso/state/analytics/actions'; | |
| import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id'; | |
| const noop = () => undefined; | |
| // The overloaded signatures help all the type info get inferred correctly when | |
| // callbacks may or may not be passed. | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| function useTrackCallback< F extends ( ...args: any[] ) => any >( | |
| callback: F, | |
| eventName: string, | |
| eventProps?: Record< string, unknown > | |
| ): F; | |
| function useTrackCallback( | |
| callback: undefined, | |
| eventName: string, | |
| eventProps?: Record< string, unknown > | |
| ): typeof noop; | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| function useTrackCallback< F extends ( ...args: any[] ) => any >( | |
| callback: F | undefined, | |
| eventName: string, | |
| eventProps: Record< string, unknown > = {} | |
| ): F | typeof noop { | |
| const fn: F | typeof noop = callback ?? noop; | |
| const dispatch = useDispatch(); | |
| const siteId = useSelector( getSelectedSiteId ); | |
| const trackedCallback = useCallback< F | typeof noop >( | |
| // @ts-expect-error It's totally ok for these args to be any. | |
| ( ...rest ) => { | |
| dispatch( | |
| recordTracksEvent( eventName, { | |
| site_id: siteId, | |
| ...eventProps, | |
| } ) | |
| ); | |
| return fn( ...rest ); | |
| }, | |
| [ callback, eventName, dispatch, siteId ] | |
| ); | |
| return trackedCallback; | |
| } | |
| export default useTrackCallback; | |