import { useEffect, useRef, useState } from "react";
import { observer } from "mobx-react-lite";
import QRCodeStyling from "qr-code-styling";
import styled from "styled-components";
import Popup from "../Popup";
import { WC_ICON } from "../../OmniConnector";
interface WCPopupProps {
title: string;
uri: string;
icon?: string;
deeplink?: string;
onClose: () => void;
}
export const WCPopup = observer(({ title, uri, icon, deeplink, onClose }: WCPopupProps) => {
const [copyText, setCopyText] = useState("Copy Link");
const [iconUrl, setIconUrl] = useState(WC_ICON);
const qrCodeRef = useRef(null);
const [qrCode] = useState(
new QRCodeStyling({
data: uri,
dotsOptions: { color: "#eeeeee", type: "dots" },
backgroundOptions: { color: "transparent" },
shape: "square",
width: 340,
height: 340,
type: "svg",
})
);
useEffect(() => {
qrCode.update({ data: uri });
qrCode.append(qrCodeRef.current as HTMLElement);
}, [uri, qrCode]);
useEffect(() => {
if (!iconUrl) return;
qrCode.update({ image: iconUrl, imageOptions: { margin: 8, imageSize: 0.2 } });
}, [iconUrl]);
useEffect(() => {
fetch(icon || WC_ICON)
.then((res) => res.blob())
.then(() => setIconUrl(icon || WC_ICON));
}, [icon]);
const handleCopy = async () => {
if (uri && uri !== "LOADING") {
await navigator.clipboard.writeText(uri);
setCopyText("Link Copied");
setTimeout(() => {
setCopyText("Copy Link");
}, 2000);
}
};
return (
{title}
} onClose={onClose}>
{copyText}
{!!deeplink && (
window.open(deeplink, "_blank")}>
Open in {title}
)}
);
});
export const QrCode = styled.div`
background: url("https://app.hot-labs.org/assets/QR.svg") center center / cover no-repeat;
position: relative;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto;
width: 400px;
height: 400px;
flex-shrink: 0;
canvas {
transform: translate(1px, 22px);
}
`;
export const CopyButton = styled.div`
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
border: 1px solid #505357;
transition: background 0.2s ease-in-out;
background: #282c30;
padding: 8px;
border-radius: 16px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
margin: auto;
flex-shrink: 0;
&:hover {
background: #383c40;
}
`;