Files
ThreadNet-Web/src/components/views/elements/QRCode.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.6 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
2025-03-19 10:39:52 +00:00
import React from "react";
2025-02-05 13:25:06 +00:00
import { toDataURL, type QRCodeSegment, type QRCodeToDataURLOptions, type QRCodeRenderersOptions } from "qrcode";
import classNames from "classnames";
2021-06-29 13:11:58 +01:00
import { _t } from "../../../languageHandler";
import Spinner from "./Spinner";
2022-08-24 10:28:59 +01:00
interface IProps extends QRCodeRenderersOptions {
/** The data for the QR code. If `null`, a spinner is shown. */
data: null | string | QRCodeSegment[];
className?: string;
}
const defaultOptions: QRCodeToDataURLOptions = {
errorCorrectionLevel: "L", // we want it as trivial-looking as possible
};
2021-06-29 13:11:58 +01:00
const QRCode: React.FC<IProps> = ({ data, className, ...options }) => {
const [dataUri, setUri] = React.useState<string | null>(null);
React.useEffect(() => {
if (data === null) {
setUri(null);
return;
}
let cancelled = false;
2021-06-29 13:11:58 +01:00
toDataURL(data, { ...defaultOptions, ...options }).then((uri) => {
if (cancelled) return;
setUri(uri);
});
return () => {
cancelled = true;
};
}, [JSON.stringify(data), options]); // eslint-disable-line react-hooks/exhaustive-deps
return (
<div className={classNames("mx_QRCode", className)}>
{dataUri ? <img src={dataUri} className="mx_VerificationQRCode" alt={_t("common|qr_code")} /> : <Spinner />}
</div>
);
};
export default QRCode;