npm install @statflo/widget-sdk # or yarn add @statflo/widget-sdk
Please see our Examples.
import useWidgetStore from "@statflo/widget-sdk";
import useWidgetStore from "@statflo/widget-sdk";
import { create } from "zustand";
const useBoundWidgetStore = create(useWidgetStore);
import { WidgetEvent } from "@statflo/widget-sdk";
const { publishEvent } = useWidgetStore.getState();
publishEvent(new WidgetEvent("MESSAGE_UPDATED", "<YOUR MESSAGE>"));
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>"));
}, []);
useWidgetStore.subscribe((state) => {
const latest = state.getLatestEvent();
if (latest) {
switch (latest.type) {
case "MESSAGE_UPDATED":
// ...
break;
}
}
});
const { events, getLatestEvent } = useBoundWidgetStore((state) => state);
useEffect(() => {
const latest = getLatestEvent();
if (!latest) {
return;
}
switch (latest.type) {
case "MESSAGE_UPDATED":
// ...
break;
}
}, [events]);
Below are the details for all the events that are currently supported by Statflo.
The following events can be published from the widget so that the app can trigger certain functionality.
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.
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;
}
type: "APPEND_MESSAGE"
data: string
Returns a string that will be appended to the contents of the chat message input. Used in Sendables
type: "REPLACE_MESSAGE"
data: string
Returns a string that will replace the contents of the chat message input. Used in Sendables
The following event types can be listened to by the widget.
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;
}
type: "AUTHENTICATION_TOKEN"
data: string
Returns the authentication token for the current user. This event is triggered upon initial authentication.
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.
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.
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.
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.
Once your widget has been deployed and is ready to be integrated into the application, please reach out to the Statflo team.