SupremeTechnopriest / react-idle-timer

User activity timer component
https://idletimer.dev
MIT License
1.15k stars 143 forks source link

Is it expected that onActive is not called if actions are fired during prompt window? #286

Closed Slessi closed 2 years ago

Slessi commented 2 years ago

Question

  1. User action before onPrompt => onActive is not called (expected)
  2. User action after onIdle => onActive is called (expected)
  3. User action after onPrompt but before onIdle => onActive is not called (?)

Is scenario 3 expected? I would've thought an action after onPrompt would trigger the onActive event and prevent the onIdle event from being called.

If this is expected, am I meant to call activate inside onAction to get the functionality I am describing?

sandbox (look at console): https://codesandbox.io/s/upbeat-banzai-1ibixc?file=/src/App.js

In below, onActive log was not triggered despite triggering an onAction after onPrompt. onActive is only triggered after onIdle.

image

SupremeTechnopriest commented 2 years ago

Yes that is expected. Once the prompt is fired you are in the prompt state. If you want activity to automatically close the prompt you can hook into the onAction event.

Slessi commented 2 years ago

@SupremeTechnopriest closing the prompt is easy, but it doesn't stop onIdle from firing.

E.g. If I want to show a warning prompt, then after prompt times out logout a user, am I expected to call activate in onAction ?

  const { activate } = useIdleTimer({
    onPrompt: () => showPrompt(),
    onIdle: () => logout(),
    onAction: () => {
      activate(); // Without this, the prompt will close, but onIdle is still called
      closePrompt();
    },
  });
SupremeTechnopriest commented 2 years ago

You can check for isPrompted inside onAction and call activate or reset.