UNPKG

datadog-ux-utils

Version:

Datadog RUM focused UX & performance toolkit: API guards (retry, breaker, rate), React telemetry (error boundary, profiler, Suspense), web vitals & resource observers, offline queues.

62 lines 1.78 kB
/** * @file idle.ts * @description Tracks user idle/active state in the browser. Useful for: * - Detecting long periods of inactivity to pause expensive operations. * - Reporting idle sessions to Datadog for UX metrics. * - Automatically logging out users after inactivity. * * This uses `mousemove`, `keydown`, and visibility changes to detect activity. */ /** * Configuration options for idle tracking. */ export interface IdleTrackerOptions { /** * Time in milliseconds before the user is considered idle. * @default 60000 (1 minute) */ idleAfterMs?: number; /** * Whether to automatically send an action to Datadog when idle/active state changes. * @default true */ reportToDatadog?: boolean; /** * Optional callback fired when the idle state changes. * @param isIdle - `true` if the user is now idle, `false` if active. */ onChange?: (isIdle: boolean) => void; } /** * Starts tracking user idle state. * * @param opts - Idle tracking configuration. * @returns A function to stop idle tracking. * * @example * ```ts * import { startIdleTracker } from "datadog-ux-utils/idle"; * * const stop = startIdleTracker({ * idleAfterMs: 2 * 60 * 1000, // 2 minutes * onChange: (isIdle) => { * console.log(isIdle ? "User is idle" : "User is active"); * }, * }); * * // Later, if you want to stop tracking: * stop(); * ``` */ export declare function startIdleTracker(opts?: IdleTrackerOptions): () => void; /** * Returns whether the user is currently considered idle. * * @example * ```ts * import { isUserIdle } from "datadog-ux-utils/idle"; * console.log("Is idle?", isUserIdle()); * ``` */ export declare function isUserIdle(): boolean; //# sourceMappingURL=idle.d.ts.map