Skip to content

Commit 8b3846e

Browse files
committed
refact: update EscrowProvider to use Escrow type and introduce SingleEscrowPayload and MultiEscrowPayload for improved type management
1 parent 071f1da commit 8b3846e

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

templates/providers/EscrowProvider.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ import {
1515
Trustline,
1616
} from "@trustless-work/escrow/types";
1717

18-
type RelaxedEscrow = Omit<
18+
type SingleEscrowPayload = Omit<
19+
Escrow,
20+
"type" | "updatedAt" | "createdAt" | "user" | "trustline"
21+
> &
22+
Partial<Pick<Escrow, "type" | "updatedAt" | "createdAt" | "user">> & {
23+
trustline: Trustline & { name?: string };
24+
};
25+
26+
type MultiEscrowPayload = Omit<
1927
Escrow,
2028
"type" | "updatedAt" | "createdAt" | "user" | "trustline" | "amount"
2129
> &
@@ -26,20 +34,17 @@ type RelaxedEscrow = Omit<
2634
};
2735

2836
type EscrowContextType = {
29-
selectedEscrow: RelaxedEscrow | null;
37+
selectedEscrow: Escrow | null;
3038
hasEscrow: boolean;
3139
userRolesInEscrow: string[];
3240
updateEscrow: (
33-
updater:
34-
| Partial<RelaxedEscrow>
35-
| ((previous: RelaxedEscrow) => RelaxedEscrow)
36-
) => void;
37-
setEscrowField: <K extends keyof RelaxedEscrow>(
38-
key: K,
39-
value: RelaxedEscrow[K]
41+
updater: Partial<Escrow> | ((previous: Escrow) => Escrow)
4042
) => void;
43+
setEscrowField: <K extends keyof Escrow>(key: K, value: Escrow[K]) => void;
4144
clearEscrow: () => void;
42-
setSelectedEscrow: (escrow?: RelaxedEscrow) => void;
45+
setSelectedEscrow: (
46+
escrow?: SingleEscrowPayload | MultiEscrowPayload
47+
) => void;
4348
setUserRolesInEscrow: (roles: string[]) => void;
4449
};
4550

@@ -48,8 +53,9 @@ const EscrowContext = createContext<EscrowContextType | undefined>(undefined);
4853
const LOCAL_STORAGE_KEY = "selectedEscrow";
4954

5055
export const EscrowProvider = ({ children }: { children: ReactNode }) => {
51-
const [selectedEscrow, setSelectedEscrowState] =
52-
useState<RelaxedEscrow | null>(null);
56+
const [selectedEscrow, setSelectedEscrowState] = useState<Escrow | null>(
57+
null
58+
);
5359
const [userRolesInEscrow, setUserRolesInEscrowState] = useState<string[]>([]);
5460

5561
/**
@@ -59,7 +65,7 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
5965
try {
6066
const stored = localStorage.getItem(LOCAL_STORAGE_KEY);
6167
if (stored) {
62-
const parsed: RelaxedEscrow = JSON.parse(stored);
68+
const parsed: Escrow = JSON.parse(stored);
6369
setSelectedEscrowState(parsed);
6470
}
6571
} catch (_err) {
@@ -72,7 +78,7 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
7278
*
7379
* @param value - The escrow to persist
7480
*/
75-
const persist = (value: RelaxedEscrow | null) => {
81+
const persist = (value: Escrow | null) => {
7682
if (value) {
7783
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(value));
7884
} else {
@@ -106,7 +112,7 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
106112
const setEscrowField: EscrowContextType["setEscrowField"] = (key, value) => {
107113
setSelectedEscrowState((current) => {
108114
if (!current) return current;
109-
const next = { ...current, [key]: value } as RelaxedEscrow;
115+
const next = { ...current, [key]: value } as Escrow;
110116
persist(next);
111117
return next;
112118
});
@@ -151,8 +157,8 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
151157
updateEscrow,
152158
setEscrowField,
153159
clearEscrow,
154-
setSelectedEscrow: (value?: RelaxedEscrow) =>
155-
setSelectedEscrowState(value ?? null),
160+
setSelectedEscrow: (value?: SingleEscrowPayload | MultiEscrowPayload) =>
161+
setSelectedEscrowState((value as unknown as Escrow) ?? null),
156162
setUserRolesInEscrow,
157163
userRolesInEscrow,
158164
}}

0 commit comments

Comments
 (0)