{"version":3,"sources":["../../src/use-user-idle/index.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { useEventListener } from '../use-event-listener'\nimport { useLatest } from '../use-latest'\nimport { useMount } from '../use-mount'\nimport { usePausable } from '../use-pausable'\nimport { useStableFn } from '../use-stable-fn'\nimport { useThrottledFn } from '../use-throttled-fn'\nimport { useTrackedRefState } from '../use-tracked-ref-state'\nimport { timestamp } from '../utils/basic'\n\nimport type { Pausable } from '../use-pausable'\nimport type { SetTimeoutReturn, WindowEventName } from '../utils/basic'\n\nexport interface UseUserIdleOptions {\n  /**\n   * Event names that listen to for detected user activity\n   *\n   * @defaultValue ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel']\n   */\n  events?: WindowEventName[]\n  /**\n   * Listen for document visibility change\n   *\n   * @defaultValue true\n   */\n  watchVisibility?: boolean\n  /**\n   * Initial state of the ref idle\n   *\n   * @defaultValue false\n   */\n  initialState?: boolean\n  /**\n   * Reset the idle state immediately\n   *\n   * @defaultValue true\n   */\n  immediate?: boolean\n}\n\nexport interface UseUserIdleReturns extends Pausable<[reset?: boolean], [reset?: boolean]> {\n  /**\n   * Whether the user is idle\n   */\n  isIdle: boolean\n  /**\n   * The timestamp of the last user activity\n   */\n  lastActive: number\n  /**\n   * Reset the idle state\n   *\n   * @param restart - Whether to restart the idle timer,`true` by default.\n   */\n  reset: (restart?: boolean, updateTimestamp?: boolean) => void\n}\n\nconst defaultEvents: WindowEventName[] = ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel']\nconst oneMinute = 60_000\n\n/**\n * A React Hook that helps to detect whether the user is idle or not.\n */\nexport function useUserIdle(timeout: number = oneMinute, options: UseUserIdleOptions = {}): UseUserIdleReturns {\n  const { immediate = true, initialState = false, watchVisibility = true, events = defaultEvents } = options\n\n  const [state, { updateRefState }] = useTrackedRefState({\n    isIdle: initialState,\n    lastActive: Date.now(),\n  })\n\n  const timer = useRef<SetTimeoutReturn | null>(null)\n  const latest = useLatest({ timeout })\n\n  const pausable = usePausable(\n    false,\n    (_ref, reset?: boolean) => reset && resetTimer(false),\n    (_ref, reset?: boolean) => reset && resetTimer(true),\n  )\n\n  const resetTimer = useStableFn((restart = true, updateTimestamp = false) => {\n    updateRefState('isIdle', false)\n\n    if (updateTimestamp) updateRefState('lastActive', timestamp())\n\n    timer.current !== null && clearTimeout(timer.current)\n\n    if (restart) {\n      timer.current = setTimeout(() => updateRefState('isIdle', true), latest.current.timeout)\n    }\n  })\n\n  const onEvent = useThrottledFn(() => resetTimer(true, true), { wait: 50 })\n\n  useEventListener(() => window, events, onEvent, { passive: true })\n\n  useEventListener(\n    () => document,\n    watchVisibility ? 'visibilitychange' : [],\n    () => !document.hidden && onEvent(),\n  )\n\n  useMount(immediate && resetTimer)\n\n  return {\n    ...pausable,\n    get isIdle() {\n      return state.isIdle\n    },\n    get lastActive() {\n      return state.lastActive\n    },\n    reset: resetTimer,\n  }\n}\n"],"names":["useUserIdle","defaultEvents","oneMinute","timeout","options","immediate","initialState","watchVisibility","events","useTrackedRefState","isIdle","lastActive","Date","now","state","updateRefState","timer","useRef","latest","useLatest","pausable","usePausable","_ref","reset","resetTimer","useStableFn","restart","updateTimestamp","timestamp","current","clearTimeout","setTimeout","onEvent","useThrottledFn","wait","useEventListener","window","passive","document","hidden","useMount"],"mappings":";;;;+BA+DgBA;;;eAAAA;;;qBA/DO;gCACU;yBACP;wBACD;2BACG;2BACA;8BACG;kCACI;qBACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiD1B,IAAMC,gBAAmC;IAAC;IAAa;IAAa;IAAU;IAAW;IAAc;CAAQ;AAC/G,IAAMC,YAAY;AAKX,SAASF;QAAYG,UAAAA,iEAAkBD,WAAWE,UAAAA,iEAA8B,CAAC;IACtF,yBAAmGA,QAA3FC,WAAAA,4CAAY,mDAA+ED,QAAzEE,cAA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