npm package: @sprinklrjs/business-components-react
Description: React components that external applications can embed in their apps.
Latest version: 0.0.18-beta.2

# Sprinklr Business React Components

React components that external applications can embed in their apps.

## Getting Started

Install the npm package

```
yarn add @sprinklrjs/business-components-react
```

### Usage

```javascript
import { OTAProvider, CaseStream, CaseTimeline, ActionTypes } from '@sprinklrjs/business-components-react';

const OTA = '#####';
// OR
const OTAPromise = fetch('/sprinklr-ota');

const Component = () => {
  const [caseNumber, setCaseNumber] = useState(undefined);

  const handleAction = action => {
    switch (action.type) {
      case ACTION_TYPES.SELECT_CASE:
        setCaseNumber(action.payload.caseNumber);
        break;
      default:
        break;
    }
  };

  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseStream onAction={handleAction} />
      {caseNumber ? <CaseTimeline caseNumber={caseNumber} /> : null}
    </OTAProvider>
  );
};
```

## Components

### OTAProvider

This component is used to provide the access token to all the UI Components. All Srinklr business components must be wrapped with this Provider so that they are able to authenticate and render properly.
Pass to it, either the access token or a promise that resolves to an acccess token.

```javascript
import { OTAProvider, CaseTimeline } from '@sprinklrjs/business-components-react';
const Component = () => {
  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseTimeline caseNumber={caseNumber} />
    </OTAProvider>
  );
};
```

### CaseConversation

This component shows the transcript of the conversation that took place between the Agent/Bot/IVR with the customer. For a live call it updates on a real time basis.

```javascript
import { OTAProvider, CaseConversation } from '@sprinklrjs/business-components-react';
const Component = () => {
  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseConversation caseNumber={caseNumber} />
    </OTAProvider>
  );
};
```

### CaseTimeline

This component shows a list of previous cases that the customer had with the brand.

```javascript
import { OTAProvider, CaseTimeline } from '@sprinklrjs/business-components-react';

const Component = () => {
  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseTimeline caseNumber={caseNumber} />
    </OTAProvider>
  );
};
```

### CaseStream

This component shows a collection of cases basis certain pre-defined condition/conditions i.e. open cases, cases assigned to an agent, all cases etc.

```javascript
import { OTAProvider, CaseStream } from '@sprinklrjs/business-components-react';
const Component = () => {
  const handleAction = action => {
    switch (action.type) {
      case ACTION_TYPES.SELECT_CASE:
        setCaseNumber(action.payload.caseNumber);
        break;
      default:
        break;
    }
  };

  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseStream onAction={handleAction} />
    </OTAProvider>
  );
};
```

The handleAction function expects the following Actions -

```javascript
//When used selects a case
type SelectCaseAction = {
  type: typeof ActionTypes.SELECT_CASE,
  payload: {
    caseNumber: number,
  },
};
//When user clicks on collapse stream icon
type CollapseAction = {
  type: typeof ActionTypes.COLLAPSE_CASE_STREAM,
};
CaseStreamAction = SelectCaseAction | CollapseAction;
```

### CaseContactInsights

This component takes customer past interactions with the brand into account to generate useful insights. For example: Customer’s preferred mode of contact.

```javascript
import { OTAProvider, CaseContactInsights } from '@sprinklrjs/business-components-react';
const Component = () => {
  return (
    <OTAProvider ota={OTAPromise /* OR OTA */}>
      <CaseContactInsights caseNumber={caseNumber} />
    </OTAProvider>
  );
};
```




### Voice Widget

This component integrates voice widget which allows agents receive inbound voice calls as well as allows them to make outbound voice calls.

```javascript

import {OTAProvider, Voice, sprVoice} from '@sprinklrjs/business-components-react';

const Component = () => {
    const handleVoiceInitialization = useCallback(() => {
        sprVoice.subscribe('incomingCall', () => {
            console.log('incoming call');
        })
    }, []);

    return (
        <OTAProvider ota={OTAPromise /* OR OTA */}>
            <Voice widgetId={widgetId} onInitialized={handleVoiceInitialization}/>
        </OTAProvider>
    );
}
```

The widget renders in a collapsed state by default. Clicking on it expands it to show the dial-pad.  
When an incoming call is received, the widget expands to provide CTAs to accept or reject the call.  
When a call is connected, call controls will appear.
  

#### Triggering commands imperatively via `sprVoice`

`sprVoice` can be used to expand or collapse the voice widget. It also allows subscribing to events happening inside the voice widget.

```javascript
import {sprVoice} from '@sprinklrjs/business-components-react';
``` 

1. `sprVoice.open()` - To expand the voice widget.
2. `sprVoice.close()` - To collapse the voice widget.
3. `sprVoice.subscribe(event, callback)` - To subscribe to `event`s (`'incomingCall' | 'opened' | 'closed'`) emitted from the voice widget. Returns a function to unsubscribe. Note that the subscriptions should be setup only after the widget has been initialized ( use `onInitialized` prop ).


## Useful Links

- [npm package](https://www.npmjs.com/package/@sprinklrjs/business-components-react)