Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headless-react: update Provider to use saveTolocalStorage prop #50

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/chat-core@0.8.0
- @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
Loading