|
| 1 | +import { |
| 2 | + PropsWithChildren, |
| 3 | + useCallback, |
| 4 | + useEffect, |
| 5 | + useRef, |
| 6 | + useState, |
| 7 | +} from "react" |
| 8 | +import { Dialog, DialogBackdrop, DialogPanel } from "@headlessui/react" |
| 9 | +import classNames from "classnames" |
| 10 | + |
| 11 | +import { isPopupMessage, isPopupResizeMessage } from "./utils" |
| 12 | +import { Button, ButtonProps } from "../components/Button" |
| 13 | + |
| 14 | +import type { Step } from "./types" |
| 15 | + |
| 16 | +export function DonatePopup({ |
| 17 | + children, |
| 18 | + ...buttonProps |
| 19 | +}: PropsWithChildren<ButtonProps>) { |
| 20 | + const iframeRef = useRef<HTMLIFrameElement>(null) |
| 21 | + |
| 22 | + const [open, setOpen] = useState(false) |
| 23 | + const [currentStep, setCurrentStep] = useState<Step>("loading") |
| 24 | + |
| 25 | + const handleOpen = useCallback(() => { |
| 26 | + setOpen(true) |
| 27 | + setCurrentStep("loading") |
| 28 | + }, []) |
| 29 | + const handleClose = () => { |
| 30 | + setOpen(false) |
| 31 | + setTimeout(() => setCurrentStep("loading"), 300) |
| 32 | + } |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const handleMessage = (event: MessageEvent) => { |
| 36 | + const message = event.data |
| 37 | + if (!isPopupMessage(message)) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if (isPopupResizeMessage(message)) { |
| 42 | + iframeRef.current?.style.setProperty("height", `${message.height}px`) |
| 43 | + return |
| 44 | + } |
| 45 | + switch (message.action) { |
| 46 | + case "checkout-loaded": { |
| 47 | + setCurrentStep("checkout") |
| 48 | + break |
| 49 | + } |
| 50 | + case "checkout-complete": { |
| 51 | + handleClose() |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + window.addEventListener("message", handleMessage) |
| 57 | + return () => { |
| 58 | + window.removeEventListener("message", handleMessage) |
| 59 | + } |
| 60 | + }, []) |
| 61 | + |
| 62 | + const handleIframeLoad = () => { |
| 63 | + setCurrentStep("choose") |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <Button {...buttonProps} onClick={handleOpen}> |
| 69 | + {children} |
| 70 | + </Button> |
| 71 | + <Dialog |
| 72 | + open={open} |
| 73 | + onClose={handleClose} |
| 74 | + transition |
| 75 | + className="transition-opacity opacity-0 data-[open]:opacity-100" |
| 76 | + > |
| 77 | + <DialogBackdrop className="fixed inset-0 bg-black/30" /> |
| 78 | + <div className="fixed inset-0 flex w-screen items-center justify-center p-4"> |
| 79 | + <DialogPanel |
| 80 | + className={classNames( |
| 81 | + "w-full bg-white rounded-md overflow-hidden flex items-center justify-center transition-all relative", |
| 82 | + currentStep === "loading" && "p-4 min-h-40 max-w-md", |
| 83 | + currentStep === "choose" && "max-w-md", |
| 84 | + currentStep === "checkout" && "max-w-5xl" |
| 85 | + )} |
| 86 | + > |
| 87 | + <iframe |
| 88 | + src="/donate" |
| 89 | + className={classNames( |
| 90 | + "w-full transition-all", |
| 91 | + currentStep === "loading" && "hidden" |
| 92 | + )} |
| 93 | + ref={iframeRef} |
| 94 | + onLoad={handleIframeLoad} |
| 95 | + ></iframe> |
| 96 | + {currentStep === "loading" && ( |
| 97 | + <p className="text-center">Loading...</p> |
| 98 | + )} |
| 99 | + <button |
| 100 | + className={classNames( |
| 101 | + "absolute top-2 right-2", |
| 102 | + currentStep === "checkout" && "text-white" |
| 103 | + )} |
| 104 | + onClick={handleClose} |
| 105 | + > |
| 106 | + x |
| 107 | + </button> |
| 108 | + </DialogPanel> |
| 109 | + </div> |
| 110 | + </Dialog> |
| 111 | + </> |
| 112 | + ) |
| 113 | +} |
0 commit comments