2022-09-13 10:59:53 +00:00
|
|
|
import dynamic from "next/dynamic";
|
2022-09-05 21:10:58 +00:00
|
|
|
import { Dispatch, useState, useEffect } from "react";
|
|
|
|
import { JSONObject } from "superjson/dist/types";
|
|
|
|
|
|
|
|
export type Gate = undefined | "rainbow"; // Add more like ` | "geolocation" | "payment"`
|
|
|
|
|
|
|
|
export type GateState = {
|
|
|
|
rainbowToken?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type GateProps = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
gates: Gate[];
|
2022-10-14 16:24:43 +00:00
|
|
|
appData: JSONObject;
|
2022-09-05 21:10:58 +00:00
|
|
|
dispatch: Dispatch<Partial<GateState>>;
|
|
|
|
};
|
|
|
|
|
2022-09-13 10:59:53 +00:00
|
|
|
const RainbowGate = dynamic(() => import("@calcom/app-store/rainbow/components/RainbowKit"));
|
|
|
|
|
2022-09-05 21:10:58 +00:00
|
|
|
// To add a new Gate just add the gate logic to the switch statement
|
2022-10-14 16:24:43 +00:00
|
|
|
const Gates: React.FC<GateProps> = ({ children, gates, appData, dispatch }) => {
|
2022-09-05 21:10:58 +00:00
|
|
|
const [rainbowToken, setRainbowToken] = useState<string>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
dispatch({ rainbowToken });
|
|
|
|
}, [rainbowToken, dispatch]);
|
|
|
|
|
|
|
|
let gateWrappers = <>{children}</>;
|
|
|
|
|
|
|
|
// Recursively wraps the `gateWrappers` with new gates allowing for multiple gates
|
|
|
|
for (const gate of gates) {
|
|
|
|
switch (gate) {
|
|
|
|
case "rainbow":
|
2022-10-14 16:24:43 +00:00
|
|
|
if (appData.blockchainId && appData.smartContractAddress && !rainbowToken) {
|
2022-09-05 21:10:58 +00:00
|
|
|
gateWrappers = (
|
|
|
|
<RainbowGate
|
|
|
|
setToken={setRainbowToken}
|
2022-10-14 16:24:43 +00:00
|
|
|
chainId={appData.blockchainId as number}
|
|
|
|
tokenAddress={appData.smartContractAddress as string}>
|
2022-09-05 21:10:58 +00:00
|
|
|
{gateWrappers}
|
|
|
|
</RainbowGate>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gateWrappers;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Gates;
|