import React, { memo, useCallback, useEffect, useRef, useState } from "react";
import { WebView, WebViewMessageEvent } from "react-native-webview";
import { Box, Circle, HStack, Stack, Text, VStack } from "native-base";
import { AsyncStorage, isMobile as deviceDetect } from "@gooddollar/web3sdk-v2";
import { CentreBox } from "../../core/layout/CentreBox";
import { AnimatedProgress } from "../../core/animated";
import { Divider } from "native-base";
import { BaseButton } from "../../core";
import { useWindowFocus } from "../../hooks";
export type OnramperCallback = (event: WebViewMessageEvent) => void;
const stepValues = [0, 0, 50, 50, 100, 100];
const useStepValues = (step: number, animationDuration = 1000) => {
const [progressValue, setProgressValue] = useState(0);
const animationDurationRef = useRef(animationDuration);
useEffect(() => {
let intervalId: any;
if (step > 0) {
intervalId = setInterval(() => {
// reset to old step end (current step start) then set to step end again
[-1, 0].forEach(shift => setProgressValue(stepValues[step + shift]));
}, animationDurationRef.current);
} else {
setProgressValue(0);
}
return () => {
if (intervalId) {
clearInterval(intervalId);
}
};
}, [step]);
return progressValue;
};
const StepsProgress = ({ step }: { step: number }) => {
const progressValue = useStepValues(step);
return (
);
};
const Stepper = memo(({ step = 0 }: { step: number }) => (
0 ? "gdPrimary" : "goodGrey.300"}>
0 ? "white" : "goodGrey.700"}> 1
Buy cUSD
2
We swap cUSD to G$
3
Done
));
export const Onramper = ({
onEvent,
onGdEvent,
step,
setStep,
isTesting,
apiKey,
widgetParams = { onlyCryptos: "CUSD_CELO", isAddressEditable: false },
targetNetwork = "CELO",
targetWallet
}: {
onEvent?: OnramperCallback;
onGdEvent: (action: string) => void;
step: number;
setStep: (step: number) => void;
isTesting: boolean;
widgetParams?: any;
targetWallet?: string;
targetNetwork?: string;
apiKey?: string;
}) => {
const url = new URL("https://buy.onramper.com/");
if (apiKey) {
url.searchParams.set("apiKey", apiKey);
}
url.searchParams.set("networkWallets", `${targetNetwork}:${targetWallet}`);
Object.entries(widgetParams).forEach(([k, v]: [string, any]) => {
url.searchParams.append(k, v);
});
const { title } = useWindowFocus();
const uri = url.toString();
const isMobile = deviceDetect();
// on page load check if a returning user is awaiting funds
useEffect(() => {
const isOnramping = async () => {
const isOnramping = await AsyncStorage.getItem("gdOnrampSuccess");
if (isOnramping === "true") {
setStep(2);
}
};
void isOnramping();
}, []);
useEffect(() => {
if (title === "Onramper widget" && step === 0) {
onGdEvent("buy_start");
setStep(1);
}
}, [title, step]);
const devNextStep = useCallback(() => {
setStep(step + 1);
}, [step]);
const resetStep = useCallback(() => {
setStep(0);
}, [step]);
if (!targetWallet) {
return <>>;
}
return (
{isTesting && (
)}
{isMobile && }
);
};