npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-idle-timer

v1.1.3

Published

A React hook to handle user inactivity and display a warning before logging out.

Downloads

275

Readme

useIdleTimer

A React hook to handle user inactivity and display a warning before logging out.

Installation

You can install use-idle-timer using npm:

    npm install use-idle-timer

Or using yarn

    yarn add use-idle-timer

Usage

import React from "react";
import { useNavigate } from "react-router-dom";

//Hook
import useIdleTimer from "use-idle-timer";

export default function Layout() {
  const navigate = useNavigate();

  const handleIdle = () => {
    console.log("Session expired");
    sessionStorage.clear();
    navigate("/");
  };

  /*If you want to use the hook with the default sweetalert 
    ( sweetAlertOptions = {
        position: "center",
        icon: "info",
        confirmButtonText: "Ok",
        timer: 3000,
    })
  */
  useIdleTimer(
    80000, // Duration(ms) before user is considered inactive
    handleIdle, // Function called when the user is inactive
    "Inactive Session", // Title of the SweetAlert
    "Your session will expire in 60 seconds if you do not interact with the system.", // Message of the SweetAlert
    60000 // Time to show the warning before the session expires (ms)
  );

  // Another way to use the hook by customizing the sweetalert
  useIdleTimer(
    60000,
    handleIdle,
    "Inactive Session",
    "Your session will expire in 60 seconds if you do not interact with the system..",
    30000,
    //Config of sweetalert
    {
      position: "center",
      icon: "info",
      confirmButtonText: "Aceptar",
      timer: 20000, // If you set the timer to 0 then the sweetalert will not have a timer
    }
  );

  return (
    <div>
      {/* Content of your component */}
      <h1>Welcome to the Layout</h1>
    </div>
  );
}

Props

useIdleTimer(timeoutDuration, onIdle, titleSweetAlert, textSweetAlert, warningTime, sweetAlertOptions)
  • timeoutDuration (number): Duration in milliseconds before the user is considered inactive.
  • onIdle (function): The function that is called when the user is inactive.
  • titleSweetAlert (string): The title of the SweetAlert shown when the user is inactive.
  • textSweetAlert (string): The message of the SweetAlert displayed.
  • warningTime (number): Duration in milliseconds before the warning is shown.
  • sweetAlertOptions (object): Additional options to customize the SweetAlert (optional).

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Jurguen Monge

Acknowledgments

  • React - The JavaScript library used.
  • SweetAlert2 - For displaying elegant alerts.