/widget-sdk

JavaScript cross-domain Widget SDK with state updates sync, UI event sync, and a clean declarative API.

Primary LanguageTypeScriptMIT LicenseMIT

codecov npm version CI/CD contributions welcome

Table of Contents

Installation

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

Getting Started

Example Widgets

Two example widgets are provided, one using React and one using vanilla javascript.

React Example Widget

  1. Install the React example widget
yarn --cwd examples/react install
  1. Start the React example widget
yarn --cwd examples/react start
  1. View the React example widget at http://localhost:3001

Vanilla JS Example Widget

  1. Install the Vanilla JS example widget
yarn --cwd examples/vanillajs install
  1. Start the Vanilla JS example widget
yarn --cwd examples/vanillajs start
  1. View the Vanilla JS example widget at http://localhost:3002

Widget Playground

We have provided a live playground at https://statflo.github.io/widget-sdk/conversations where you can test your widgets as you build them. We recommend building the example widgets above and testing them in the playground so you can get familiar with this project.

Suggested Workflow

  1. Check out "Getting Started" guide in the left-hand navigation menu.
  2. Click on "Widget Manager" in the left-hand navigation menu and try adding the example widgets above by clicking "Add Widget" in the top left.
  3. Click on "Conversations" in the left-hand navigation to view your newly added widgets.
  4. Click on "Event Manager" to simulate sending events to your widgets.

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 { useStore } from "zustand";

const publishEvent = useStore(useWidgetStore, (state: WidgetState) => state.publishEvent);

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 = useStore(useWidgetStore, (state: WidgetState) => state.publishEvent);

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.