Skip to content

Commit

Permalink
remove some code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
frontendphil committed Jan 16, 2025
1 parent a93b6dc commit 6c00449
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 39 deletions.
47 changes: 13 additions & 34 deletions deployables/app/app/routes/edit-route.$data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ export const action = async ({ request, params }: Route.ActionArgs) => {
multisend: await getMultisend(route, module),
})

const url = new URL(request.url)

return Response.redirect(
new URL(`/edit-route/${btoa(JSON.stringify(updatedRoute))}`, url.origin),
)
return saveRouteState(request.url, updatedRoute)
}

export const clientAction = async ({
Expand All @@ -97,49 +93,24 @@ export const clientAction = async ({

chrome.runtime.sendMessage('', route)

const url = new URL(request.url)

return Response.redirect(
new URL(`/edit-route/${btoa(JSON.stringify(route))}`, url.origin),
)
return saveRouteState(request.url, route)
}
case Intent.UpdateChain: {
const route = parseRouteData(params.data)
const chainId = verifyChainId(getInt(data, 'chainId'))

const url = new URL(request.url)

return Response.redirect(
new URL(
`/edit-route/${btoa(JSON.stringify(updateChainId(route, chainId)))}`,
url.origin,
),
)
return saveRouteState(request.url, updateChainId(route, chainId))
}
case Intent.UpdateAvatar: {
const route = parseRouteData(params.data)
const avatar = getHexString(data, 'avatar')

const url = new URL(request.url)

return Response.redirect(
new URL(
`/edit-route/${btoa(JSON.stringify(updateAvatar(route, { safe: avatar })))}`,
url.origin,
),
)
return saveRouteState(request.url, updateAvatar(route, { safe: avatar }))
}
case Intent.RemoveAvatar: {
const route = parseRouteData(params.data)

const url = new URL(request.url)

return Response.redirect(
new URL(
`/edit-route/${btoa(JSON.stringify(removeAvatar(route)))}`,
url.origin,
),
)
return saveRouteState(request.url, removeAvatar(route))
}
default:
return serverAction()
Expand Down Expand Up @@ -281,3 +252,11 @@ const updateChainId = (
avatar: formatPrefixedAddress(chainId, address),
}
}

const saveRouteState = (currentUrl: string, route: ExecutionRoute) => {
const url = new URL(currentUrl)

return Response.redirect(
new URL(`/edit-route/${btoa(JSON.stringify(route))}`, url.origin),
)
}
10 changes: 5 additions & 5 deletions packages/modules/src/updateRolesWaypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type RoleUpdatePayload = {
export const updateRolesWaypoint = (
route: ExecutionRoute,
{ moduleAddress, multisend, version }: RoleUpdatePayload,
) => {
): ExecutionRoute => {
const [chainId] = splitPrefixedAddress(route.avatar)

invariant(
Expand All @@ -30,10 +30,10 @@ export const updateRolesWaypoint = (
'The route needs at least a start and an end waypoint to define a mod in between.',
)

const [startingPoint] = route.waypoints
const [startingPoint, ...waypoints] = route.waypoints

if (hasRolesWaypoint(route.waypoints)) {
const newWaypoints = route.waypoints.map((waypoint) => {
const newWaypoints = waypoints.map((waypoint) => {
if (waypoint.account.type !== AccountType.ROLES) {
return waypoint
}
Expand All @@ -49,7 +49,7 @@ export const updateRolesWaypoint = (

return {
...route,
waypoints: newWaypoints,
waypoints: [startingPoint, ...newWaypoints],
}
}

Expand All @@ -64,7 +64,7 @@ export const updateRolesWaypoint = (
version,
from: startingPoint.account.prefixedAddress,
}),
...route.waypoints.slice(1),
...waypoints,
],
}
}
Expand Down

0 comments on commit 6c00449

Please sign in to comment.