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

@xtreamsrl/react-feedback-management

v0.3.3

Published

This package exposes a provider GlobalFeedbackProvider and the following hooks: - useGlobalError - useGlobalSuccess - useErrorManagement - useSuccessManagement

Downloads

3

Readme

@xtreamsrl/react-feedback-management

This package exposes a provider GlobalFeedbackProvider and the following hooks:

  • useGlobalError
  • useGlobalSuccess
  • useErrorManagement
  • useSuccessManagement

The library is a wrapper that keeps track of Success and Error. Moreover, it provides functions to update their values.

Installation

npm install @xtreamsrl/react-feedback-management

Usage

Before actually using these library tools it is necessary to define the structure of Error and Success relying on augmentation in file feedback.d.ts (the name is not relevant) with the following structure:

import { Error, Success } from '@xtreamsrl/react-feedback-management';

declare module '@xtreamsrl/react-feedback-management' {

  interface Error {
    /*
    * Here you can define your error type.
    * e.g. */
    code: number;
    message: string
  }

  interface Success {
    /*
    * Here you can define your success type.
    * e.g. */
    message: string
  }
}

GlobalFeedbackProvider

After having defined the Error and Success interfaces using augmentation, it is necessary to wrap the main app with the GlobalFeedbackProvider

import { GlobalFeedbackProvider } from '@xtreamsrl/react-feedback-management';

function App() {
  return <GlobalFeedbackProvider>
    <MainApp/>
  </GlobalFeedbackProvider>
}

With these two steps done, one can use the available hooks as needed.

useErrorManagement

A possible way of using this hook is to show and hide error toasts. Here is an example:

import {useErrorManagement} from "@xtreamsrl/react-feedback-management";
import Snackbar from '@mui/material/Snackbar';
import Alert from '@mui/material/Alert'

const ErrorToast: React.FC = () => {
  const { error, setError } = useErrorManagement();
  const handleClose = () => setError(null);

  return error ? (
    <Snackbar
      data-test="success-toast"
      anchorOrigin={{
        vertical: 'bottom',
        horizontal: 'center',
      }}
      open={error !== null}
      autoHideDuration={3000}
      onClose={handleClose}>
      <Alert sx={{ width: '100%' }} onClose={handleClose} severity="error" variant="filled">
        {error?.message}
      </Alert>
    </Snackbar>
  ) : <button onClick={() => setError({message: 'not ok', code: 1})}>Show</button>;
};

Here the error is set in the component itself using the button, but generally it is set from outside the component, more specifically, in the catch clause of asynchronous operations.

useGlobalError

This hook is useful to globally set an error if an operation fails. An example may be using the return values of a query execution (e.g., error or isError) and pass them to the useGlobalError.

useGlobalError({isError, error: {message: error.message, code: error.code}})

useSuccessManagement

A possible way of using this hook is to show and hide success toasts. Here is an example:

import {useSuccessManagement} from "@xtreamsrl/react-feedback-management";
import Snackbar from '@mui/material/Snackbar';
import Alert from '@mui/material/Alert'

const SuccessToast: React.FC = () => {
  const { success, setSuccess } = useSuccessManagement();
  const handleClose = () => setSuccess(null);

  return success ? (
    <Snackbar
      data-test="success-toast"
      anchorOrigin={{
        vertical: 'bottom',
        horizontal: 'center',
      }}
      open={success !== null}
      autoHideDuration={3000}
      onClose={handleClose}>
      <Alert sx={{ width: '100%' }} onClose={handleClose} severity="success" variant="filled">
        {success?.message}
      </Alert>
    </Snackbar>
  ) : <button onClick={() => setSuccess({message: 'ok'})}>Show</button>;
};

useGlobalSuccess

This hook is useful to globally set a success if an operation succeed. An example may be using the return values of a query execution (e.g., isSuccess) and pass them to the useGlobalSuccess.

useGlobalSuccess({isSuccess, success: {message: 'Operation completed'}})

Who we are