import { Component, useEffect, useId, useRef, useState, type ReactNode } from "react";
import { QRCodeSVG } from "qrcode.react";
import { useCopy } from "../../ui/copy";
import { useAnchoredPopover } from "../../components/living/use-anchored-popover";
import { ChannelAction } from "./ChannelAction";
import { Btn } from "../../components/living/primitives";
import { downloadBlob } from "../../lib/download";
import { Glyph } from "../../ui/icons";
import "./channel-invite-qr-code.css";
class InvitationCode extends Component<{ value: string; label: string; failure: string; children: ReactNode }, { failed: boolean }> {
state = { failed: false };
static getDerivedStateFromError() { return { failed: true }; }
render() {
return this.state.failed ?
{this.props.failure}
: <>{this.props.children}>;
}
}
/** Each invitation owner supplies its exact URL, including the complete fragment. */
export function ChannelInviteQrCode({ url, disabled = false, label, presentation = "popover" }: {
url: string | null | undefined; disabled?: boolean; label: string; presentation?: "popover" | "inline";
}) {
const { t } = useCopy();
const id = useId();
const anchor = useRef(null);
const panel = useRef(null);
const [open, setOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [saveFailed, setSaveFailed] = useState(false);
useAnchoredPopover(anchor, panel, { enabled: presentation === "popover" });
useEffect(() => {
setSaveFailed(false);
if (!url || disabled) { if (panel.current?.matches(":popover-open")) panel.current.hidePopover(); setOpen(false); }
}, [url, disabled]);
const save = async () => {
const svg = panel.current?.querySelector("svg[data-invitation-code]");
if (!(svg instanceof SVGSVGElement)) return;
setSaving(true); setSaveFailed(false);
const imageSvg = svg.cloneNode(true) as SVGSVGElement;
const edge = svg.viewBox.baseVal.width * 8;
imageSvg.setAttribute("width", String(edge)); imageSvg.setAttribute("height", String(edge));
const imageURL = URL.createObjectURL(new Blob([new XMLSerializer().serializeToString(imageSvg)], { type: "image/svg+xml" }));
try {
const image = new Image(); image.src = imageURL;
await image.decode();
const canvas = document.createElement("canvas");
canvas.width = canvas.height = edge;
const context = canvas.getContext("2d");
if (!context) throw new Error("image_unavailable");
context.imageSmoothingEnabled = false;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const png = await new Promise((resolve, reject) => canvas.toBlob(blob => blob ? resolve(blob) : reject(new Error("image_unavailable")), "image/png"));
if (panel.current?.querySelector("svg[data-invitation-code]") === svg) downloadBlob(png, "piik-invitation.png");
} catch {
if (panel.current?.querySelector("svg[data-invitation-code]") === svg) setSaveFailed(true);
} finally { URL.revokeObjectURL(imageURL); setSaving(false); }
};
const content = open && url && <>
{saveFailed && {t("channel.inviteQrCode.saveFailed")}
}
>;
if (presentation === "inline") return setOpen(event.currentTarget.open)}>
{ if (!url || disabled) event.preventDefault(); }}>{label}
{content}
;
return <>
{
if (event.target === event.currentTarget) setOpen(event.newState === "open");
}}>
{label}
panel.current?.hidePopover()} />
{content}
>;
}