@@ -9,6 +9,8 @@ import { useMemo } from 'react'
99import { loadable } from 'jotai/utils'
1010import { blockResultsAtom , syncedRoundAtom } from './core'
1111import { BlockResult , Round } from './types'
12+ import { groupResultsAtom } from '@/features/groups/data/core'
13+ import { GroupId , GroupResult } from '@/features/groups/data/types'
1214
1315const nextRoundAvailableAtomBuilder = ( store : JotaiStore , round : Round ) => {
1416 // This atom conditionally subscribes to updates on the syncedRoundAtom
@@ -19,33 +21,50 @@ const nextRoundAvailableAtomBuilder = (store: JotaiStore, round: Round) => {
1921 } )
2022}
2123
22- const fetchBlockResultAtomBuilder = ( round : Round ) => {
24+ export const fetchBlockResultAtomBuilder = ( round : Round ) => {
2325 return atom ( async ( _get ) => {
2426 return await indexer
2527 . lookupBlock ( round )
2628 . do ( )
2729 . then ( ( result ) => {
30+ const [ transactionIds , groupResults ] = ( ( result . transactions ?? [ ] ) as TransactionResult [ ] ) . reduce (
31+ ( acc , t ) => {
32+ acc [ 0 ] . push ( t . id )
33+ if ( t . group ) {
34+ const group : GroupResult = acc [ 1 ] . get ( t . group ) ?? {
35+ id : t . group ,
36+ round : result . round as number ,
37+ timestamp : new Date ( result . timestamp * 1000 ) . toISOString ( ) ,
38+ transactionIds : [ ] ,
39+ }
40+ group . transactionIds . push ( t . id )
41+ acc [ 1 ] . set ( t . group , group )
42+ }
43+ return acc
44+ } ,
45+ [ [ ] , new Map ( ) ] as [ string [ ] , Map < GroupId , GroupResult > ]
46+ )
47+
2848 return [
2949 {
3050 round : result . round as number ,
3151 timestamp : new Date ( result . timestamp * 1000 ) . toISOString ( ) ,
32- transactionIds : result . transactions ?. map ( ( t : TransactionResult ) => t . id ) ?? [ ] ,
52+ transactionIds,
3353 } as BlockResult ,
3454 ( result . transactions ?? [ ] ) as TransactionResult [ ] ,
55+ groupResults ,
3556 ] as const
3657 } )
3758 } )
3859}
3960
40- const getBlockAtomBuilder = ( store : JotaiStore , round : Round ) => {
41- const fetchBlockResultAtom = fetchBlockResultAtomBuilder ( round )
42-
43- const syncEffect = atomEffect ( ( get , set ) => {
61+ export const syncBlockAtomEffectBuilder = ( fetchBlockResultAtom : ReturnType < typeof fetchBlockResultAtomBuilder > ) => {
62+ return atomEffect ( ( get , set ) => {
4463 ; ( async ( ) => {
4564 try {
46- const [ blockResult , transactionResults ] = await get ( fetchBlockResultAtom )
65+ const [ blockResult , transactionResults , groupResults ] = await get ( fetchBlockResultAtom )
4766
48- if ( transactionResults && transactionResults . length > 0 ) {
67+ if ( transactionResults . length > 0 ) {
4968 set ( transactionResultsAtom , ( prev ) => {
5069 transactionResults . forEach ( ( t ) => {
5170 prev . set ( t . id , t )
@@ -54,6 +73,15 @@ const getBlockAtomBuilder = (store: JotaiStore, round: Round) => {
5473 } )
5574 }
5675
76+ if ( groupResults . size > 0 ) {
77+ set ( groupResultsAtom , ( prev ) => {
78+ groupResults . forEach ( ( g ) => {
79+ prev . set ( g . id , g )
80+ } )
81+ return prev
82+ } )
83+ }
84+
5785 set ( blockResultsAtom , ( prev ) => {
5886 return prev . set ( blockResult . round , blockResult )
5987 } )
@@ -62,6 +90,11 @@ const getBlockAtomBuilder = (store: JotaiStore, round: Round) => {
6290 }
6391 } ) ( )
6492 } )
93+ }
94+
95+ const getBlockAtomBuilder = ( store : JotaiStore , round : Round ) => {
96+ const fetchBlockResultAtom = fetchBlockResultAtomBuilder ( round )
97+ const syncEffect = syncBlockAtomEffectBuilder ( fetchBlockResultAtom )
6598
6699 return atom ( async ( get ) => {
67100 const blockResults = store . get ( blockResultsAtom )
0 commit comments