Skip to content

Commit

Permalink
feat(explorer): server-side currency data
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 20, 2025
1 parent 70e57bf commit 14f0796
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
23 changes: 23 additions & 0 deletions apps/explorer/app/fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { explored } from '../config/explored'
import { unstable_serialize } from 'swr'
import { CurrencyID, exchangeRateRoute } from '@siafoundation/explored-types'
import { exploredApi } from '../config'

// Builds fallback data for the exchange rate. Passing this to the SWR
// config's fallback prop allows the exchange rate hooks with a matching
// key to server-render with an initial exchange rate value.
export async function buildFallbackDataExchangeRate(currency: CurrencyID) {
const rate = await explored.exchangeRate({
params: {
currency,
},
})
return {
// Hooks build with react-core have keys of the form:
// ['method', `${api}${route}${params}${JSON.stringify(args.payload)}`]
[unstable_serialize([
'get',
`${exploredApi}${exchangeRateRoute.replace(':currency', currency)}`,
])]: rate.data,
}
}
24 changes: 22 additions & 2 deletions apps/explorer/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { NextAppSsrAppRouter } from '@siafoundation/design-system'
import { appLink } from '../config'
import Script from 'next/script'
import { rootFontClasses } from '@siafoundation/fonts'
import { cookies } from 'next/headers'
import { CurrencyID } from '@siafoundation/explored-types'
import { buildFallbackDataDefaultCurrencyId } from '@siafoundation/react-core'
import { buildFallbackDataExchangeRate } from './fallback'

export const metadata = {
title: 'Explorer',
Expand All @@ -15,15 +19,31 @@ export const metadata = {
),
}

export default function RootLayout({
function getUserCurrencyPreference() {
const cookieStore = cookies()
const currency = cookieStore.get('currency')?.value as CurrencyID
return currency || 'usd'
}

export default async function RootLayout({
children,
}: {
children: React.ReactNode
}) {
const currency = getUserCurrencyPreference()
return (
<html lang="en" suppressHydrationWarning className={rootFontClasses}>
<body>
<NextAppSsrAppRouter>
<NextAppSsrAppRouter
fallback={{
// Pass the currency cookie value to the app settings provider so that
// it initializes and server-renders with the user's prefered currency.
...buildFallbackDataDefaultCurrencyId(currency),
// Pass the currency's initial exchange rate value to the exchange rate
// hooks so that they initialize and server-render with the value.
...(await buildFallbackDataExchangeRate(currency)),
}}
>
<Layout>{children}</Layout>
</NextAppSsrAppRouter>
</body>
Expand Down

0 comments on commit 14f0796

Please sign in to comment.