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

@statflo/widget-sdk

v0.4.8

Published

SDK for building widgets with Statflo and beyond

Downloads

378

Readme

codecov npm version CI/CD contributions welcome

Table of Contents

Installation

npm install @statflo/widget-sdk # or yarn add @statflo/widget-sdk

Creating a widget

Please see our Examples.

Initializing the widget store

Native

import useWidgetStore from "@statflo/widget-sdk";

React

import useWidgetStore from "@statflo/widget-sdk";
import { create } from "zustand";

const useBoundWidgetStore = create(useWidgetStore);

Publishing an event

Native

import { WidgetEvent } from "@statflo/widget-sdk";

const { publishEvent } = useWidgetStore.getState();

publishEvent(new WidgetEvent("MESSAGE_UPDATED", "<YOUR MESSAGE>"));

React

import { WidgetEvent } from "@statflo/widget-sdk";

const { publishEvent } = useBoundWidgetStore((state) => state);

useEffect(() => {
  // This event only fires on component initialization
  publishEvent(new WidgetEvent("MESSAGE_UPDATED", "<YOUR MESSAGE>"));
}, []);

Listening to an event

Native

useWidgetStore.subscribe((state) => {
  const latest = state.getLatestEvent();

  if (latest) {
    switch (latest.type) {
      case "MESSAGE_UPDATED":
        // ...
        break;
    }
  }
});

React

const { events, getLatestEvent } = useBoundWidgetStore((state) => state);

useEffect(() => {
  const latest = getLatestEvent();

  if (!latest) {
    return;
  }

  switch (latest.type) {
    case "MESSAGE_UPDATED":
      // ...
      break;
  }
}, [events]);

Events API

Below are the details for all the events that are currently supported by Statflo.

Outgoing Events

The following events can be published from the widget so that the app can trigger certain functionality.

Expand Iframe

type: "EXPAND_IFRAME"
data: {
  name: string;
  expand: boolean;
}

Returns an object with the name of the widget and a true or false value depending on whether the iframe should be considered in an expanded state or not. If true is returned this will trigger the Container Height event.

Show Alert

type: "SHOW_ALERT"
data: AlertDetails

Returns details for an alert to be shown by the app. The alert will appear in the bottom right corner of the screen and automatically disappear after 5 seconds.

type AlertDetails = {
  status: "info" | "warning" | "dark" | "light" | "white" | "neutral" | "success" | "error";
  text: string;
}

Append Message

type: "APPEND_MESSAGE"
data: string

Returns a string that will be appended to the contents of the chat message input. Used in Sendables

Replace Message

type: "REPLACE_MESSAGE"
data: string

Returns a string that will replace the contents of the chat message input. Used in Sendables

Incoming Events

The following event types can be listened to by the widget.

User Authenticated

type: "USER_AUTHENTICATED"
data: User

Returns details about the user currently logged into the app. This event is triggered upon initial authentication.

type User = {
  carrier_id: number;
  dealer_id: number;
  email: string;
  language: string;
}

Authentication Token

type: "AUTHENTICATION_TOKEN"
data: string

Returns the authentication token for the current user. This event is triggered upon initial authentication.

Dark Mode

type: "DARK_MODE"
data: boolean

Returns whether the user has their preferences set to view the app in dark mode or not. This event is triggered upon initial authentication.

Current Account ID (Ban ID)

type: "CURRENT_ACCOUNT_ID"
data: string

Returns the account ID for the conversation that the user currently has open. This event will trigger every time the user changes which conversation they have open.

Container Height

type: "CONTAINER_HEIGHT"
data: number

Returns a number that represents the height (in px) of the element the widget is contained within. This event is triggered by the widget publishing an Expand iFrame event.

Security

To view all MDN's window.postMessage() security concerns click here.

The primary concern in this package is the target origin of the window.postMessage() API as described by MDN:

Always specify an exact target origin, not *, when you use postMessage to send data to other windows. A malicious site can change the location of the window without your knowledge, and therefore it can intercept the data sent using postMessage.

By default, the target origin will be the url of the widget you register with the Statflo API.

Adding a widget to Statflo

Once your widget has been deployed and is ready to be integrated into the application, please reach out to the Statflo team.