Skip to content

Commit

Permalink
refactor(make actions better): make actions work with all actions, no…
Browse files Browse the repository at this point in the history
…t just init
  • Loading branch information
rileylnapier committed Nov 30, 2023
1 parent 99152d9 commit 00eead7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
28 changes: 2 additions & 26 deletions packages/components/src/components/CourierSdk.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef } from "react";
import React, { useEffect } from "react";
import { useCourier } from "@trycourier/react-provider";
import { useInbox, usePreferences } from "@trycourier/react-hooks";

Expand All @@ -8,13 +8,7 @@ export const CourierSdk: React.FunctionComponent<{
toast: boolean;
preferences: boolean;
};
}> = ({ activeComponents, children }) => {
const ref = useRef({
inbox: false,
toast: false,
preferences: false,
});

}> = ({ children }) => {
const courier = useCourier();
const inbox = useInbox();
const preferences = usePreferences();
Expand Down Expand Up @@ -51,23 +45,5 @@ export const CourierSdk: React.FunctionComponent<{
};
}, [courier]);

useEffect(() => {
for (const component of Object.keys(activeComponents)) {
const typedComponent = component as "inbox" | "toast";

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

const initActions =
window?.courier?.__actions?.[`${typedComponent}/init`] ?? [];
for (const initAction of initActions) {
initAction();
}

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

return <>{children}</>;
};
9 changes: 9 additions & 0 deletions packages/components/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
}
</style>
<script type="text/javascript">
window.courierAsyncInit = () => {
window.courier.on(
"preferences/FETCH_PREFERENCE_PAGE/DONE",
(payload) => {
console.log("FETCH_PREFERENCE_PAGE", payload);
}
);
};

const fontWeights = {
thin: 200,
normal: 400,
Expand Down
16 changes: 15 additions & 1 deletion packages/components/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ import { InboxProps } from "@trycourier/react-inbox";
import { ToastProps } from "@trycourier/react-toast";
import { UsePreferences } from "@trycourier/react-hooks";

const middleware = () => (next) => (action) => {
const _action =
window?.courier?.__actions?.[action.type.toLowerCase()] ??
window?.courier?.__actions?.[action.type] ??
[];

for (const __a of _action) {
__a(action.payload);
}

next(action);
};

declare global {
interface Window {
courier: {
__actions: {
[action: string]: Array<() => void>;
[action: string]: Array<(payload: any) => void>;
};
toast?: {
mergeConfig?: (config: ToastProps) => void;
Expand Down Expand Up @@ -111,6 +124,7 @@ const initCourier = (courierConfig?: ICourierConfig) => {
userId={userId}
userSignature={userSignature}
wsOptions={wsOptions}
middleware={[middleware]}
>
<CourierComponents />
</CourierProvider>,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-provider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const CourierProvider: React.FunctionComponent<
userId,
});

const middleware = [..._middleware, ...defaultMiddleware];
const middleware = [...defaultMiddleware, ..._middleware];
const useReducer = useCallback(
createReducer<any, Partial<ICourierContext>>(...middleware),
[_middleware]
Expand Down

0 comments on commit 00eead7

Please sign in to comment.