diff --git a/lemmy-translations b/lemmy-translations index abd40d47..6fbc8693 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit abd40d4737fa732321fd7b62e42bbfcd51081cb6 +Subproject commit 6fbc86932a03c4d40829ee4a3395259b2a7660e5 diff --git a/src/shared/components/common/totp-modal.tsx b/src/shared/components/common/totp-modal.tsx index aa4c7fe4..2ea968ef 100644 --- a/src/shared/components/common/totp-modal.tsx +++ b/src/shared/components/common/totp-modal.tsx @@ -25,64 +25,42 @@ interface TotpModalState { const TOTP_LENGTH = 6; -async function handleSubmit(modal: TotpModal, totp: string) { - const successful = await modal.props.onSubmit(totp); +async function handleSubmit(i: TotpModal, totp: string) { + const successful = await i.props.onSubmit(totp); if (!successful) { - modal.setState({ totp: "" }); - for (const inputRef of modal.inputRefs) { - inputRef!.value = ""; - } - modal.inputRefs[0]?.focus(); + i.setState({ totp: "" }); + i.inputRef.current?.focus(); } } -function handleInput( - { modal, i }: { modal: TotpModal; i: number }, - event: any, -) { +function handleInput(i: TotpModal, event: any) { if (isNaN(event.target.value)) { - modal.inputRefs[i]!.value = ""; return; } - modal.setState(prev => ({ ...prev, totp: prev.totp + event.target.value })); - modal.inputRefs[i + 1]?.focus(); + i.setState({ + totp: event.target.value, + }); - const { totp } = modal.state; + const { totp } = i.state; if (totp.length >= TOTP_LENGTH) { - handleSubmit(modal, totp); + handleSubmit(i, totp); } } -function handleKeyUp( - { modal, i }: { modal: TotpModal; i: number }, - event: any, -) { - if (event.key === "Backspace" && i > 0) { - modal.setState(prev => ({ - ...prev, - totp: prev.totp.slice(0, prev.totp.length - 1), - })); - modal.inputRefs[i - 1]?.focus(); - } -} - -function handlePaste(modal: TotpModal, event: any) { +function handlePaste(i: TotpModal, event: any) { event.preventDefault(); const text: string = event.clipboardData.getData("text")?.trim(); if (text.length > TOTP_LENGTH || isNaN(Number(text))) { toast(I18NextService.i18n.t("invalid_totp_code"), "danger"); - modal.clearTotp(); + i.clearTotp(); } else { - [...text].forEach((num, i) => { - modal.inputRefs[i]!.value = num; - }); - modal.setState({ totp: text }); + i.setState({ totp: text }); if (text.length === TOTP_LENGTH) { - handleSubmit(modal, text); + handleSubmit(i, text); } } } @@ -91,8 +69,8 @@ export default class TotpModal extends Component< TotpModalProps, TotpModalState > { - private readonly modalDivRef: RefObject; - inputRefs: (HTMLInputElement | null)[] = []; + readonly modalDivRef: RefObject; + readonly inputRef: RefObject; modal: Modal; state: TotpModalState = { totp: "", @@ -102,6 +80,7 @@ export default class TotpModal extends Component< super(props, context); this.modalDivRef = createRef(); + this.inputRef = createRef(); this.clearTotp = this.clearTotp.bind(this); this.handleShow = this.handleShow.bind(this); @@ -207,34 +186,24 @@ export default class TotpModal extends Component<
- {Array.from(Array(TOTP_LENGTH).keys()).map(i => ( - { - this.inputRefs[i] = element; - }} - enterKeyHint="done" - /> - ))} +
@@ -255,13 +224,10 @@ export default class TotpModal extends Component< clearTotp() { this.setState({ totp: "" }); - for (const inputRef of this.inputRefs) { - inputRef!.value = ""; - } } async handleShow() { - this.inputRefs[0]?.focus(); + this.inputRef.current?.focus(); if (this.props.type === "generate") { const { getSVG } = await import("@shortcm/qr-image/lib/svg");