Skip to content

Commit 1368942

Browse files
committed
feat: state context improvement, style impro
1 parent 5098f79 commit 1368942

File tree

2 files changed

+117
-111
lines changed

2 files changed

+117
-111
lines changed
Lines changed: 116 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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";
23

34
export interface IToken {
45
symbol: string;
@@ -12,31 +13,31 @@ interface INewTransactionContext {
1213
escrowTitle: string;
1314
setEscrowTitle: (title: string) => void;
1415
deliverableText: string;
15-
setDeliverableText: (deliverableText: string) => void;
16+
setDeliverableText: (text: string) => void;
1617
deliverableFile: File | undefined;
17-
setDeliverableFile: (deliverableFile: File | undefined) => void;
18+
setDeliverableFile: (file: File | undefined) => void;
1819
extraDescriptionUri: string;
19-
setExtraDescriptionUri: (extraDescriptionUri: string) => void;
20+
setExtraDescriptionUri: (uri: string) => void;
2021
transactionUri: string;
21-
setTransactionUri: (transactionUri: string) => void;
22+
setTransactionUri: (uri: string) => void;
2223
isFileUploading: boolean;
23-
setIsFileUploading: (isFileUploading: boolean) => void;
24+
setIsFileUploading: (v: boolean) => void;
2425
receivingQuantity: string;
25-
setReceivingQuantity: (quantity: string) => void;
26+
setReceivingQuantity: (qty: string) => void;
2627
receivingToken: string;
2728
setReceivingToken: (token: string) => void;
2829
sellerAddress: string;
29-
setSellerAddress: (address: string) => void;
30+
setSellerAddress: (addr: string) => void;
3031
sendingQuantity: string;
31-
setSendingQuantity: (quantity: string) => void;
32+
setSendingQuantity: (qty: string) => void;
3233
sendingToken: IToken;
3334
setSendingToken: (token: IToken) => void;
3435
buyerAddress: string;
35-
setBuyerAddress: (address: string) => void;
36+
setBuyerAddress: (addr: string) => void;
3637
isBuyerAddressCustom: boolean;
3738
setIsBuyerAddressCustom: (v: boolean) => void;
3839
deadline: string;
39-
setDeadline: (deadline: string) => void;
40+
setDeadline: (d: string) => void;
4041
notificationEmail: string;
4142
setNotificationEmail: (email: string) => void;
4243
resetContext: () => void;
@@ -48,36 +49,38 @@ interface INewTransactionContext {
4849
setIsBuyerAddressResolved: (v: boolean) => void;
4950
}
5051

51-
const NewTransactionContext = createContext<INewTransactionContext>({} as INewTransactionContext);
52+
const initialToken: IToken = { address: "native", symbol: "", logo: "" };
5253

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+
};
5461

5562
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", "");
5966
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", "");
6269
const [isFileUploading, setIsFileUploading] = useState(false);
6370
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);
7578
const [isRecipientAddressResolved, setIsRecipientAddressResolved] = useState(false);
7679
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", "");
7982

80-
const resetContext = () => {
83+
const resetContext = useCallback(() => {
8184
setEscrowType("general");
8285
setEscrowTitle("");
8386
setDeliverableText("");
@@ -89,93 +92,96 @@ export const NewTransactionProvider: React.FC<{ children: React.ReactNode }> = (
8992
setReceivingToken("");
9093
setSellerAddress("");
9194
setSendingQuantity("");
92-
setSendingToken({ address: "native", symbol: "", logo: "" });
95+
setSendingToken(initialToken);
9396
setBuyerAddress("");
9497
setIsBuyerAddressCustom(false);
9598
setDeadline("");
9699
setNotificationEmail("");
97100
setHasSufficientNativeBalance(true);
98101
setIsRecipientAddressResolved(false);
99102
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);
117103
}, [
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,
132118
]);
133119

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+
]
180184
);
185+
186+
return <NewTransactionContext.Provider value={contextValues}>{children}</NewTransactionContext.Provider>;
181187
};

web/src/pages/NewTransaction/Terms/Payment/BuyerAddress.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const Collapse = styled.div<{ $open: boolean; }>`
6161
max-height: ${({ $open }) => ($open ? "160px" : "0")};
6262
opacity: ${({ $open }) => ($open ? 1 : 0)};
6363
overflow: hidden;
64-
transition: max-height 0.6s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.6s ease;
64+
transition: max-height 0.5s, opacity 0.5s ease;
6565
`;
6666

6767
const SecondaryLabel = styled.label`

0 commit comments

Comments
 (0)