npm package: @livechat/developer-ui-react
Description: Typescript library with react components for Text Platform Developer App.
Author: Wojciech Dudek
Latest version: 0.0.191

# <img src="https://platform.labs.text.com/console/favicon.ico" widht="24px" height="24px" /> Text Platform - Developer UI (react)

> Typescript library with react components for Text Platform Developer App

[![mit](https://img.shields.io/badge/license-MIT-blue.svg)](https://choosealicense.com/licenses/mit/)

## 📦 Installation

Use the [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/) package managers to install the package in your project.

```bash
npm install -g @livechat/developer-ui-react
# or
yarn global add @livechat/developer-ui-react
```

## 🚀 Usage

### App

```tsx
import { Button, Card } from "@livechat/design-system-react-components";
import type { AppConfig } from "@livechat/developer-sdk";
import { AppProvider, useApp } from "@livechat/developer-ui-react";

// import app config (aka. manifest) from proper location
// it can be downloaded using 'txdev app pull/clone' (@livechat/developer-cli)
import config from "./livechat.config.json";

function Component() {
  const { app } = useApp();

  return (
    <Card>
      <h1>App is now initialized and ready to use.</h1>
      <h2>Account ID: {app.authorization?.data?.account_id}</h2>
      <Button onClick={() => app.features.reports.sendError("4xx")}>
        Send error
      </Button>
    </Card>
  );
}

function Wrapper() {
  return (
    <AppProvider config={config as unknown as AppConfig}>
      <Component />
    </AppProvider>
  );
}
```

To be able to use `useApp` hook, `Component` must be wrapped with `AppProvider`. It takes config of your app (aka. manifest) and uses it to construct app instance, to run initialization and authorization (if needed) under the hood. If you don't have your app config (aka. manifest) yet, please refer to [@livechat/developer-cli](https://www.npmjs.com/package/@livechat/developer-cli) to see how use Text Platform Developer CLI to pull/clone your app.

Also, have a look at [@livechat/developer-sdk](https://www.npmjs.com/package/@livechat/developer-sdk) to see how you can use app instance and its built-in features f.e. [reporting](https://www.npmjs.com/package/@livechat/developer-sdk#reporting).

### Product widgets (LiveChat and HelpDesk)

```tsx
import { Button, Card } from "@livechat/design-system-react-components";
import {
  useLiveChatDetails,
  withLiveChatDetails,
  LiveChatDetailsProvider,
} from "@livechat/developer-ui-react";

const Widget = ({ widget, customerProfile }: LiveChatDetailsContext) => (
  <div>
    <h1>LiveChat Chat Details widget</h1>
    <Button onClick={() => widget.putMessage("New message")} />
    <Card title="Customer profile">
      <span>Name: {customerProfile?.name ?? "Loading..."}</span>
    </Card>
  </div>
);

const WidgetContainer = () => (
  <LiveChatDetailsProvider>
    {(context) => <Widget {...context} />}
  </LiveChatDetailsProvider>
);

// or

const WidgetContainer = () => (
  <LiveChatDetailsProvider>
    <WidgetWrappedWithHook />
    <WidgetWrappedWithHoc />
  </LiveChatDetailsProvider>
);

const WidgetWrappedWithHook = () => {
  const context = useLiveChatDetails();

  return <Widget {...context} />;
};

const WidgetWrappedWithHoc = withLiveChatDetails(Widget);
```

For now we are supporting widgets:

- LiveChat
  - [Chat Details](https://www.npmjs.com/package/@livechat/agent-app-sdk#details-widget-idetailswidget) - `LiveChatDetailsProvider` and `useLiveChatDetails`
  - [Message Box](https://www.npmjs.com/package/@livechat/agent-app-sdk#messagebox-widget-imessageboxwidget) - `LiveChatMessageBoxProvider` and `useLiveChatMessageBox`
  - [Fullscreen](https://www.npmjs.com/package/@livechat/agent-app-sdk#messagebox-widget-imessageboxwidget) - `LiveChatFullscreenProvider` and `useLiveChatFullscreen`
- HelpDesk
  - [Ticket Details](https://www.npmjs.com/package/@livechat/agent-app-sdk#messagebox-widget-imessageboxwidget) - `HelpDeskDetailsProvider` and `useHelpDeskDetails`
  - [Fullscreen](https://www.npmjs.com/package/@livechat/agent-app-sdk#messagebox-widget-imessageboxwidget) - `HelpDeskFullscreenProvider` and `useHelpDeskFullscreen`

## 📃 License

The code and documentation in this project are released under the [MIT License](https://choosealicense.com/licenses/mit/).