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

ocrolus-widget-react

v1.0.12

Published

React interface for initializing and rendering the Ocrolus upload widget.

Downloads

34

Readme

Ocrolus React Widget

Installation

Install the package using npm:

npm install ocrolus-widget-react

Usage

There's two entry points for the widget for react developers the component OcrolusUpload and the hook useWidget.

OcrolusUpload Component

The Ocrolus Upload component does not wrap any JSX around the component you pass as a child. It simply makes the component inside onClick open the widget.

Props

  • widgetUuid (required): A unique identifier for the widget.
  • token (optional): An authentication token.
  • onClose (optional): Callback function called when the upload is closed. This will return how many files were uploaded.
  • onOpen (optional): Callback function that must return a promise. The purpose of this method is to execute just in time token fetching prior to opening the widget. In order to get just in time initialization return the access token in this method. If nothing is returned, the stateful token value will be passed into the config.
  • onError (optional): Callback function called in case of an error. This will return errors defined in WIDGET_ERROR if the library or the jwt token initialization fails.
  • loadingElement: (optional): React.ComponentType What to render while the library is initializing.
  • readyObserver (optional): Callback to observe the readiness state. This will be called when ready is true.

Example

import { OcrolusUpload } from "ocrolus-upload-package";

const MyComponent = () => {
  const [error, setError] = useState<boolean>(false);
  const [token, setToken] = useState<string>();
  const handleUploadClose = (data) => {
    console.log("Upload closed. Uploaded file count:", data.uploadedFileCount);
  };
  const handleError = (error) => {
    // Do something with error data
    setError(true)
  }
  const handleFetch = async () => {
    const response = await fetch("https://www.myTokenServer.com")
    const accessToken = await response.json()
    setToken(accessToken)
    return accessToken
  }

  return (
    error ? 
        {/* Your error content here */}
    : <OcrolusUpload
      widgetUuid="your-unique-uuid"
      token={token}
      onOpen={handleFetch}
      onClose={handleUploadClose}
      onError={handleError}
      readyObserver={(isReady) => console.log("Widget is ready:", isReady)}
    >
      {/* Your component content goes here */}
    </OcrolusUpload>
  );
};

export default MyComponent;

useWidget Hook

The useWidget hook provides a set of utility functions for managing the Ocrolus upload widget.

Example

import { useWidget } from 'ocrolus-upload-package';

const MyComponent = () => {
  const { ready, error, exit, open } = useWidget({
    widgetUuid: 'your-unique-uuid',
    token: 'your-auth-token',
    onClose: (data) => console.log('Upload closed. Uploaded file count:', data.uploadedFileCount),
    onError: (error) => console.error('Error during upload:', error),
  });

  // Use the hook values as needed

  return (
    <div>
      { ready ? /* Your component content goes here */ : /* Your Loading placeholder */ }
      <button onClick={open}>Open Upload</button>
      <button onClick={exit}>Exit Upload</button>
    </div>
  );
};

export default MyComponent;

Interfaces

OcrolusUploadOptions

An interface specifying the options that can be passed to the OcrolusUpload component.

interface OcrolusUploadOptions {
  widgetUuid: string;
  token?: string;
  onClose?: (data: OnCloseData) => void; // Callback function called when the upload is closed. This will return how many files were uploaded.
  onError?: (error: WidgetError) => void; // Callback function called in case of an initialization error. This will return errors defined in `WIDGET_ERROR` if the library or the jwt token initialization fails.
}

OnCloseData

An interface specifying the data structure passed to the onClose callback.

interface OnCloseData {
  uploadedFileCount: number;
}

OcrolusUploadHookValues

An interface specifying the values returned by the useWidget hook.

interface OcrolusUploadHookValues {
  ready: boolean; // Will return true if the widget bundle downloads, the library initializes after the token is passed to the hook.
  error: WidgetError | null; // Will contain a widget error if anything in initialization fails.
  exit: () => void; // Callback to manually close the widget modal
  open: () => void; // Callback to manually open the widget modal
}

WidgetError

An interface specifying the structure of widget errors.

interface WidgetError {
  cause: string;
  code: string;
}

Constants

WIDGET_ERRORS

A dictionary of predefined widget errors.

const WIDGET_ERRORS: WidgetErrorDictionary = {
  UNABLE_TO_INITIALIZE_LIBRARY: {
    code: WIDGET_EVENTS.INITIALIZE_LIBRARY_FAILURE,
    cause: "Unable to initialize widget library",
  },
  INITIALIZE_JWT_TOKEN_FAILURE: {
    code: WIDGET_EVENTS.INITIALIZE_JWT_TOKEN_FAILURE,
    cause: "Unable to initialize on jwt token",
  },
};

WIDGET_EVENTS

The WIDGET_EVENTS constant provides a set of predefined events used by the Ocrolus upload widget.

export const WIDGET_EVENTS = {
  // EMBED WIDGET CASES
  EMBEDDED_WIDGET_MOUNTED: "EMBEDDED_WIDGET_MOUNTED",
  EMBEDDED_WIDGET_DATA: "EMBEDDED_WIDGET_DATA",
  // FILE UPLOADER CASES
  OPEN_FILE_UPLOADER: "OPEN_FILE_UPLOADER",
  FILE_UPLOADER_MOUNTED: "FILE_UPLOADER_MOUNTED",
  FILE_UPLOADER_DATA: "FILE_UPLOADER_DATA",
  CLOSE_FILE_UPLOADER: "CLOSE_FILE_UPLOADER",
  // PLAID CONNECT CASES
  OPEN_PLAID_CONNECT: "OPEN_PLAID_CONNECT",
  PLAID_CONNECTED_MOUNTED: "PLAID_CONNECTED_MOUNTED",
  PLAID_CONNECTED_DATA: "PLAID_CONNECTED_DATA",
  CLOSE_PLAID_CONNECT: "CLOSE_PLAID_CONNECT",
  // JWT CASES
  INITIALIZE_JWT_TOKEN_SUCCESS: "INITIALIZE_JWT_TOKEN_SUCCESS",
  INITIALIZE_JWT_TOKEN_FAILURE: "INITIALIZE_JWT_TOKEN_FAILURE",
  // PREVIEW MODE CASES
  PREVIEW_MODE_MOUNTED: "PREVIEW_MODE_MOUNTED",
  PREVIEW_MODE_DATA: "PREVIEW_MODE_DATA",
  UPDATE_PREVIEW_MODE: "UPDATE_PREVIEW_MODE",
};