/ packages / DApp / src / hooks / useFeaturedBatches.ts
useFeaturedBatches.ts
 1  import { config } from '../config'
 2  import { useFeaturedVotes } from '../providers/featuredVotes/provider'
 3  
 4  export const useFeaturedBatches = () => {
 5    const { activeVoting, votes } = useFeaturedVotes()
 6  
 7    if (!activeVoting) {
 8      return {
 9        finalizeVotingLimit: 1,
10        batchCount: 1,
11        batchDoneCount: 0,
12        beingEvaluated: false,
13        beingFinalized: false,
14      }
15    }
16  
17    const evaluated = activeVoting.evaluated
18    const finalized = activeVoting.finalized
19    const allVotes = votes ?? {}
20    const votesCount: number = Object.values(allVotes).reduce(
21      (acc: number, curr: any) => acc + Object.keys(curr?.votes).length,
22      0,
23    )
24  
25    const beingFinalized = !evaluated && finalized
26    const beingEvaluated = evaluated && !finalized
27    const currentPosition = activeVoting.evaluatingPos
28    const firstFinalization = beingEvaluated && currentPosition === votesCount + 1
29  
30    const votesLeftCount = votesCount - currentPosition + 1
31    const finalizeVotingLimit = firstFinalization
32      ? Math.min(votesCount, config.votesLimit)
33      : Math.min(votesLeftCount, config.votesLimit)
34    const batchCount = Math.ceil((beingFinalized ? votesCount + 1 : votesCount) / config.votesLimit)
35    const batchLeftCount = Math.ceil(votesLeftCount / config.votesLimit)
36  
37    const batchDoneCount = batchCount - batchLeftCount
38  
39    return {
40      finalizeVotingLimit,
41      batchCount,
42      batchDoneCount,
43      beingFinalized,
44      beingEvaluated,
45    }
46  }