1
- import React , { createContext , useState , useContext , useEffect } from "react" ;
1
+ import React , { createContext , useContext , useState , useCallback , useMemo } from "react" ;
2
+ import { useLocalStorage } from "hooks/useLocalStorage" ;
2
3
3
4
export interface IToken {
4
5
symbol : string ;
@@ -12,31 +13,31 @@ interface INewTransactionContext {
12
13
escrowTitle : string ;
13
14
setEscrowTitle : ( title : string ) => void ;
14
15
deliverableText : string ;
15
- setDeliverableText : ( deliverableText : string ) => void ;
16
+ setDeliverableText : ( text : string ) => void ;
16
17
deliverableFile : File | undefined ;
17
- setDeliverableFile : ( deliverableFile : File | undefined ) => void ;
18
+ setDeliverableFile : ( file : File | undefined ) => void ;
18
19
extraDescriptionUri : string ;
19
- setExtraDescriptionUri : ( extraDescriptionUri : string ) => void ;
20
+ setExtraDescriptionUri : ( uri : string ) => void ;
20
21
transactionUri : string ;
21
- setTransactionUri : ( transactionUri : string ) => void ;
22
+ setTransactionUri : ( uri : string ) => void ;
22
23
isFileUploading : boolean ;
23
- setIsFileUploading : ( isFileUploading : boolean ) => void ;
24
+ setIsFileUploading : ( v : boolean ) => void ;
24
25
receivingQuantity : string ;
25
- setReceivingQuantity : ( quantity : string ) => void ;
26
+ setReceivingQuantity : ( qty : string ) => void ;
26
27
receivingToken : string ;
27
28
setReceivingToken : ( token : string ) => void ;
28
29
sellerAddress : string ;
29
- setSellerAddress : ( address : string ) => void ;
30
+ setSellerAddress : ( addr : string ) => void ;
30
31
sendingQuantity : string ;
31
- setSendingQuantity : ( quantity : string ) => void ;
32
+ setSendingQuantity : ( qty : string ) => void ;
32
33
sendingToken : IToken ;
33
34
setSendingToken : ( token : IToken ) => void ;
34
35
buyerAddress : string ;
35
- setBuyerAddress : ( address : string ) => void ;
36
+ setBuyerAddress : ( addr : string ) => void ;
36
37
isBuyerAddressCustom : boolean ;
37
38
setIsBuyerAddressCustom : ( v : boolean ) => void ;
38
39
deadline : string ;
39
- setDeadline : ( deadline : string ) => void ;
40
+ setDeadline : ( d : string ) => void ;
40
41
notificationEmail : string ;
41
42
setNotificationEmail : ( email : string ) => void ;
42
43
resetContext : ( ) => void ;
@@ -48,36 +49,38 @@ interface INewTransactionContext {
48
49
setIsBuyerAddressResolved : ( v : boolean ) => void ;
49
50
}
50
51
51
- const NewTransactionContext = createContext < INewTransactionContext > ( { } as INewTransactionContext ) ;
52
+ const initialToken : IToken = { address : "native" , symbol : "" , logo : "" } ;
52
53
53
- export const useNewTransactionContext = ( ) => useContext ( NewTransactionContext ) ;
54
+ const NewTransactionContext = createContext < INewTransactionContext | undefined > ( undefined ) ;
55
+
56
+ export const useNewTransactionContext = ( ) => {
57
+ const context = useContext ( NewTransactionContext ) ;
58
+ if ( ! context ) throw new Error ( "Context Provider not found." ) ;
59
+ return context ;
60
+ } ;
54
61
55
62
export const NewTransactionProvider : React . FC < { children : React . ReactNode } > = ( { children } ) => {
56
- const [ escrowType , setEscrowType ] = useState ( localStorage . getItem ( "escrowType" ) || "general" ) ;
57
- const [ escrowTitle , setEscrowTitle ] = useState ( localStorage . getItem ( "escrowTitle" ) || "" ) ;
58
- const [ deliverableText , setDeliverableText ] = useState ( localStorage . getItem ( "deliverableText" ) || "" ) ;
63
+ const [ escrowType , setEscrowType ] = useLocalStorage ( "escrowType" , "general" ) ;
64
+ const [ escrowTitle , setEscrowTitle ] = useLocalStorage ( "escrowTitle" , "" ) ;
65
+ const [ deliverableText , setDeliverableText ] = useLocalStorage ( "deliverableText" , "" ) ;
59
66
const [ deliverableFile , setDeliverableFile ] = useState < File | undefined > ( ) ;
60
- const [ transactionUri , setTransactionUri ] = useState ( localStorage . getItem ( "transactionUri" ) || "" ) ;
61
- const [ extraDescriptionUri , setExtraDescriptionUri ] = useState ( localStorage . getItem ( "extraDescriptionUri" ) || "" ) ;
67
+ const [ transactionUri , setTransactionUri ] = useLocalStorage ( "transactionUri" , "" ) ;
68
+ const [ extraDescriptionUri , setExtraDescriptionUri ] = useLocalStorage ( "extraDescriptionUri" , "" ) ;
62
69
const [ isFileUploading , setIsFileUploading ] = useState ( false ) ;
63
70
const [ hasSufficientNativeBalance , setHasSufficientNativeBalance ] = useState ( true ) ;
64
- const [ receivingQuantity , setReceivingQuantity ] = useState ( localStorage . getItem ( "receivingQuantity" ) || "" ) ;
65
- const [ receivingToken , setReceivingToken ] = useState ( localStorage . getItem ( "receivingToken" ) || "" ) ;
66
- const [ sellerAddress , setSellerAddress ] = useState ( localStorage . getItem ( "sellerAddress" ) || "" ) ;
67
- const [ sendingQuantity , setSendingQuantity ] = useState ( localStorage . getItem ( "sendingQuantity" ) || "" ) ;
68
- const [ sendingToken , setSendingToken ] = useState < IToken > (
69
- JSON . parse ( localStorage . getItem ( "sendingToken" ) ?? "null" ) || { address : "native" , symbol : "" , logo : "" }
70
- ) ;
71
- const [ buyerAddress , setBuyerAddress ] = useState ( localStorage . getItem ( "buyerAddress" ) ?? "" ) ;
72
- const [ isBuyerAddressCustom , setIsBuyerAddressCustom ] = useState (
73
- JSON . parse ( localStorage . getItem ( "isBuyerAddressCustom" ) ?? "false" )
74
- ) ;
71
+ const [ receivingQuantity , setReceivingQuantity ] = useLocalStorage ( "receivingQuantity" , "" ) ;
72
+ const [ receivingToken , setReceivingToken ] = useLocalStorage ( "receivingToken" , "" ) ;
73
+ const [ sellerAddress , setSellerAddress ] = useLocalStorage ( "sellerAddress" , "" ) ;
74
+ const [ sendingQuantity , setSendingQuantity ] = useLocalStorage ( "sendingQuantity" , "" ) ;
75
+ const [ sendingToken , setSendingToken ] = useLocalStorage ( "sendingToken" , initialToken ) ;
76
+ const [ buyerAddress , setBuyerAddress ] = useLocalStorage ( "buyerAddress" , "" ) ;
77
+ const [ isBuyerAddressCustom , setIsBuyerAddressCustom ] = useLocalStorage ( "isBuyerAddressCustom" , false ) ;
75
78
const [ isRecipientAddressResolved , setIsRecipientAddressResolved ] = useState ( false ) ;
76
79
const [ isBuyerAddressResolved , setIsBuyerAddressResolved ] = useState ( false ) ;
77
- const [ deadline , setDeadline ] = useState ( localStorage . getItem ( "deadline" ) || "" ) ;
78
- const [ notificationEmail , setNotificationEmail ] = useState ( localStorage . getItem ( "notificationEmail" ) || "" ) ;
80
+ const [ deadline , setDeadline ] = useLocalStorage ( "deadline" , "" ) ;
81
+ const [ notificationEmail , setNotificationEmail ] = useLocalStorage ( "notificationEmail" , "" ) ;
79
82
80
- const resetContext = ( ) => {
83
+ const resetContext = useCallback ( ( ) => {
81
84
setEscrowType ( "general" ) ;
82
85
setEscrowTitle ( "" ) ;
83
86
setDeliverableText ( "" ) ;
@@ -89,93 +92,96 @@ export const NewTransactionProvider: React.FC<{ children: React.ReactNode }> = (
89
92
setReceivingToken ( "" ) ;
90
93
setSellerAddress ( "" ) ;
91
94
setSendingQuantity ( "" ) ;
92
- setSendingToken ( { address : "native" , symbol : "" , logo : "" } ) ;
95
+ setSendingToken ( initialToken ) ;
93
96
setBuyerAddress ( "" ) ;
94
97
setIsBuyerAddressCustom ( false ) ;
95
98
setDeadline ( "" ) ;
96
99
setNotificationEmail ( "" ) ;
97
100
setHasSufficientNativeBalance ( true ) ;
98
101
setIsRecipientAddressResolved ( false ) ;
99
102
setIsBuyerAddressResolved ( false ) ;
100
- } ;
101
-
102
- useEffect ( ( ) => {
103
- localStorage . setItem ( "escrowType" , escrowType ) ;
104
- localStorage . setItem ( "escrowTitle" , escrowTitle ) ;
105
- localStorage . setItem ( "deliverableText" , deliverableText ) ;
106
- localStorage . setItem ( "extraDescriptionUri" , extraDescriptionUri ) ;
107
- localStorage . setItem ( "transactionUri" , transactionUri ) ;
108
- localStorage . setItem ( "receivingQuantity" , receivingQuantity ) ;
109
- localStorage . setItem ( "receivingToken" , receivingToken ) ;
110
- localStorage . setItem ( "buyerAddress" , buyerAddress ) ;
111
- localStorage . setItem ( "isBuyerAddressCustom" , JSON . stringify ( isBuyerAddressCustom ) ) ;
112
- localStorage . setItem ( "sendingQuantity" , sendingQuantity ) ;
113
- localStorage . setItem ( "sendingToken" , JSON . stringify ( sendingToken ) ) ;
114
- localStorage . setItem ( "sellerAddress" , sellerAddress ) ;
115
- localStorage . setItem ( "deadline" , deadline ) ;
116
- localStorage . setItem ( "notificationEmail" , notificationEmail ) ;
117
103
} , [
118
- escrowType ,
119
- escrowTitle ,
120
- deliverableText ,
121
- extraDescriptionUri ,
122
- transactionUri ,
123
- receivingQuantity ,
124
- receivingToken ,
125
- buyerAddress ,
126
- isBuyerAddressCustom ,
127
- sendingQuantity ,
128
- sendingToken ,
129
- sellerAddress ,
130
- deadline ,
131
- notificationEmail ,
104
+ setEscrowType ,
105
+ setEscrowTitle ,
106
+ setDeliverableText ,
107
+ setExtraDescriptionUri ,
108
+ setTransactionUri ,
109
+ setReceivingQuantity ,
110
+ setReceivingToken ,
111
+ setSellerAddress ,
112
+ setSendingQuantity ,
113
+ setSendingToken ,
114
+ setBuyerAddress ,
115
+ setIsBuyerAddressCustom ,
116
+ setDeadline ,
117
+ setNotificationEmail ,
132
118
] ) ;
133
119
134
- return (
135
- < NewTransactionContext . Provider
136
- value = { {
137
- escrowType,
138
- setEscrowType,
139
- escrowTitle,
140
- setEscrowTitle,
141
- deliverableText,
142
- setDeliverableText,
143
- deliverableFile,
144
- setDeliverableFile,
145
- extraDescriptionUri,
146
- setExtraDescriptionUri,
147
- transactionUri,
148
- setTransactionUri,
149
- isFileUploading,
150
- setIsFileUploading,
151
- receivingQuantity,
152
- setReceivingQuantity,
153
- receivingToken,
154
- setReceivingToken,
155
- buyerAddress,
156
- setBuyerAddress,
157
- isBuyerAddressCustom,
158
- setIsBuyerAddressCustom,
159
- sendingQuantity,
160
- setSendingQuantity,
161
- hasSufficientNativeBalance,
162
- setHasSufficientNativeBalance,
163
- sendingToken,
164
- setSendingToken,
165
- sellerAddress,
166
- setSellerAddress,
167
- isRecipientAddressResolved,
168
- setIsRecipientAddressResolved,
169
- isBuyerAddressResolved,
170
- setIsBuyerAddressResolved,
171
- deadline,
172
- setDeadline,
173
- notificationEmail,
174
- setNotificationEmail,
175
- resetContext,
176
- } }
177
- >
178
- { children }
179
- </ NewTransactionContext . Provider >
120
+ const contextValues = useMemo (
121
+ ( ) => ( {
122
+ escrowType,
123
+ setEscrowType,
124
+ escrowTitle,
125
+ setEscrowTitle,
126
+ deliverableText,
127
+ setDeliverableText,
128
+ deliverableFile,
129
+ setDeliverableFile,
130
+ extraDescriptionUri,
131
+ setExtraDescriptionUri,
132
+ transactionUri,
133
+ setTransactionUri,
134
+ isFileUploading,
135
+ setIsFileUploading,
136
+ receivingQuantity,
137
+ setReceivingQuantity,
138
+ receivingToken,
139
+ setReceivingToken,
140
+ buyerAddress,
141
+ setBuyerAddress,
142
+ isBuyerAddressCustom,
143
+ setIsBuyerAddressCustom,
144
+ sendingQuantity,
145
+ setSendingQuantity,
146
+ hasSufficientNativeBalance,
147
+ setHasSufficientNativeBalance,
148
+ sendingToken,
149
+ setSendingToken,
150
+ sellerAddress,
151
+ setSellerAddress,
152
+ isRecipientAddressResolved,
153
+ setIsRecipientAddressResolved,
154
+ isBuyerAddressResolved,
155
+ setIsBuyerAddressResolved,
156
+ deadline,
157
+ setDeadline,
158
+ notificationEmail,
159
+ setNotificationEmail,
160
+ resetContext,
161
+ } ) ,
162
+ [
163
+ escrowType ,
164
+ escrowTitle ,
165
+ deliverableText ,
166
+ deliverableFile ,
167
+ extraDescriptionUri ,
168
+ transactionUri ,
169
+ isFileUploading ,
170
+ receivingQuantity ,
171
+ receivingToken ,
172
+ buyerAddress ,
173
+ isBuyerAddressCustom ,
174
+ sendingQuantity ,
175
+ hasSufficientNativeBalance ,
176
+ sendingToken ,
177
+ sellerAddress ,
178
+ isRecipientAddressResolved ,
179
+ isBuyerAddressResolved ,
180
+ deadline ,
181
+ notificationEmail ,
182
+ resetContext ,
183
+ ]
180
184
) ;
185
+
186
+ return < NewTransactionContext . Provider value = { contextValues } > { children } </ NewTransactionContext . Provider > ;
181
187
} ;
0 commit comments