Skip to content

Commit

Permalink
Merge pull request #846 from TokenScript/feature/timers-poc
Browse files Browse the repository at this point in the history
added timer feature to slow page navigation
  • Loading branch information
nicktaras authored Sep 27, 2023
2 parents 80a54a2 + f19f3e4 commit ca30f22
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
openingAction: "Let's Go!",
theme: theme,
position: "top-right",
userCancelIssuerAutoRedirectTimer: 2000,
},
unSupportedUserAgent: {
authentication: {
Expand Down
33 changes: 28 additions & 5 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
removeUrlSearchParams,
createIssuerHashMap,
createIssuerHashArray,
sleep,
} from '../utils'
import { getNftCollection, getNftTokens } from '../utils/token/nftProvider'
import { TokenStore } from './tokenStore'
Expand Down Expand Up @@ -112,6 +113,7 @@ export class Client {
[UIUpdateEventType.ISSUERS_LOADED]: undefined,
[UIUpdateEventType.WALLET_DISCONNECTED]: undefined,
}
private userCancelTokenAutoload: boolean

private urlParams: URLSearchParams

Expand Down Expand Up @@ -745,10 +747,17 @@ export class Client {
}

private async loadRemoteOutletTokens(issuer: OffChainTokenConfig): Promise<OutletTokenResult | void> {
const redirectRequired = shouldUseRedirectMode(this.config.offChainRedirectMode)

if (redirectRequired) this.tokenStore.setTokens(issuer.collectionID, [])

this.tokenStore.setTokens(issuer.collectionID, [])
this.ui.showLoader(
'<h4>Connecting to issuers...</h4>',
'<small>Your browser will re-direct shortly</small>',
"<button class='cancel-autoload-btn btn-tn' aria-label='Cancel authentication'>Cancel</button>",
)
this.enableTokenAutoLoadCancel()
if (this.config.uiOptions?.userCancelIssuerAutoRedirectTimer) await sleep(this.config.uiOptions.userCancelIssuerAutoRedirectTimer)
if (this.userCancelTokenAutoload) {
return {}
}
const res = await this.messaging.sendMessage(
{
action: OutletAction.GET_ISSUER_TOKENS,
Expand All @@ -759,7 +768,7 @@ export class Client {
},
this.config.messagingForceTab,
this.config.type === 'active' ? this.ui : null,
redirectRequired ? window.location.href : false,
window.location.href,
)

if (!res) return // Site is redirecting
Expand Down Expand Up @@ -848,6 +857,7 @@ export class Client {
600,
true,
)
this.enableAuthCancel(authRequests)
}

const authRequestBatch = await this.getMultiRequestBatch(authRequests)
Expand Down Expand Up @@ -984,6 +994,19 @@ export class Client {
})
}

public enableTokenAutoLoadCancel(): void {
waitForElementToExist('.cancel-autoload-btn')
.then((cancelAuthButton: HTMLElement) => {
cancelAuthButton.onclick = () => {
this.userCancelTokenAutoload = true
this.ui.dismissLoader()
}
})
.catch((err) => {
logger(2, err)
})
}

private async handleWalletRequired(authRequest) {
if (this.ui) {
this.ui.dismissLoader()
Expand Down
3 changes: 1 addition & 2 deletions src/client/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface UIOptionsInterface {
position?: PopupPosition
autoPopup?: boolean
alwaysShowStartScreen?: boolean
userCancelIssuerAutoRedirectTimer?: number
viewOverrides?: {
[type: string]: {
component?: ViewComponent
Expand Down Expand Up @@ -393,9 +394,7 @@ export class Ui implements UiInterface {
loader.style.display = 'block'
const dismissBtn = this.loadContainer.querySelector('.dismiss-error-tn') as HTMLDivElement
dismissBtn.style.display = 'none'

this.loadContainer.querySelector('.loader-msg-tn').innerHTML = message.join('\n')

this.loadContainer.style.display = 'flex'
}

Expand Down
3 changes: 2 additions & 1 deletion src/theme/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
}
}

.overlay-tn .cancel-auth-btn {
.overlay-tn .cancel-auth-btn,
.overlay-tn .cancel-autoload-btn {
display: block;
margin: 10px auto;
cursor: pointer;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,7 @@ export const getCookieByName = (cookieName: string) => {
export const deleteCookieByName = (cookieName) => {
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
}

export const sleep = async (ms: number) => {
await new Promise((resolve) => setTimeout(resolve, ms))
}

0 comments on commit ca30f22

Please sign in to comment.