import { useEffect, useRef } from 'react'; import { getShallowPropsDifference, getPropsDifference } from '../../compare'; import { EVENT_TYPE, ACTION_SUBJECT, ACTION, FireAnalyticsCallback, } from '../../../plugins/analytics'; type RenderActions = ACTION.RE_RENDERED; type RenderActionSubjects = | ACTION_SUBJECT.EDITOR | ACTION_SUBJECT.REACT_EDITOR_VIEW; export function useComponentRenderTracking( props: Props, action: RenderActions, actionSubject: RenderActionSubjects, handleAnalyticsEvent: FireAnalyticsCallback, propsToIgnore: Array = [], useShallow?: boolean, ) { const propsRef = useRef(); const renderCountRef = useRef(0); useEffect(() => { const lastProps = propsRef.current; const renderCount = renderCountRef.current; if (lastProps) { let difference = useShallow ? getShallowPropsDifference(lastProps, props) : getPropsDifference(lastProps, props, 0, 2, propsToIgnore); handleAnalyticsEvent({ payload: { action, actionSubject, attributes: { propsDifference: difference, count: renderCount, }, eventType: EVENT_TYPE.OPERATIONAL, }, }); } propsRef.current = props; renderCountRef.current = renderCountRef.current + 1; }); // No dependencies run on each render } export type RenderTrackingProps = { componentProps: ComponentProps; action: RenderActions; actionSubject: RenderActionSubjects; handleAnalyticsEvent: FireAnalyticsCallback; propsToIgnore?: Array; useShallow?: boolean; }; export function RenderTracking(props: RenderTrackingProps) { useComponentRenderTracking( props.componentProps, props.action, props.actionSubject, props.handleAnalyticsEvent, props.propsToIgnore, props.useShallow, ); return null; }