Skip to content

Commit

Permalink
remove direct need for active execution route and add failing spec
Browse files Browse the repository at this point in the history
  • Loading branch information
frontendphil committed Dec 18, 2024
1 parent d622315 commit d1d2fba
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
14 changes: 14 additions & 0 deletions extension/src/panel/pages/routes/list/ListRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ describe('List routes', () => {

await expectRouteToBe('/secondRoute')
})

it('works without an active route', async () => {
await mockRoute({ id: 'test-route' })

await render(
'/routes/list',
[{ path: '/routes/list', Component: ListRoutes, loader, action }],
{ inspectRoutes: ['/:activeRouteId'], initialSelectedRoute: null },
)

await userEvent.click(screen.getByRole('button', { name: 'Launch' }))

await expectRouteToBe('/test-route')
})
})

describe('Clearing transactions', () => {
Expand Down
30 changes: 26 additions & 4 deletions extension/src/panel/pages/routes/list/ListRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { Breadcrumbs, InlineForm, Page, PrimaryButton } from '@/components'
import { createRoute, getRoutes } from '@/execution-routes'
import {
createRoute,
getLastUsedRouteId,
getRoute,
getRoutes,
} from '@/execution-routes'
import { getString } from '@/utils'
import { Plus } from 'lucide-react'
import { redirect, useLoaderData, type ActionFunctionArgs } from 'react-router'
import { Route } from './Route'
import { Intent } from './intents'

export const loader = async () => {
const routes = await getRoutes()
const activeRouteId = await getLastUsedRouteId()

if (activeRouteId != null) {
const { avatar } = await getRoute(activeRouteId)

return {
routes,
currentlyActiveAvatar: avatar,
}
}

return {
routes: await getRoutes(),
routes,
currentlyActiveAvatar: null,
}
}

Expand All @@ -31,7 +49,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
}

export const ListRoutes = () => {
const { routes } = useLoaderData<typeof loader>()
const { routes, currentlyActiveAvatar } = useLoaderData<typeof loader>()

return (
<Page>
Expand All @@ -45,7 +63,11 @@ export const ListRoutes = () => {

<Page.Content>
{routes.map((route) => (
<Route key={route.id} route={route} />
<Route
key={route.id}
route={route}
currentlyActiveAvatar={currentlyActiveAvatar}
/>
))}
</Page.Content>

Expand Down
10 changes: 5 additions & 5 deletions extension/src/panel/pages/routes/list/Route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import {
SecondaryLinkButton,
Tag,
} from '@/components'
import { useExecutionRoute, useRouteConnect } from '@/execution-routes'
import { useRouteConnect } from '@/execution-routes'
import { useTransactions } from '@/state'
import type { ExecutionRoute } from '@/types'
import { formatDistanceToNow } from 'date-fns'
import { Cable, PlugZap, Unplug } from 'lucide-react'
import { useRef, useState } from 'react'
import { useSubmit } from 'react-router'
import type { PrefixedAddress } from 'ser-kit'
import { ConnectionStack } from '../../ConnectionStack'
import { asLegacyConnection } from '../../legacyConnectionMigrations'
import { ClearTransactionsModal } from '../../useConfirmClearTransaction'
import { Intent } from './intents'

interface RouteProps {
currentlyActiveAvatar: PrefixedAddress | null
route: ExecutionRoute
}

export const Route = ({ route }: RouteProps) => {
export const Route = ({ route, currentlyActiveAvatar }: RouteProps) => {
const [connected, connect] = useRouteConnect(route)
const currentlySelectedRoute = useExecutionRoute()
const [confirmClearTransactions, setConfirmClearTransactions] =
useState(false)
const transactions = useTransactions()
Expand Down Expand Up @@ -82,8 +83,7 @@ export const Route = ({ route }: RouteProps) => {
}
// we continue working with the same avatar, so don't have to clear the recorded transaction
const keepTransactionBundle =
currentlySelectedRoute &&
currentlySelectedRoute.avatar === route.avatar
currentlyActiveAvatar === route.avatar

if (keepTransactionBundle) {
return
Expand Down
14 changes: 9 additions & 5 deletions extension/test-utils/RenderWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { ProvideConnectProvider, ProvideInjectedWallet } from '@/providers'
import { ProvideProvider } from '@/providers-ui'
import { ProvideState, type TransactionState } from '@/state'
import type { Eip1193Provider, ExecutionRoute } from '@/types'
import type { PropsWithChildren } from 'react'
import { type PropsWithChildren } from 'react'
import { createMockRoute } from './creators'

type RenderWraperProps = PropsWithChildren<{
initialState?: TransactionState[]
initialSelectedRoute?: ExecutionRoute
initialSelectedRoute?: ExecutionRoute | null
initialProvider?: Eip1193Provider
}>

Expand All @@ -23,9 +23,13 @@ export const RenderWrapper = ({
<ProvideState initialState={initialState}>
<ProvideConnectProvider initialProvider={initialProvider}>
<ProvideInjectedWallet>
<ProvideExecutionRoute route={initialSelectedRoute}>
<ProvideProvider>{children}</ProvideProvider>
</ProvideExecutionRoute>
{initialSelectedRoute == null ? (
children
) : (
<ProvideExecutionRoute route={initialSelectedRoute}>
<ProvideProvider>{children}</ProvideProvider>
</ProvideExecutionRoute>
)}
</ProvideInjectedWallet>
</ProvideConnectProvider>
</ProvideState>
Expand Down
2 changes: 1 addition & 1 deletion extension/test-utils/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Options = Parameters<typeof baseRender>[1] & {
/**
* Pass a route id here to define the currently launched route
*/
initialSelectedRoute?: ExecutionRoute
initialSelectedRoute?: ExecutionRoute | null
/**
* Pass a custom provider instance to be used as the connect provider
*/
Expand Down

0 comments on commit d1d2fba

Please sign in to comment.