Skip to content

Commit

Permalink
headless-react: update Provider to use saveTolocalStorage prop #50
Browse files Browse the repository at this point in the history
updates following this breaking change in headless: #49

J=CLIP-1292
TEST=auto&manual

see that unit tests passed. spun up test site and see that convo state persist across tabs.
  • Loading branch information
yen-tt authored Jun 5, 2024
1 parent 0b2f4e9 commit f6c7220
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
14 changes: 2 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/chat-headless-react/THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The following npm packages may be included in this product:

- @yext/[email protected]
- @yext/chat-headless@0.8.0
- @yext/chat-headless@0.9.0

These packages each contain the following license and notice below:

Expand Down
4 changes: 2 additions & 2 deletions packages/chat-headless-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-headless-react",
"version": "0.7.0",
"version": "0.8.0",
"description": "the official React UI Bindings layer for Chat Headless",
"main": "./dist/commonjs/src/index.js",
"module": "./dist/esm/src/index.mjs",
Expand Down Expand Up @@ -39,7 +39,7 @@
"homepage": "https://github.com/yext/chat-headless#readme",
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"@yext/chat-headless": "^0.8.0",
"@yext/chat-headless": "^0.9.0",
"react-redux": "^8.0.5"
},
"peerDependencies": {
Expand Down
20 changes: 11 additions & 9 deletions packages/chat-headless-react/src/ChatHeadlessProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ export function ChatHeadlessProvider(
const { children, config } = props;

const headless = useMemo(() => {
const configWithoutSession = { ...config, saveToSessionStorage: false };
const headless = provideChatHeadless(updateClientSdk(configWithoutSession));
const configWithoutLocalStorage = { ...config, saveToLocalStorage: false };
const headless = provideChatHeadless(
updateClientSdk(configWithoutLocalStorage)
);
return headless;
}, [config]);

return (
<ChatHeadlessInstanceProvider
deferRender={config.saveToSessionStorage}
deferRender={config.saveToLocalStorage}
headless={headless}
>
{children}
Expand All @@ -53,7 +55,7 @@ export function ChatHeadlessProvider(
*/
export type ChatHeadlessInstanceProviderProps = PropsWithChildren<{
// Set this to true when using server-side rendering in conjunction with
// browser-specific APIs like session storage.
// browser-specific APIs like local storage.
deferRender?: boolean;
headless: ChatHeadless;
}>;
Expand All @@ -69,19 +71,19 @@ export function ChatHeadlessInstanceProvider(
props: ChatHeadlessInstanceProviderProps
): JSX.Element {
const { children, deferRender, headless } = props;
// deferLoad is typically used with sessionStorage so that the children won't be
// deferLoad is typically used with localStorage so that the children won't be
// immediately rendered and trigger the "load initial message" flow before
// the state can be loaded from session.
// the state can be loaded from local storage.
const [deferLoad, setDeferLoad] = useState(deferRender);

// sessionStorage is overridden here so that it is compatible with server-
// side rendering, which cannot have browser api calls like session storage
// localStorage is overridden here so that it is compatible with server-
// side rendering, which cannot have browser api calls like local storage
// outside of hooks.
useEffect(() => {
if (!deferRender || !headless) {
return;
}
headless.initSessionStorage();
headless.initLocalStorage();
setDeferLoad(false);
}, [headless, deferRender]);

Expand Down
16 changes: 11 additions & 5 deletions packages/chat-headless-react/tests/headlessProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import {
import { renderToString } from "react-dom/server";
import { useChatState } from "../src/useChatState";

it("only fetches session storage on client-side render", async () => {
it("only fetches local storage on client-side render", async () => {
const win = window;
Object.defineProperty(win, "sessionStorage", {
Object.defineProperty(win, "localStorage", {
value: {
...win.sessionStorage,
...win.localStorage,
getItem: (_: string): string => {
return JSON.stringify({
messages: [{ text: "foobar", source: "BOT" }],
messages: [
{
text: "foobar",
source: "BOT",
timestamp: new Date().toISOString(),
},
],
isLoading: false,
canSendMessage: false,
} satisfies ConversationState);
Expand All @@ -27,7 +33,7 @@ it("only fetches session storage on client-side render", async () => {
const config: HeadlessConfig = {
botId: "123",
apiKey: "1234",
saveToSessionStorage: true,
saveToLocalStorage: true,
};
const str = () =>
renderToString(
Expand Down

0 comments on commit f6c7220

Please sign in to comment.