Skip to content

Commit

Permalink
Improve UX for forced actions (#447)
Browse files Browse the repository at this point in the history
* Improve UX for forced actions

* add implementation
  • Loading branch information
torztomasz authored Jul 28, 2023
1 parent b770a19 commit 7d860b4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
17 changes: 15 additions & 2 deletions packages/backend/src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ export class UserController {
return { type: 'redirect', url: '/users/recover' }
}

const registeredUser =
await this.userRegistrationEventRepository.findByStarkKey(
context.user.starkKey
)

if (registeredUser) {
return {
type: 'redirect',
url: `/users/${context.user.starkKey.toString()}`,
}
}

const content = renderUserRegisterPage({
context,
exchangeAddress: this.exchangeAddress,
Expand Down Expand Up @@ -286,7 +298,8 @@ export class UserController {
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)
const collateralAsset = this.pageContextService.getCollateralAsset(context)
const [userAssets, userStatistics] = await Promise.all([
const [registeredUser, userAssets, userStatistics] = await Promise.all([
this.userRegistrationEventRepository.findByStarkKey(starkKey),
this.preprocessedAssetHistoryRepository.getCurrentByStarkKeyPaginated(
starkKey,
pagination,
Expand All @@ -301,7 +314,6 @@ export class UserController {
message: `User with starkKey ${starkKey.toString()} not found`,
}
}

const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userAssets: userAssets,
})
Expand All @@ -318,6 +330,7 @@ export class UserController {
const content = renderUserAssetsPage({
context,
starkKey,
ethereumAddress: registeredUser?.ethAddress,
assets,
...pagination,
total: userStatistics.assetCount,
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/preview/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ const routes: Route[] = [
ctx.body = renderUserAssetsPage({
context,
starkKey: StarkKey.fake(),
ethereumAddress: undefined,
assets: repeat(visible, randomUserAssetEntry),
limit,
offset,
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/scripts/keys/starkKeyRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export function initStarkKeyRegistration() {
EthereumAddress(exchangeAddress)
)

window.location.reload()
window.location.href = `/users/${starkKey.toString()}`
})
}
9 changes: 7 additions & 2 deletions packages/frontend/src/view/pages/user/UserAssetsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PageContext } from '@explorer/shared'
import { StarkKey } from '@explorer/types'
import { EthereumAddress, StarkKey } from '@explorer/types'
import React from 'react'

import { ContentWrapper } from '../../components/page/ContentWrapper'
Expand All @@ -13,6 +13,7 @@ import { UserPageTitle } from './components/UserPageTitle'
interface UserAssetsPageProps {
context: PageContext
starkKey: StarkKey
ethereumAddress: EthereumAddress | undefined
assets: UserAssetEntry[]
limit: number
offset: number
Expand All @@ -25,6 +26,7 @@ export function renderUserAssetsPage(props: UserAssetsPageProps) {

function UserAssetsPage(props: UserAssetsPageProps) {
const common = getAssetsTableProps(props.starkKey)
const isMine = props.context.user?.starkKey === props.starkKey
return (
<Page
path={common.path}
Expand All @@ -43,9 +45,12 @@ function UserAssetsPage(props: UserAssetsPageProps) {
total={props.total}
>
<UserAssetsTable
starkKey={props.starkKey}
tradingMode={props.context.tradingMode}
starkKey={props.starkKey}
ethereumAddress={props.ethereumAddress}
assets={props.assets}
isMine={isMine}
isFrozen={props.context.freezeStatus === 'frozen'}
/>
</TableWithPagination>
</ContentWrapper>
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/view/pages/user/UserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function UserPage(props: UserPageProps) {
<UserAssetsTable
tradingMode={props.context.tradingMode}
starkKey={props.starkKey}
ethereumAddress={props.ethereumAddress}
assets={props.assets}
isMine={isMine}
isFrozen={props.context.freezeStatus === 'frozen'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TradingMode } from '@explorer/shared'
import { StarkKey } from '@explorer/types'
import { EthereumAddress, StarkKey } from '@explorer/types'
import React from 'react'

import { Asset, assetToInfo } from '../../../../utils/assets'
Expand All @@ -14,6 +14,7 @@ import { Table } from '../../../components/table/Table'
interface UserAssetsTableProps {
assets: UserAssetEntry[]
starkKey: StarkKey
ethereumAddress: EthereumAddress | undefined
tradingMode: TradingMode
isMine?: boolean
isFrozen?: boolean
Expand All @@ -28,16 +29,13 @@ export interface UserAssetEntry {
}

export function UserAssetsTable(props: UserAssetsTableProps) {
const forcedActionLink = (entry: UserAssetEntry) =>
props.tradingMode === 'perpetual'
? `/forced/new/${
entry.vaultOrPositionId
}/${entry.asset.hashOrId.toString()}`
: `/forced/new/${entry.vaultOrPositionId}`
const isUserRegistered = !!props.ethereumAddress

const escapeHatchElem = (entry: UserAssetEntry) =>
entry.action === 'WITHDRAW' ? (
<LinkButton href={`/escape/${entry.vaultOrPositionId}`}>
<LinkButton
href={getEscapeHatchLink(entry.vaultOrPositionId, isUserRegistered)}
>
ESCAPE
</LinkButton>
) : (
Expand Down Expand Up @@ -79,7 +77,11 @@ export function UserAssetsTable(props: UserAssetsTableProps) {
(!props.isFrozen ? (
<LinkButton
className="w-32"
href={forcedActionLink(entry)}
href={getForcedActionLink(
props.tradingMode,
entry,
isUserRegistered
)}
disabled={isDisabled}
>
{entry.action}
Expand All @@ -93,3 +95,29 @@ export function UserAssetsTable(props: UserAssetsTableProps) {
/>
)
}

function getEscapeHatchLink(
vaultOrPositionId: string,
isUserRegistered: boolean
) {
if (!isUserRegistered) {
return '/users/register'
}
return `/escape/${vaultOrPositionId}`
}

function getForcedActionLink(
tradingMode: TradingMode,
entry: UserAssetEntry,
isUserRegistered: boolean
) {
if (!isUserRegistered) {
return '/users/register'
}

return tradingMode === 'perpetual'
? `/forced/new/${
entry.vaultOrPositionId
}/${entry.asset.hashOrId.toString()}`
: `/forced/new/${entry.vaultOrPositionId}`
}

0 comments on commit 7d860b4

Please sign in to comment.