link-stack/packages/bridge-ui/components/QRCode.tsx

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import { FC, useEffect, useState } from "react";
2025-08-20 12:30:47 +02:00
// @ts-ignore - react-qr-code doesn't have React 19 compatible types yet
import QRCodeInternal from "react-qr-code";
import { Box } from "@mui/material";
import { colors } from "../styles/theme";
type QRCodeProps = {
name: string;
label: string;
2024-05-16 18:22:10 +02:00
token: string;
verified: boolean;
helperText?: string;
2024-06-05 15:12:48 +02:00
getValue?: (id: string) => Promise<Record<string, string>>;
refreshInterval?: number;
};
export const QRCode: FC<QRCodeProps> = ({
name,
label,
2024-05-16 18:22:10 +02:00
token,
verified,
helperText,
getValue,
refreshInterval,
}) => {
const [value, setValue] = useState("");
2024-06-05 15:12:48 +02:00
const [kind, setKind] = useState("data");
const { white } = colors;
useEffect(() => {
2024-05-16 18:22:10 +02:00
if (!verified && getValue && refreshInterval) {
const interval = setInterval(async () => {
2024-06-05 15:12:48 +02:00
const { qr, kind } = await getValue(token);
setValue(qr);
setKind(kind);
2024-05-16 18:22:10 +02:00
}, refreshInterval * 1000);
return () => clearInterval(interval);
}
}, [getValue, refreshInterval]);
2024-05-16 18:22:10 +02:00
return !verified ? (
<Box sx={{ backgroundColor: white, m: 2 }}>
2024-06-05 15:12:48 +02:00
{kind === "data" ? (
<QRCodeInternal value={value} />
) : (
<img src={value} alt={name} />
)}
<Box>{helperText}</Box>
</Box>
2024-05-16 18:22:10 +02:00
) : null;
};