From 9170833d0f91db4cfb0e1456dbcf980ec2c8a885 Mon Sep 17 00:00:00 2001 From: tomiir Date: Tue, 2 Jan 2024 09:20:13 -0600 Subject: [PATCH] chore: use W3mFrameHelpers to get localstorage stored las login attempt time --- .../src/views/w3m-email-verify-otp-view/index.ts | 6 +++--- packages/wallet/src/W3mFrameHelpers.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/scaffold/src/views/w3m-email-verify-otp-view/index.ts b/packages/scaffold/src/views/w3m-email-verify-otp-view/index.ts index 3bc3974af1..84ec5641fb 100644 --- a/packages/scaffold/src/views/w3m-email-verify-otp-view/index.ts +++ b/packages/scaffold/src/views/w3m-email-verify-otp-view/index.ts @@ -10,6 +10,7 @@ import { } from '@web3modal/core' import { state } from 'lit/decorators.js' import styles from './styles.js' +import { W3mFrameHelpers } from '@web3modal/wallet' // -- Helpers ------------------------------------------- // const OTP_LENGTH = 6 @@ -26,7 +27,7 @@ export class W3mEmailVerifyOtpView extends LitElement { // -- State & Properties -------------------------------- // @state() private loading = false - @state() private timeoutTimeLeft = 30 + @state() private timeoutTimeLeft = W3mFrameHelpers.getTimeToNextEmailLogin() private OTPTimeout: NodeJS.Timeout | undefined @@ -80,10 +81,9 @@ export class W3mEmailVerifyOtpView extends LitElement { // -- Private ------------------------------------------- // private startOTPTimeout() { - this.timeoutTimeLeft = 30 this.OTPTimeout = setInterval(() => { if (this.timeoutTimeLeft > 0) { - this.timeoutTimeLeft -= 1 + this.timeoutTimeLeft = W3mFrameHelpers.getTimeToNextEmailLogin() } else { clearInterval(this.OTPTimeout) } diff --git a/packages/wallet/src/W3mFrameHelpers.ts b/packages/wallet/src/W3mFrameHelpers.ts index dfcaec8b14..47100a182d 100644 --- a/packages/wallet/src/W3mFrameHelpers.ts +++ b/packages/wallet/src/W3mFrameHelpers.ts @@ -37,5 +37,17 @@ export const W3mFrameHelpers = { throw new Error(`Please try again after ${cooldownSec} seconds`) } } + }, + + getTimeToNextEmailLogin() { + const lastEmailLoginTime = W3mFrameStorage.get(W3mFrameConstants.LAST_EMAIL_LOGIN_TIME) + if (lastEmailLoginTime) { + const difference = Date.now() - Number(lastEmailLoginTime) + if (difference < 30_000) { + return Math.ceil((30_000 - difference) / 1000) + } + } + + return 0 } }