1- import React , { createContext , useState , useContext , useMemo , useCallback } from "react" ;
1+ import React , { createContext , useState , useContext , useMemo , useCallback , useEffect } from "react" ;
22
3+ import { useLocation } from "react-router-dom" ;
34import { Address } from "viem" ;
45
56import { DEFAULT_CHAIN } from "consts/chains" ;
67import { klerosCoreAddress } from "hooks/contracts/generated" ;
78import { useLocalStorage } from "hooks/useLocalStorage" ;
89import { isEmpty , isUndefined } from "utils/index" ;
910
11+ export const MIN_DISPUTE_BATCH_SIZE = 2 ;
12+
1013export type Answer = {
1114 id : string ;
1215 title : string ;
@@ -59,6 +62,10 @@ interface INewDisputeContext {
5962 setIsSubmittingCase : ( isSubmittingCase : boolean ) => void ;
6063 isPolicyUploading : boolean ;
6164 setIsPolicyUploading : ( isPolicyUploading : boolean ) => void ;
65+ isBatchCreation : boolean ;
66+ setIsBatchCreation : ( isBatchCreation : boolean ) => void ;
67+ batchSize : number ;
68+ setBatchSize : ( batchSize ?: number ) => void ;
6269}
6370
6471const getInitialDisputeData = ( ) : IDisputeData => ( {
@@ -92,13 +99,26 @@ export const NewDisputeProvider: React.FC<{ children: React.ReactNode }> = ({ ch
9299 const [ disputeData , setDisputeData ] = useLocalStorage < IDisputeData > ( "disputeData" , initialDisputeData ) ;
93100 const [ isSubmittingCase , setIsSubmittingCase ] = useState < boolean > ( false ) ;
94101 const [ isPolicyUploading , setIsPolicyUploading ] = useState < boolean > ( false ) ;
102+ const [ isBatchCreation , setIsBatchCreation ] = useState < boolean > ( false ) ;
103+ const [ batchSize , setBatchSize ] = useLocalStorage < number > ( "disputeBatchSize" , MIN_DISPUTE_BATCH_SIZE ) ;
95104
96105 const disputeTemplate = useMemo ( ( ) => constructDisputeTemplate ( disputeData ) , [ disputeData ] ) ;
106+ const location = useLocation ( ) ;
97107
98108 const resetDisputeData = useCallback ( ( ) => {
99109 const freshData = getInitialDisputeData ( ) ;
100110 setDisputeData ( freshData ) ;
101- } , [ setDisputeData ] ) ;
111+ setBatchSize ( MIN_DISPUTE_BATCH_SIZE ) ;
112+ // eslint-disable-next-line react-hooks/exhaustive-deps
113+ } , [ ] ) ;
114+
115+ useEffect ( ( ) => {
116+ // Cleanup function to clear local storage when user leaves the route
117+ if ( location . pathname . includes ( "/resolver" ) || location . pathname . includes ( "/attachment" ) ) return ;
118+
119+ resetDisputeData ( ) ;
120+ // eslint-disable-next-line react-hooks/exhaustive-deps
121+ } , [ location . pathname ] ) ;
102122
103123 const contextValues = useMemo (
104124 ( ) => ( {
@@ -110,8 +130,23 @@ export const NewDisputeProvider: React.FC<{ children: React.ReactNode }> = ({ ch
110130 setIsSubmittingCase,
111131 isPolicyUploading,
112132 setIsPolicyUploading,
133+ isBatchCreation,
134+ setIsBatchCreation,
135+ batchSize,
136+ setBatchSize,
113137 } ) ,
114- [ disputeData , disputeTemplate , resetDisputeData , isSubmittingCase , isPolicyUploading , setDisputeData ]
138+ [
139+ disputeData ,
140+ disputeTemplate ,
141+ resetDisputeData ,
142+ isSubmittingCase ,
143+ isPolicyUploading ,
144+ setDisputeData ,
145+ isBatchCreation ,
146+ setIsBatchCreation ,
147+ batchSize ,
148+ setBatchSize ,
149+ ]
115150 ) ;
116151
117152 return < NewDisputeContext . Provider value = { contextValues } > { children } </ NewDisputeContext . Provider > ;
0 commit comments