Skip to content

Commit

Permalink
feat: add TokenSearch and RouteSelected events (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov authored Nov 6, 2024
1 parent 28a80af commit 9165dd0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/widget/src/components/Routes/RoutesExpanded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const RoutesExpandedElement = () => {
navigate(navigationRoutes.transactionExecution, {
state: { routeId: route.id },
})
emitter.emit(WidgetEvent.RouteSelected, { route, routes: routes! })
}

const onExit = () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/widget/src/components/TokenList/TokenList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useAccount } from '@lifi/wallet-management'
import { Box } from '@mui/material'
import type { FC } from 'react'
import { type FC, useEffect } from 'react'
import { useChain } from '../../hooks/useChain.js'
import { useDebouncedWatch } from '../../hooks/useDebouncedWatch.js'
import { useTokenBalances } from '../../hooks/useTokenBalances.js'
import { useTokenSearch } from '../../hooks/useTokenSearch.js'
import { useWidgetEvents } from '../../hooks/useWidgetEvents.js'
import { FormKeyHelper } from '../../stores/form/types.js'
import { useFieldValues } from '../../stores/form/useFieldValues.js'
import { WidgetEvent } from '../../types/events.js'
import type { TokenAmount } from '../../types/token.js'
import { TokenNotFound } from './TokenNotFound.js'
import { VirtualizedTokenList } from './VirtualizedTokenList.js'
Expand All @@ -20,6 +22,7 @@ export const TokenList: FC<TokenListProps> = ({
height,
onClick,
}) => {
const emitter = useWidgetEvents()
const [selectedChainId] = useFieldValues(FormKeyHelper.getChainKey(formType))
const [tokenSearchFilter]: string[] = useDebouncedWatch(
320,
Expand Down Expand Up @@ -81,6 +84,16 @@ export const TokenList: FC<TokenListProps> = ({
Boolean(featuredTokens?.length || popularTokens?.length) &&
!tokenSearchFilter

// biome-ignore lint/correctness/useExhaustiveDependencies: Should fire only when search filter changes
useEffect(() => {
if (normalizedSearchFilter) {
emitter.emit(WidgetEvent.TokenSearch, {
value: normalizedSearchFilter,
tokens,
})
}
}, [normalizedSearchFilter, emitter])

return (
<Box ref={parentRef} style={{ height, overflow: 'auto' }}>
{!tokens.length && !isLoading ? (
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './types/events.js'
export type { TokenAmount } from './types/token.js'
export * from './types/widget.js'
export { getPriceImpact } from './utils/getPriceImpact.js'
export { navigationRoutes } from './utils/navigationRoutes.js'
7 changes: 7 additions & 0 deletions packages/widget/src/pages/MainPage/ReviewButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { useNavigate } from 'react-router-dom'
import { BaseTransactionButton } from '../../components/BaseTransactionButton/BaseTransactionButton.js'
import { useRoutes } from '../../hooks/useRoutes.js'
import { useToAddressRequirements } from '../../hooks/useToAddressRequirements.js'
import { useWidgetEvents } from '../../hooks/useWidgetEvents.js'
import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js'
import { useFieldValues } from '../../stores/form/useFieldValues.js'
import { useSplitSubvariantStore } from '../../stores/settings/useSplitSubvariantStore.js'
import { WidgetEvent } from '../../types/events.js'
import { navigationRoutes } from '../../utils/navigationRoutes.js'

export const ReviewButton: React.FC = () => {
const { t } = useTranslation()
const navigate = useNavigate()
const emitter = useWidgetEvents()
const { subvariant, subvariantOptions } = useWidgetConfig()
const splitState = useSplitSubvariantStore((state) => state.state)
const [toAddress] = useFieldValues('toAddress')
Expand All @@ -25,6 +28,10 @@ export const ReviewButton: React.FC = () => {
navigate(navigationRoutes.transactionExecution, {
state: { routeId: currentRoute.id },
})
emitter.emit(WidgetEvent.RouteSelected, {
route: currentRoute,
routes: routes!,
})
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/widget/src/pages/RoutesPage/RoutesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import { useHeader } from '../../hooks/useHeader.js'
import { useNavigateBack } from '../../hooks/useNavigateBack.js'
import { useRoutes } from '../../hooks/useRoutes.js'
import { useToAddressRequirements } from '../../hooks/useToAddressRequirements.js'
import { useWidgetEvents } from '../../hooks/useWidgetEvents.js'
import { useFieldValues } from '../../stores/form/useFieldValues.js'
import { WidgetEvent } from '../../types/events.js'
import { navigationRoutes } from '../../utils/navigationRoutes.js'
import { Stack } from './RoutesPage.style.js'

export const RoutesPage: React.FC<BoxProps> = () => {
const { navigate } = useNavigateBack()
const emitter = useWidgetEvents()
const {
routes,
isLoading,
Expand Down Expand Up @@ -54,6 +57,10 @@ export const RoutesPage: React.FC<BoxProps> = () => {
navigate(navigationRoutes.transactionExecution, {
state: { routeId: route.id },
})
emitter.emit(WidgetEvent.RouteSelected, {
route,
routes: routes!,
})
}

const routeNotFound = !routes?.length && !isLoading && !isFetching
Expand Down
25 changes: 20 additions & 5 deletions packages/widget/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { ChainId, ChainType, Process, Route } from '@lifi/sdk'
import type { DefaultValues } from '../stores/form/types.js'
import type { SettingsProps } from '../stores/settings/types.js'
import type { NavigationRouteType } from '../utils/navigationRoutes.js'
import type { TokenAmount } from './token.js'

export enum WidgetEvent {
RouteExecutionStarted = 'routeExecutionStarted',
RouteExecutionUpdated = 'routeExecutionUpdated',
RouteExecutionCompleted = 'routeExecutionCompleted',
RouteExecutionFailed = 'routeExecutionFailed',
RouteHighValueLoss = 'routeHighValueLoss',
RouteSelected = 'routeSelected',
AvailableRoutes = 'availableRoutes',
ContactSupport = 'contactSupport',
SourceChainTokenSelected = 'sourceChainTokenSelected',
Expand All @@ -26,6 +28,7 @@ export enum WidgetEvent {
PageEntered = 'pageEntered',
FormFieldChanged = 'formFieldChanged',
SettingUpdated = 'settingUpdated',
TokenSearch = 'tokenSearch',
}

export type WidgetEvents = {
Expand All @@ -34,6 +37,7 @@ export type WidgetEvents = {
routeExecutionCompleted: Route
routeExecutionFailed: RouteExecutionUpdate
routeHighValueLoss: RouteHighValueLossUpdate
routeSelected: RouteSelected
availableRoutes: Route[]
contactSupport: ContactSupport
sourceChainTokenSelected: ChainTokenSelected
Expand All @@ -45,31 +49,42 @@ export type WidgetEvents = {
widgetExpanded: boolean
pageEntered: NavigationRouteType
settingUpdated: SettingUpdated
tokenSearch: TokenSearch
}

export interface ContactSupport {
export type ContactSupport = {
supportId?: string
}

export interface RouteHighValueLossUpdate {
export type RouteHighValueLossUpdate = {
fromAmountUSD: number
toAmountUSD: number
gasCostUSD?: number
feeCostUSD?: number
valueLoss: number
}

export interface RouteExecutionUpdate {
export type RouteExecutionUpdate = {
route: Route
process: Process
}

export interface ChainTokenSelected {
export type RouteSelected = {
route: Route
routes: Route[]
}

export type TokenSearch = {
value: string
tokens: TokenAmount[]
}

export type ChainTokenSelected = {
chainId: ChainId
tokenAddress: string
}

export interface WalletConnected {
export type WalletConnected = {
address?: string
chainId?: number
chainType?: ChainType
Expand Down

0 comments on commit 9165dd0

Please sign in to comment.