Files
upstream-rocketchat/apps/meteor/client/hooks/useIdleDetection.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

91 lines
2.6 KiB
TypeScript

import { useStableCallback } from '@rocket.chat/fuselage-hooks';
import { useEffect, useRef } from 'react';
const events = ['mousemove', 'mousedown', 'touchend', 'touchstart', 'keypress'];
export type UseIdleDetectionOptions = {
id?: string;
time?: number;
awayOnWindowBlur?: boolean;
};
export const DEFAULT_IDLE_DETECTION_OPTIONS = Object.freeze({
id: 'useIdleDetection',
time: 600_000, // 10 minutes
awayOnWindowBlur: false,
});
/**
* A hook that detects when the user is idle.
*
* This hook listens for mousemove, mousedown, touchend, touchstart, and keypress events.
* When any of these events are triggered, the user is considered active.
* If no events are triggered for a specified period of time, the user is considered idle.
*
* @param options - An object with the following properties:
* @param options.id - A unique identifier for the idle detection mechanism. Defaults to 'useIdleDetection'.
* @param options.time - The time in milliseconds to consider the user idle. Defaults to 600000 ms (10 minutes).
* @param options.awayOnWindowBlur - A boolean flag to trigger the idle state when the window loses focus. Defaults to false.
*/
export const useIdleDetection = ({
id = DEFAULT_IDLE_DETECTION_OPTIONS.id,
time = DEFAULT_IDLE_DETECTION_OPTIONS.time,
awayOnWindowBlur = DEFAULT_IDLE_DETECTION_OPTIONS.awayOnWindowBlur,
}: UseIdleDetectionOptions = {}) => {
const idleRef = useRef(false);
const dispatchIdle = useStableCallback(() => {
if (idleRef.current) return;
document.dispatchEvent(new Event(`${id}_idle`));
document.dispatchEvent(
new CustomEvent(`${id}_change`, {
detail: { isIdle: true },
}),
);
idleRef.current = true;
});
const dispatchActive = useStableCallback(() => {
if (!idleRef.current) return;
document.dispatchEvent(new Event(`${id}_active`));
document.dispatchEvent(
new CustomEvent(`${id}_change`, {
detail: { isIdle: false },
}),
);
idleRef.current = false;
});
useEffect(() => {
let interval: ReturnType<typeof setTimeout>;
const handleIdle = () => {
dispatchActive();
clearTimeout(interval);
interval = setTimeout(() => {
dispatchIdle();
}, time);
};
handleIdle();
events.forEach((key) => document.addEventListener(key, handleIdle));
return () => {
clearTimeout(interval);
events.forEach((key) => document.removeEventListener(key, handleIdle));
};
}, [dispatchActive, dispatchIdle, time]);
useEffect(() => {
if (!awayOnWindowBlur) {
return;
}
window.addEventListener('blur', dispatchIdle);
return () => {
window.removeEventListener('blur', dispatchIdle);
};
}, [awayOnWindowBlur, dispatchIdle]);
};