Skip to content

Commit

Permalink
fix(unmount when init): make sure to unmount if we see the component …
Browse files Browse the repository at this point in the history
…has been rendered
  • Loading branch information
rileylnapier committed Aug 22, 2023
1 parent 054bb59 commit d184663
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/client-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/client-api",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"main": "dist/index.js",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/client-graphql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/client-graphql",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ To listen for actions that happen inside Courier's SDK.
title: "Hello",
body: "World",
});
};
});
};
</script>
```
Expand All @@ -123,7 +123,7 @@ To listen for actions that happen inside Courier's SDK.
window.courierAsyncInit = () => {
window.courier.on("inbox/init", () => {
console.log(window.courier.inbox.config);
};
});
};
</script>
```
Expand Down
12 changes: 6 additions & 6 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/components",
"version": "4.2.2",
"version": "4.2.3",
"private": true,
"description": "Beautiful, easy React toast notifications",
"author": "Courier <support@courier.com>",
Expand All @@ -18,11 +18,11 @@
"license": "MIT",
"dependencies": {
"@trycourier/courier": "^1.3.0",
"@trycourier/react-brand-designer": "^4.2.2",
"@trycourier/react-inbox": "^4.2.2",
"@trycourier/react-preferences": "^4.2.2",
"@trycourier/react-provider": "^4.2.2",
"@trycourier/react-toast": "^4.2.2",
"@trycourier/react-brand-designer": "^4.2.3",
"@trycourier/react-inbox": "^4.2.3",
"@trycourier/react-preferences": "^4.2.3",
"@trycourier/react-provider": "^4.2.3",
"@trycourier/react-toast": "^4.2.3",
"babel-loader": "^8.0.6",
"babel-preset-preact": "^2.0.0",
"camel-case": "^4.1.2",
Expand Down
18 changes: 9 additions & 9 deletions packages/components/src/components/CourierSdk.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import React, { useEffect } from "react";
import React, { useEffect, useRef } from "react";
import { useCourier } from "@trycourier/react-provider";
import { useInbox } from "@trycourier/react-hooks";

const didActionsInit = {
inbox: false,
toast: false,
preferences: false,
};

export const CourierSdk: React.FunctionComponent<{
activeComponents: {
inbox: boolean;
toast: boolean;
preferences: boolean;
};
}> = ({ activeComponents, children }) => {
const ref = useRef({
inbox: false,
toast: false,
preferences: false,
});

const courier = useCourier();
const inbox = useInbox();

Expand Down Expand Up @@ -45,7 +45,7 @@ export const CourierSdk: React.FunctionComponent<{
for (const component of Object.keys(activeComponents)) {
const typedComponent = component as "inbox" | "toast";

if (!courier[typedComponent] || didActionsInit[typedComponent]) {
if (!courier[typedComponent] || ref.current[typedComponent]) {
continue;
}

Expand All @@ -55,7 +55,7 @@ export const CourierSdk: React.FunctionComponent<{
initAction();
}

didActionsInit[typedComponent] = true;
ref.current[typedComponent] = true;
}
}, [courier?.inbox, courier?.toast, activeComponents]);

Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const CourierComponents: React.FunctionComponent = () => {

const componentConfigs = window.courierConfig?.components;
const initialInbox = querySelector(window?.document?.body, "courier-inbox");

const [inboxElement, setInboxElement] = useState(initialInbox ?? undefined);

const [inboxConfig, setInboxConfig] = useState({
Expand All @@ -34,6 +35,7 @@ export const CourierComponents: React.FunctionComponent = () => {
});

const initialToast = querySelector(window?.document?.body, "courier-toast");

const [toastElement, setToastElement] = useState(initialToast ?? undefined);

const [toastConfig, setToastConfig] = useState({
Expand All @@ -45,6 +47,7 @@ export const CourierComponents: React.FunctionComponent = () => {
window?.document?.body,
"courier-preferences"
);

const [preferencesElement, setPreferencesElement] = useState(
initialPreferences ?? undefined
);
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
})();
});
};*/

window.courierConfig = {
inboxApiUrl:
"https://3rjq5oe9b1.execute-api.us-east-1.amazonaws.com/dev/q",
Expand Down
16 changes: 11 additions & 5 deletions packages/components/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { render } from "react-dom";
import { render, unmountComponentAtNode } from "react-dom";

import {
CourierProvider,
Expand Down Expand Up @@ -64,7 +64,7 @@ interface ICourierConfig {
preferencePageDraftMode?: boolean;
}

const initCourier = async (courierConfig?: ICourierConfig) => {
const initCourier = (courierConfig?: ICourierConfig) => {
const {
tenantId,
apiUrl,
Expand All @@ -86,9 +86,15 @@ const initCourier = async (courierConfig?: ICourierConfig) => {
if (!userId || !clientKey) {
return;
}
const existingCourierRoot =
document.getElementsByTagName("courier-root")?.[0];

const courierRoot = document.createElement("courier-root");
document.body.appendChild(courierRoot);
if (existingCourierRoot) {
unmountComponentAtNode(existingCourierRoot);
} else {
const courierRoot = document.createElement("courier-root");
document.body.appendChild(courierRoot);
}

render(
<CourierProvider
Expand All @@ -106,7 +112,7 @@ const initCourier = async (courierConfig?: ICourierConfig) => {
>
<CourierComponents />
</CourierProvider>,
courierRoot
document.getElementsByTagName("courier-root")?.[0]
);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/react-brand-designer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-brand-designer",
"version": "4.2.2",
"version": "4.2.3",
"description": "Embeddable brand designer for multi-channel notifications",
"homepage": "https://github.com/trycourier/courier-react/tree/main/packages/react-elements#readme",
"license": "ISC",
Expand Down Expand Up @@ -29,7 +29,7 @@
"url": "https://github.com/trycourier/courier-react/issues"
},
"dependencies": {
"@trycourier/react-elements": "^4.2.2",
"@trycourier/react-elements": "^4.2.3",
"rimraf": "^3.0.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-elements",
"version": "4.2.2",
"version": "4.2.3",
"description": "Shareable components for each Courier package",
"author": "Drew Youngwerth <drew@youngwerth.com>",
"homepage": "https://github.com/trycourier/courier-react/tree/main/packages/react-elements#readme",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-hooks",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
Expand All @@ -20,7 +20,7 @@
"concat-md": "^0.3.5"
},
"dependencies": {
"@trycourier/client-graphql": "^4.2.2",
"@trycourier/client-graphql": "^4.2.3",
"deep-extend": "^0.6.0",
"rimraf": "^3.0.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-inbox-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-inbox-next",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"private": true,
"main": "dist/index.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/react-inbox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-inbox",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
Expand All @@ -20,10 +20,10 @@
"dependencies": {
"@fontsource/poppins": "^4.5.9",
"@tippyjs/react": "^4.2.3",
"@trycourier/client-graphql": "^4.2.2",
"@trycourier/react-elements": "^4.2.2",
"@trycourier/react-hooks": "^4.2.2",
"@trycourier/react-preferences": "^4.2.2",
"@trycourier/client-graphql": "^4.2.3",
"@trycourier/react-elements": "^4.2.3",
"@trycourier/react-hooks": "^4.2.3",
"@trycourier/react-preferences": "^4.2.3",
"classnames": "^2.2.6",
"date-fns": "^2.19.0",
"deep-extend": "^0.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-preferences/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-preferences",
"version": "4.2.2",
"version": "4.2.3",
"main": "dist/index.js",
"types": "typings/index.d.ts",
"scripts": {
Expand All @@ -23,7 +23,7 @@
],
"license": "ISC",
"dependencies": {
"@trycourier/react-hooks": "^4.2.2",
"@trycourier/react-hooks": "^4.2.3",
"react-toggle": "^4.1.2",
"styled-components": "^5.3.6"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-provider",
"version": "4.2.2",
"version": "4.2.3",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
Expand All @@ -16,7 +16,7 @@
},
"license": "ISC",
"dependencies": {
"@trycourier/client-graphql": "^4.2.2",
"@trycourier/client-graphql": "^4.2.3",
"buffer": "^6.0.3",
"jwt-decode": "^3.1.2",
"react-use": "^17.2.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/react-provider/src/hooks/use-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const useTransport = ({
transport: CourierTransport;
}>();

return useMemo(() => {
const newTransport = useMemo(() => {
if (transport) {
return transport;
}
Expand Down Expand Up @@ -86,6 +86,8 @@ const useTransport = ({
}
return newTransport;
}, [tenantId, authorization, clientKey, transport, userSignature, wsOptions]);

return newTransport;
};

export default useTransport;
4 changes: 2 additions & 2 deletions packages/react-toast/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/react-toast",
"version": "4.2.2",
"version": "4.2.3",
"description": "Beautiful, easy React toast notifications",
"main": "dist/index.js",
"types": "typings/index.d.ts",
Expand All @@ -24,7 +24,7 @@
"concat-md": "^0.3.5"
},
"dependencies": {
"@trycourier/react-hooks": "^4.2.2",
"@trycourier/react-hooks": "^4.2.3",
"deep-extend": "^0.6.0",
"markdown-to-jsx": "^7.1.7",
"react-toastify": "^9.1.3",
Expand Down
14 changes: 7 additions & 7 deletions packages/storybook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/storybook",
"version": "4.2.2",
"version": "4.2.3",
"private": true,
"description": "Beautiful, easy React toast notifications",
"author": "Courier <support@courier.com>",
Expand All @@ -23,12 +23,12 @@
"@storybook/addon-links": "^6.5.9",
"@storybook/addon-viewport": "^6.5.9",
"@storybook/react": "^6.5.9",
"@trycourier/components": "^4.2.2",
"@trycourier/react-brand-designer": "^4.2.2",
"@trycourier/react-inbox": "^4.2.2",
"@trycourier/react-preferences": "^4.2.2",
"@trycourier/react-provider": "^4.2.2",
"@trycourier/react-toast": "^4.2.2",
"@trycourier/components": "^4.2.3",
"@trycourier/react-brand-designer": "^4.2.3",
"@trycourier/react-inbox": "^4.2.3",
"@trycourier/react-preferences": "^4.2.3",
"@trycourier/react-provider": "^4.2.3",
"@trycourier/react-toast": "^4.2.3",
"react-frame-component": "^5.2.3",
"react-markdown": "^8.0.1",
"rehype-raw": "^6.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@trycourier/types",
"private": true,
"version": "4.2.2",
"version": "4.2.3",
"main": "index.d.ts",
"types": "index.d.ts",
"scripts": {}
Expand Down

0 comments on commit d184663

Please sign in to comment.