Reuse QRCode for VerificationQRCode and specify widths

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2020-05-14 11:20:27 +01:00
parent d1c6f3099c
commit 4c7d703275
3 changed files with 13 additions and 34 deletions

View file

@ -221,7 +221,7 @@ export default class ShareDialog extends React.PureComponent<IProps, IState> {
<div className="mx_ShareDialog_split"> <div className="mx_ShareDialog_split">
<div className="mx_ShareDialog_qrcode_container"> <div className="mx_ShareDialog_qrcode_container">
<QRCode data={matrixToUrl} /> <QRCode data={matrixToUrl} width={256} />
</div> </div>
<div className="mx_ShareDialog_social_container"> <div className="mx_ShareDialog_social_container">
{ socials.map((social) => ( { socials.map((social) => (

View file

@ -16,19 +16,21 @@ limitations under the License.
import * as React from "react"; import * as React from "react";
import {toDataURL, QRCodeSegment, QRCodeToDataURLOptions} from "qrcode"; import {toDataURL, QRCodeSegment, QRCodeToDataURLOptions} from "qrcode";
import classNames from "classnames";
import {_t} from "../../../languageHandler"; import {_t} from "../../../languageHandler";
import Spinner from "./Spinner"; import Spinner from "./Spinner";
interface IProps extends QRCodeToDataURLOptions { interface IProps extends QRCodeToDataURLOptions {
data: string | QRCodeSegment[]; data: string | QRCodeSegment[];
className?: string;
} }
const defaultOptions: QRCodeToDataURLOptions = { const defaultOptions: QRCodeToDataURLOptions = {
errorCorrectionLevel: 'L', // we want it as trivial-looking as possible errorCorrectionLevel: 'L', // we want it as trivial-looking as possible
}; };
const QRCode: React.FC<IProps> = ({data, ...options}) => { const QRCode: React.FC<IProps> = ({data, className, ...options}) => {
const [dataUri, setUri] = React.useState<string>(null); const [dataUri, setUri] = React.useState<string>(null);
React.useEffect(() => { React.useEffect(() => {
let cancelled = false; let cancelled = false;
@ -39,10 +41,9 @@ const QRCode: React.FC<IProps> = ({data, ...options}) => {
return () => { return () => {
cancelled = true; cancelled = true;
}; };
}, [data, options]); }, [JSON.stringify(data), options]);
return <div className={classNames("mx_QRCode", className)}>
return <div className="mx_QRCode">
{ dataUri ? <img src={dataUri} className="mx_VerificationQRCode" alt={_t("QR Code")} /> : <Spinner /> } { dataUri ? <img src={dataUri} className="mx_VerificationQRCode" alt={_t("QR Code")} /> : <Spinner /> }
</div>; </div>;
}; };

View file

@ -17,8 +17,7 @@ limitations under the License.
import React from "react"; import React from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import {replaceableComponent} from "../../../../utils/replaceableComponent"; import {replaceableComponent} from "../../../../utils/replaceableComponent";
import Spinner from "../Spinner"; import QRCode from "../QRCode";
import * as QRCode from "qrcode";
@replaceableComponent("views.elements.crypto.VerificationQRCode") @replaceableComponent("views.elements.crypto.VerificationQRCode")
export default class VerificationQRCode extends React.PureComponent { export default class VerificationQRCode extends React.PureComponent {
@ -26,33 +25,12 @@ export default class VerificationQRCode extends React.PureComponent {
qrCodeData: PropTypes.object.isRequired, qrCodeData: PropTypes.object.isRequired,
}; };
constructor(props) {
super(props);
this.state = {
dataUri: null,
};
this.generateQrCode();
}
componentDidUpdate(prevProps): void {
if (JSON.stringify(this.props) === JSON.stringify(prevProps)) return; // No prop change
this.generateQRCode();
}
async generateQrCode() {
// Now actually assemble the QR code's data URI
const uri = await QRCode.toDataURL([{data: this.props.qrCodeData.buffer, mode: 'byte'}], {
errorCorrectionLevel: 'L', // we want it as trivial-looking as possible
});
this.setState({dataUri: uri});
}
render() { render() {
if (!this.state.dataUri) { return (
return <div className='mx_VerificationQRCode'><Spinner /></div>; <QRCode
} data={[{data: this.props.qrCodeData.buffer, mode: 'byte'}]}
className="mx_VerificationQRCode"
return <img src={this.state.dataUri} className='mx_VerificationQRCode' />; width={196} />
);
} }
} }