Skip to content

Commit

Permalink
chore: load available safes through ser-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
frontendphil committed Feb 10, 2025
1 parent 9ec63ba commit 004b882
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions deployables/app/app/routes/$address.$chainId/available-safes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,49 @@ import { validateAddress } from '@/utils'
import { invariantResponse } from '@epic-web/invariant'
import { verifyChainId } from '@zodiac/chains'
import { getOptionalString } from '@zodiac/form-data'
import { initSafeApiKit } from '@zodiac/safe'
import type { ShouldRevalidateFunctionArgs } from 'react-router'
import { queryAvatars, splitPrefixedAddress, unprefixAddress } from 'ser-kit'
import { Intent } from '../edit/intents'
import type { Route } from './+types/available-safes'

export const loader = async ({ params }: Route.LoaderArgs) => {
const { chainId, address } = params
const { address, chainId } = params

const safeService = initSafeApiKit(verifyChainId(parseInt(chainId)))
const verifiedChainId = verifyChainId(parseInt(chainId))
const validatedAddress = validateAddress(address)

invariantResponse(validatedAddress != null, `Invalid address: ${address}`)

try {
const { safes } = await safeService.getSafesByOwner(validatedAddress)
const routes = await queryAvatars(validatedAddress)
console.log({ routes })

return Response.json(safes)
const possibleRoutes = routes.filter((route) => {
const [chainId] = splitPrefixedAddress(route.avatar)

return chainId === verifiedChainId
})

return Response.json(
Array.from(
new Set(possibleRoutes.map((route) => unprefixAddress(route.avatar))),
),
)
} catch {
return Response.json([])
}
}

export const shouldRevalidate = ({
formData,
currentParams,
nextParams,
}: ShouldRevalidateFunctionArgs) => {
console.log({ currentParams, nextParams })
if (currentParams.chainId !== nextParams.chainId) {
return true
}

if (formData == null) {
return false
}
Expand Down

0 comments on commit 004b882

Please sign in to comment.