1.0.1 • Published 1 year ago

@sarmadnawaz-testing-workspace/fastn v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@fastn-ai/react

A React library for seamlessly embedding Fastn in your React applications.

Installation

Install the package via npm:

npm install @fastn-ai/react

Usage

Setup Application Context

To begin, wrap your application in the FastnProvider and provide the required configuration:

import { useState } from "react";
import { FastnProvider } from "@fastn-ai/react";

function App() {
  const [config] = useState({
    projectId: "your-project-id",
    tenantId: "your-tenant-id",
    authToken: "your-auth-token",
    env: "your-environment", // e.g., "LIVE", "DRAFT"
    subscriptionId: "your-subscription-id",
  });

  return (
    <FastnProvider config={config}>
      {/* Your components go here */}
    </FastnProvider>
  );
}

Application Configuration Properties

Prop NameTypeTypeDescription
projectIdstringYesThe ID of the project whose widgets/connectors you want to embed.
authTokenstringYesThe authentication token used to authenticate your application users.
tenantIdstringNoThe ID of the tenant (e.g., user, organization, etc.) (optional).
envstringNoThe environment of the widget ("LIVE" or other values) (optional).
subscriptionIdstringNoID of the subscription for connectors configurations

Embedding Connector Widgets

Use the useConnectors hook to fetch and display connector information in your application.

import { useConnectors } from "@fastn-ai/react";

import { useConnectors } from "@sarmadnawaz-testing-workspace/fastn";

const ConnectorsList = () => {
  const { connectors, loading, error } = useConnectors();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      {connectors?.length ? (
        connectors.map((connector) => (
          <div key={connector.id}>{/** Your Connector Card Component */}</div>
        ))
      ) : (
        <p>No connectors found</p>
      )}
    </div>
  );
};

cHook Return Properties

Prop NameTypeDescription
connectorsarrayAn array of connectors objects.
loadingbooleanA boolean value to indicate if the connectors are being loaded.
errorstringAn error message if the connectors failed to load.
Connector Object Structure
Prop NameTypeDescription
idstringThe ID of the connector.
namestringThe name of the connector. (i.e slack)
statusstringThe status of the connector "ACTIVE" or "INACTIVE".
imageUristringThe URI of the connector image.
descriptionstringThe description of the connector.
actionsarrayAn array of actions objects.
Action Object Structure
Prop NameTypeDescription
namestringThe name of the action.
onClickfunctionFunction to handle click events, returning a Promise.
actionTypestringThe tell about the action type. "ACTIVATION" or "DEACTIVATION".
formobjectForm metadata, required for actions needing user input.
onSubmitfunctionSubmit handler function, required if the action has a form.

Form Object

Prop NameTypeDescription
fieldsarrayArray of field objects defining the form inputs.
descriptionstringDescription or instructions for the form.
submitButtonLabelstringLabel for the form's submit button.
Field Object Structure
Prop NameTypeDescription
namestringName of the field.
labelstringLabel for the field.
initialValuestringDefault value of the field.
requiredbooleanIndicates if the field is required.
typestringInput type, e.g., "text", "number", "email".
hiddenbooleanIf true, the field will be hidden.
disabledbooleanIf true, the field will be disabled.

Embedding Connector Configuration

Use the useConfigurations hook to fetch and display connector configurations in your application.

import { useConfigurations } from "@fastn-ai/react";

const ConfigurationsList = () => {
  const { configurations, loading, error } = useConfigurations();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      {configurations?.length ? (
        configurations.map((configuration) => (
          <div key={configuration.id}>
            {/** Your Configuration Card Component */}
          </div>
        ))
      ) : (
        <p>No configurations found</p>
      )}
    </div>
  );
};

cHook Return Properties

Prop NameTypeDescription
configurationsarrayAn array of configurations objects.
loadingbooleanA boolean value to indicate if the configurations are being loaded.
errorstringAn error message if the configurations failed to load.
Configuration Object Structure
Prop NameTypeDescription
namestringThe name of the connector.
statusstringThe status of the configuration "ENABLED" or "DISABLED".
descriptionstringThe description of the connector.
imageUristringThe URI of the connector image.
actionsarrayAn array of actions objects.
Action Object Structure
Prop NameTypeDescription
namestringThe name of the action.
onClickfunctionFunction to handle click events, returning a Promise.
actionTypestringThe tell about the action type. "ENABLE" or "DISBALE".
formobjectForm metadata, required for actions needing user input.
onSubmitfunctionSubmit handler function, required if the action has a form.

Embedding Configuration Form

Use the ConfigurationForm component to display a form for configuring a connector. The wrapper component should be displayed only after the onClick handler for the enable action has been successfully completed, so the user can activate the specific connector.ß For updates, the component should simply be displayed when the Update button is clicked. The update functionality is not part of the actions array.

import { ConfigurationForm } from "@fastn-ai/react";

const ConfigurationFormWrapper = ({
  configuration,
}: {
  configuration: any,
}) => {
  return (
    <ConfigurationForm
      configuration={configuration}
      onComplete={onClose}
      onCancel={onClose}
      primaryButtonAs={(props) => (
        <button {...props}>{props?.isLoading ? "Loading..." : "Save"}</button>
      )}
      secondaryButtonAs={(props) => <button {...props}>Cancel</button>}
    />
  );
};

Need Help?

If you have any questions or need help, please contact us at support@fastn.ai.