mirror of
https://github.com/vector-im/element-call.git
synced 2024-11-21 00:28:08 +08:00
49 lines
1016 B
TypeScript
49 lines
1016 B
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
import { FC, useEffect, useState } from "react";
|
|
import { toDataURL } from "qrcode";
|
|
import classNames from "classnames";
|
|
import { t } from "i18next";
|
|
|
|
import styles from "./QrCode.module.css";
|
|
|
|
interface Props {
|
|
data: string;
|
|
className?: string;
|
|
}
|
|
|
|
export const QrCode: FC<Props> = ({ data, className }) => {
|
|
const [url, setUrl] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let isCancelled = false;
|
|
|
|
toDataURL(data, { errorCorrectionLevel: "L" })
|
|
.then((url) => {
|
|
if (!isCancelled) {
|
|
setUrl(url);
|
|
}
|
|
})
|
|
.catch((reason) => {
|
|
if (!isCancelled) {
|
|
setUrl(null);
|
|
}
|
|
});
|
|
|
|
return (): void => {
|
|
isCancelled = true;
|
|
};
|
|
}, [data]);
|
|
|
|
return (
|
|
<div className={classNames(styles.qrCode, className)}>
|
|
{url && <img src={url} alt={t("qr_code")} />}
|
|
</div>
|
|
);
|
|
};
|