stacker.news/pages/wallets/[...slug].js
ekzyis 21a9696ea0
Use SSR for wallets (#2397)
* Use SSR for wallet forms

* Fix back/forward navigation with useData hook

* Fix protocol fallback not working with shallow routing

* Fix wallet refetch

* Replace useEffect for default selection with smart link

* Remove unused useWalletQuery

* Move server2client wallet transform into single function

* Add comment about graphql-tag fragment warning

* Check if wallet not found

* Handle wallet is sometimes null on back or forward navigation
2025-08-10 12:15:40 -05:00

34 lines
1.4 KiB
JavaScript

import { getGetServerSideProps } from '@/api/ssrApollo'
import { useData } from '@/components/use-data'
import { WalletForms as WalletFormsComponent } from '@/wallets/client/components'
import { WALLET } from '@/wallets/client/fragments'
import { useDecryptedWallet } from '@/wallets/client/hooks'
import { unurlify } from '@/wallets/lib/util'
import { useQuery } from '@apollo/client'
import { useRouter } from 'next/router'
const variablesFunc = params => {
const id = Number(params.slug[0])
return !Number.isNaN(id) ? { id } : { name: unurlify(params.slug[0]) }
}
export const getServerSideProps = getGetServerSideProps({ query: WALLET, variables: variablesFunc, authRequired: true })
export default function WalletForms ({ ssrData }) {
const router = useRouter()
const variables = variablesFunc(router.query)
// this will print the following warning in the console:
// Warning: fragment with name WalletTemplateFields already exists.
// graphql-tag enforces all fragment names across your application to be unique
// this is not a problem because the warning is only meant to avoid overwriting fragments but we're reusing it
const { data, refetch } = useQuery(WALLET, { variables })
const dat = useData(data, ssrData)
const decryptedWallet = useDecryptedWallet(dat?.wallet)
const wallet = decryptedWallet ?? ssrData?.wallet
if (!wallet) {
return null
}
return <WalletFormsComponent wallet={wallet} refetch={refetch} />
}