11import { JotaiStore } from '@/features/common/data/types'
22import { atom , useAtomValue , useStore } from 'jotai'
3- import { BlockResult , Round } from './types'
4- import { blocksAtom , syncedRoundAtom } from '.'
53import { indexer } from '@/features/common/data'
64import { TransactionResult } from '@algorandfoundation/algokit-utils/types/indexer'
75import { atomEffect } from 'jotai-effect'
86import { fetchTransactionsAtomBuilder , fetchTransactionsModelAtomBuilder , transactionsAtom } from '@/features/transactions/data'
9- import { asBlockDetails } from '../mappers'
7+ import { asBlock } from '../mappers'
108import { useMemo } from 'react'
119import { loadable } from 'jotai/utils'
10+ import { blocksAtom , syncedRoundAtom } from './core'
11+ import { BlockResult , Round } from './types'
1212
1313const nextRoundAvailableAtomBuilder = ( store : JotaiStore , round : Round ) => {
1414 // This atom conditionally subscribes to updates on the syncedRoundAtom
@@ -19,7 +19,7 @@ const nextRoundAvailableAtomBuilder = (store: JotaiStore, round: Round) => {
1919 } )
2020}
2121
22- const fetchBlockAtomBuilder = ( round : Round ) => {
22+ const fetchBlockResultAtomBuilder = ( round : Round ) => {
2323 return atom ( async ( _get ) => {
2424 return await indexer
2525 . lookupBlock ( round )
@@ -37,22 +37,25 @@ const fetchBlockAtomBuilder = (round: Round) => {
3737 } )
3838}
3939
40- const getBlockDetailsAtomBuilder = ( store : JotaiStore , round : Round ) => {
41- const fetchBlockAtom = fetchBlockAtomBuilder ( round )
40+ const getBlockAtomBuilder = ( store : JotaiStore , round : Round ) => {
41+ const fetchBlockResultAtom = fetchBlockResultAtomBuilder ( round )
4242
4343 const syncEffect = atomEffect ( ( get , set ) => {
4444 ; ( async ( ) => {
4545 try {
46- const [ block , transactions ] = await get ( fetchBlockAtom )
46+ const [ blockResult , transactionResults ] = await get ( fetchBlockResultAtom )
4747
48- if ( transactions && transactions . length > 0 ) {
48+ if ( transactionResults && transactionResults . length > 0 ) {
4949 set ( transactionsAtom , ( prev ) => {
50- return new Map ( [ ...prev , ...transactions . map ( ( t : TransactionResult ) => [ t . id , t ] as const ) ] )
50+ transactionResults . forEach ( ( t ) => {
51+ prev . set ( t . id , t )
52+ } )
53+ return prev
5154 } )
5255 }
5356
5457 set ( blocksAtom , ( prev ) => {
55- return new Map ( [ ... prev , [ block . round , block ] ] )
58+ return prev . set ( blockResult . round , blockResult )
5659 } )
5760 } catch ( e ) {
5861 // Ignore any errors as there is nothing to sync
@@ -62,31 +65,31 @@ const getBlockDetailsAtomBuilder = (store: JotaiStore, round: Round) => {
6265
6366 return atom ( async ( get ) => {
6467 const blocks = store . get ( blocksAtom )
65- const cachedBlock = blocks . get ( round )
68+ const cachedBlockResult = blocks . get ( round )
6669 const nextRoundAvailable = get ( nextRoundAvailableAtomBuilder ( store , round ) )
67- if ( cachedBlock ) {
70+ if ( cachedBlockResult ) {
6871 const transactions = await get (
69- fetchTransactionsModelAtomBuilder ( store , fetchTransactionsAtomBuilder ( store , cachedBlock . transactionIds ) )
72+ fetchTransactionsModelAtomBuilder ( store , fetchTransactionsAtomBuilder ( store , cachedBlockResult . transactionIds ) )
7073 )
71- return asBlockDetails ( cachedBlock , transactions , nextRoundAvailable )
74+ return asBlock ( cachedBlockResult , transactions , nextRoundAvailable )
7275 }
7376
7477 get ( syncEffect )
7578
76- const [ fetchedBlock , fetchedBlockTransactions ] = await get ( fetchBlockAtom )
77- const transactions = await get ( fetchTransactionsModelAtomBuilder ( store , fetchedBlockTransactions ) )
78- return asBlockDetails ( fetchedBlock , transactions , nextRoundAvailable )
79+ const [ blockResult , transactionResults ] = await get ( fetchBlockResultAtom )
80+ const transactions = await get ( fetchTransactionsModelAtomBuilder ( store , transactionResults ) )
81+ return asBlock ( blockResult , transactions , nextRoundAvailable )
7982 } )
8083}
8184
82- const useBlockDetailsAtom = ( round : Round ) => {
85+ const useBlockAtom = ( round : Round ) => {
8386 const store = useStore ( )
8487
8588 return useMemo ( ( ) => {
86- return getBlockDetailsAtomBuilder ( store , round )
89+ return getBlockAtomBuilder ( store , round )
8790 } , [ store , round ] )
8891}
8992
90- export const useLoadableBlockDetails = ( round : Round ) => {
91- return useAtomValue ( loadable ( useBlockDetailsAtom ( round ) ) )
93+ export const useLoadableBlock = ( round : Round ) => {
94+ return useAtomValue ( loadable ( useBlockAtom ( round ) ) )
9295}
0 commit comments