@zean-chat/react 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,13 +10,13 @@ npm install @zean-chat/react
10
10
  import { ZeanChat, useZean } from "@zean-chat/react";
11
11
 
12
12
  // Once, in the root layout or root route.
13
- <ZeanChat appKey="app_your_app_key" getToken={user ? fetchZeanToken : undefined} attributes={{ plan: "pro" }} />;
13
+ <ZeanChat appKey="app_your_app_key" user={user ? { id: user.id, name: user.name, email: user.email } : undefined} attributes={{ plan: "pro" }} />;
14
14
 
15
15
  // Anywhere.
16
16
  const { open, close, toggle, unreadCount, identify, signOut, setAttributes } = useZean();
17
17
  ```
18
18
 
19
- To switch people, render one time without `getToken` in between. Full guide: [install the chat in React and Next.js](https://zean.chat/docs/install-react), [React reference](https://zean.chat/docs/reference-react).
19
+ Passing `user` signs the person in; `undefined` signs them out. Full guide: [install the chat in React and Next.js](https://zean.chat/docs/install-react), [React reference](https://zean.chat/docs/reference-react).
20
20
 
21
21
  ## Why Zean
22
22
 
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import type { ZeanChatProps, ZeanControls } from './interfaces.js';
2
- export type { ZeanAttributes, ZeanLocale, ZeanTokenSource } from '@zean-chat/js';
2
+ export type { ZeanAttributes, ZeanLocale, ZeanUser } from '@zean-chat/js';
3
3
  export type { ZeanChatProps, ZeanControls } from './interfaces.js';
4
4
  /**
5
5
  * Shows the chat. Render it once, near the root, on every page. It renders nothing itself: the chat sits in its own
6
6
  * frame, and stays across page changes.
7
7
  */
8
- export declare function ZeanChat({ appKey, getToken, attributes, locale, loaderUrl }: ZeanChatProps): null;
8
+ export declare function ZeanChat({ appKey, user, attributes, locale, loaderUrl }: ZeanChatProps): null;
9
9
  /** Opens and closes the chat, and follows the unread count. Works in any component, before the chat has loaded. */
10
10
  export declare function useZean(): ZeanControls;
package/dist/index.js CHANGED
@@ -3,23 +3,18 @@
3
3
  // src/index.ts
4
4
  import { useEffect, useRef, useSyncExternalStore } from "react";
5
5
  import { loadZean, zean } from "@zean-chat/js";
6
- function ZeanChat({ appKey, getToken, attributes, locale, loaderUrl }) {
7
- const latestToken = useRef(getToken);
8
- const tokenSource = useRef(() => latestToken.current?.() ?? Promise.resolve(null));
6
+ function ZeanChat({ appKey, user, attributes, locale, loaderUrl }) {
7
+ const userKey = user ? JSON.stringify(user) : null;
9
8
  const identified = useRef(false);
10
- const signedIn = Boolean(getToken);
11
9
  const attributesKey = attributes ? JSON.stringify(attributes) : null;
12
- useEffect(() => {
13
- latestToken.current = getToken;
14
- });
15
10
  useEffect(() => {
16
11
  loadZean({ appKey, locale, loaderUrl });
17
12
  }, [appKey, locale, loaderUrl]);
18
13
  useEffect(() => {
19
- if (signedIn && !identified.current) zean.identify(tokenSource.current);
20
- else if (!signedIn && identified.current) zean.signOut();
21
- identified.current = signedIn;
22
- }, [signedIn]);
14
+ if (userKey) zean.identify(JSON.parse(userKey));
15
+ else if (identified.current) zean.signOut();
16
+ identified.current = userKey !== null;
17
+ }, [userKey]);
23
18
  useEffect(() => {
24
19
  if (attributesKey) zean.setAttributes(JSON.parse(attributesKey));
25
20
  }, [attributesKey]);
@@ -1,10 +1,7 @@
1
1
  import type { ZeanApi, ZeanAttributes, ZeanOptions } from '@zean-chat/js';
2
2
  export interface ZeanChatProps extends ZeanOptions {
3
- /**
4
- * While a person is signed in: returns a token your server signed. Leave it out when no one is; to switch people,
5
- * leave it out once in between.
6
- */
7
- getToken?: ZeanOptions['getToken'];
3
+ /** The person signed in to your app: `{ id, name, email }`. Leave it out when no one is: the chat signs out. */
4
+ user?: ZeanOptions['user'];
8
5
  /** What the page knows about the person, shown on the Contact's profile. */
9
6
  attributes?: ZeanAttributes;
10
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zean-chat/react",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Live chat widget for React and Next.js: add a free customer support chat to your app with one component and a hook. Intercom and Crisp alternative.",
5
5
  "keywords": [
6
6
  "react",