# @tolstoy-ai/react-native-sdk

Tolstoy shoppable videos and virtual try-on for a bare React Native app. The package does not use Expo.

- Video carousels for the home screen and the product screen. The videos play natively with your `react-native-video`.
- A full-screen video feed with product cards. A tap on a product card calls your `onProductPress(productId, variantId)`.
- A try-on screen in a WebView. The shopper signs in with your app account or with an email.

## Requirements

- React Native 0.81 or later, with the New Architecture and Hermes.
- These peer packages in your app: `react-native-video` (6.16.1 or later) and `react-native-webview` (13.16 or later). Install them and their native parts as their own guides tell you.

## Install

```sh
npm install @tolstoy-ai/react-native-sdk
```

## Use

Put `TolstoyProvider` around the screens that show Tolstoy parts.

```tsx
import {
  TolstoyProvider,
  TolstoyVideoCarousel,
  TolstoyProductVideos,
  TolstoyTryOn,
} from '@tolstoy-ai/react-native-sdk';

<TolstoyProvider
  appKey="<your Tolstoy app key>"
  appUrl="<your-store>.myshopify.com"
  onProductPress={(productId, variantId) => navigation.navigate('Product', { productId, variantId })}
  getAppSession={async () => customerAccountAccessToken ?? null}
  onAddToCart={async (lines) => addToAppCart(lines)}
  onOpenCart={() => navigation.navigate('Cart')}
>
  <TolstoyVideoCarousel publishId="<home player publish id>" />
  <TolstoyProductVideos publishId="<product page player publish id>" productId={product.id} />
</TolstoyProvider>
```

Show the try-on screen full screen, for example in a modal or as its own route:

```tsx
<TolstoyTryOn productId={product.id} onClose={() => navigation.goBack()} />
```

| Prop | Where | Description |
| --- | --- | --- |
| `appKey` | `TolstoyProvider` | Your Tolstoy app key. |
| `appUrl` | `TolstoyProvider` | Your store's `*.myshopify.com` domain. |
| `onProductPress(productId, variantId?)` | `TolstoyProvider` | Opens your product screen. The ids are numeric Shopify ids. |
| `getAppSession()` | `TolstoyProvider`, optional | Gives the signed-in shopper's token (see "Token contract"). Give `null` when no shopper is signed in. |
| `onAddToCart(lines)` | `TolstoyProvider`, optional | Adds lines (`variantId`, `quantity`) to your app's cart. Reject the promise when the add fails. Without this prop, add to cart from try-on shows an error. |
| `onOpenCart()` | `TolstoyProvider`, optional | Opens your cart screen. |
| `environment` | `TolstoyProvider`, optional | `production` (default) or `dev`. |
| `publishId` | carousels | The publish id of a Tolstoy player. Tolstoy gives it to you. |
| `productId` | `TolstoyProductVideos`, `TolstoyTryOn` | The numeric Shopify product id. |
| `widgetId` | `TolstoyTryOn`, optional | Your app's own Tolstoy try-on widget. Tolstoy gives it to you. Without it, try-on uses the store's website try-on widget. |

`TolstoyProductVideos` shows nothing when the product has no videos. `useTolstoyVideos(publishId, productId?)` gives the same data for your own UI.

## Token contract

`getAppSession()` must give the **Shopify Customer Account API access token** of the shopper who is signed in to your app. This is the access token that your app gets from the store's Customer Account API sign-in (OAuth), and it starts with `shcat_`.

- Tolstoy sends the token to the store's own Customer Account API (`https://<store>/.well-known/customer-account-api` gives its address). Shopify tells Tolstoy the shopper's email. Tolstoy then signs the shopper in to try-on.
- Tolstoy does not keep the token.
- Do not give a Storefront API customer access token, an Appbrew token, or your own session token. Tolstoy cannot verify them, and the shopper must then sign in with an email.
- When `getAppSession` is not set, or gives `null`, the shopper signs in with an email on the try-on screen.

## How the try-on screen talks to your app

The try-on screen is a Tolstoy web page in a WebView. The page sends these messages to the package, and the package calls your props:

| Message | Prop |
| --- | --- |
| `product/open` ("View product") | `onProductPress(productId, variantId?)` |
| `cart/add` | `onAddToCart(lines)` |
| `cart/open` | `onOpenCart()` |
| `go/back` (the shopper closes the chat) | the `onClose` of `TolstoyTryOn` |

Try-on works only for a store that has the Tolstoy shopper chat switched on.
