@trustless-work/blocks 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +39 -13
  2. package/bin/index.js +1128 -1137
  3. package/package.json +44 -44
  4. package/templates/escrows/details/EscrowDetailDialog.tsx +3 -3
  5. package/templates/escrows/details/GeneralInformation.tsx +2 -2
  6. package/templates/escrows/details/SuccessReleaseDialog.tsx +2 -3
  7. package/templates/escrows/details/useDetailsEscrow.ts +2 -2
  8. package/templates/escrows/escrows-by-role/cards/EscrowsCards.tsx +32 -16
  9. package/templates/escrows/escrows-by-role/table/EscrowsTable.tsx +34 -18
  10. package/templates/escrows/escrows-by-role/useEscrowsByRole.shared.ts +33 -25
  11. package/templates/escrows/escrows-by-signer/cards/EscrowsCards.tsx +22 -16
  12. package/templates/escrows/escrows-by-signer/table/EscrowsTable.tsx +23 -17
  13. package/templates/escrows/escrows-by-signer/useEscrowsBySigner.shared.ts +32 -25
  14. package/templates/escrows/single-release/approve-milestone/button/ApproveMilestone.tsx +1 -1
  15. package/templates/escrows/single-release/approve-milestone/dialog/ApproveMilestone.tsx +1 -1
  16. package/templates/escrows/single-release/approve-milestone/form/ApproveMilestone.tsx +1 -1
  17. package/templates/escrows/single-release/approve-milestone/shared/useApproveMilestone.ts +1 -1
  18. package/templates/escrows/single-release/change-milestone-status/button/ChangeMilestoneStatus.tsx +1 -1
  19. package/templates/escrows/single-release/change-milestone-status/dialog/ChangeMilestoneStatus.tsx +1 -1
  20. package/templates/escrows/single-release/change-milestone-status/form/ChangeMilestoneStatus.tsx +1 -1
  21. package/templates/escrows/single-release/change-milestone-status/shared/useChangeMilestoneStatus.ts +1 -1
  22. package/templates/escrows/single-release/dispute-escrow/button/DisputeEscrow.tsx +1 -1
  23. package/templates/escrows/single-release/fund-escrow/button/FundEscrow.tsx +1 -1
  24. package/templates/escrows/single-release/fund-escrow/shared/useFundEscrow.ts +1 -1
  25. package/templates/escrows/single-release/initialize-escrow/shared/useInitializeEscrow.ts +1 -1
  26. package/templates/escrows/single-release/release-escrow/button/ReleaseEscrow.tsx +3 -3
  27. package/templates/escrows/single-release/resolve-dispute/button/ResolveDispute.tsx +1 -1
  28. package/templates/escrows/single-release/resolve-dispute/dialog/ResolveDispute.tsx +1 -1
  29. package/templates/escrows/single-release/resolve-dispute/shared/useResolveDispute.ts +1 -1
  30. package/templates/escrows/single-release/update-escrow/shared/useUpdateEscrow.ts +224 -224
  31. package/templates/providers/ReactQueryClientProvider.tsx +3 -1
  32. /package/templates/{escrows/escrow-context → providers}/EscrowAmountProvider.tsx +0 -0
  33. /package/templates/{escrows/escrow-context → providers}/EscrowDialogsProvider.tsx +0 -0
  34. /package/templates/{escrows/escrow-context → providers}/EscrowProvider.tsx +0 -0
@@ -1,224 +1,224 @@
1
- import * as React from "react";
2
- import { useForm } from "react-hook-form";
3
- import { zodResolver } from "@hookform/resolvers/zod";
4
- import { useUpdateEscrowSchema } from "./schema";
5
- import { z } from "zod";
6
- import {
7
- UpdateSingleReleaseEscrowPayload,
8
- UpdateSingleReleaseEscrowResponse,
9
- } from "@trustless-work/escrow/types";
10
- import { toast } from "sonner";
11
- import { useEscrowContext } from "../../../escrow-context/EscrowProvider";
12
- import { useWalletContext } from "@/components/tw-blocks/wallet-kit/WalletProvider";
13
- import { useEscrowsMutations } from "@/components/tw-blocks/tanstack/useEscrowsMutations";
14
- import {
15
- ErrorResponse,
16
- handleError,
17
- } from "@/components/tw-blocks/handle-errors/handle";
18
- import { GetEscrowsFromIndexerResponse } from "@trustless-work/escrow/types";
19
-
20
- export function useUpdateEscrow() {
21
- const [isSubmitting, setIsSubmitting] = React.useState(false);
22
-
23
- const { getSingleReleaseFormSchema } = useUpdateEscrowSchema();
24
- const formSchema = getSingleReleaseFormSchema();
25
-
26
- const { walletAddress } = useWalletContext();
27
- const { selectedEscrow, setSelectedEscrow } = useEscrowContext();
28
- const { updateEscrow } = useEscrowsMutations();
29
-
30
- const form = useForm<z.infer<typeof formSchema>>({
31
- resolver: zodResolver(formSchema),
32
- defaultValues: {
33
- engagementId: selectedEscrow?.engagementId || "",
34
- title: selectedEscrow?.title || "",
35
- description: selectedEscrow?.description || "",
36
- platformFee: selectedEscrow?.platformFee as unknown as
37
- | number
38
- | string
39
- | undefined,
40
- amount: selectedEscrow?.amount as unknown as number | string | undefined,
41
- receiverMemo: selectedEscrow?.receiverMemo
42
- ? String(selectedEscrow.receiverMemo)
43
- : "",
44
- trustline: {
45
- address: selectedEscrow?.trustline?.address || "",
46
- decimals: 10000000,
47
- },
48
- roles: {
49
- approver: selectedEscrow?.roles?.approver || "",
50
- serviceProvider: selectedEscrow?.roles?.serviceProvider || "",
51
- platformAddress: selectedEscrow?.roles?.platformAddress || "",
52
- receiver: selectedEscrow?.roles?.receiver || "",
53
- releaseSigner: selectedEscrow?.roles?.releaseSigner || "",
54
- disputeResolver: selectedEscrow?.roles?.disputeResolver || "",
55
- },
56
- milestones: (selectedEscrow?.milestones || []).map((m: any) => ({
57
- description: m?.description || "",
58
- })),
59
- },
60
- mode: "onChange",
61
- });
62
-
63
- React.useEffect(() => {
64
- if (!selectedEscrow) return;
65
- form.reset({
66
- engagementId: selectedEscrow?.engagementId || "",
67
- title: selectedEscrow?.title || "",
68
- description: selectedEscrow?.description || "",
69
- platformFee:
70
- (selectedEscrow?.platformFee as unknown as
71
- | number
72
- | string
73
- | undefined) || "",
74
- amount:
75
- (selectedEscrow?.amount as unknown as number | string | undefined) ||
76
- "",
77
- receiverMemo: selectedEscrow?.receiverMemo
78
- ? String(selectedEscrow.receiverMemo)
79
- : "",
80
- trustline: {
81
- address: selectedEscrow?.trustline?.address || "",
82
- decimals: 10000000,
83
- },
84
- roles: {
85
- approver: selectedEscrow?.roles?.approver || "",
86
- serviceProvider: selectedEscrow?.roles?.serviceProvider || "",
87
- platformAddress: selectedEscrow?.roles?.platformAddress || "",
88
- receiver: selectedEscrow?.roles?.receiver || "",
89
- releaseSigner: selectedEscrow?.roles?.releaseSigner || "",
90
- disputeResolver: selectedEscrow?.roles?.disputeResolver || "",
91
- },
92
- milestones: (selectedEscrow?.milestones || []).map((m: any) => ({
93
- description: m?.description || "",
94
- })),
95
- });
96
- }, [selectedEscrow, form]);
97
-
98
- const milestones = form.watch("milestones");
99
- const isAnyMilestoneEmpty = milestones.some((m) => m.description === "");
100
-
101
- const handleAddMilestone = () => {
102
- const current = form.getValues("milestones");
103
- const updated = [...current, { description: "" }];
104
- form.setValue("milestones", updated);
105
- };
106
-
107
- const handleRemoveMilestone = (index: number) => {
108
- const current = form.getValues("milestones");
109
- const updated = current.filter((_, i) => i !== index);
110
- form.setValue("milestones", updated);
111
- };
112
-
113
- const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => {
114
- let rawValue = e.target.value;
115
- rawValue = rawValue.replace(/[^0-9.]/g, "");
116
- if (rawValue.split(".").length > 2) rawValue = rawValue.slice(0, -1);
117
- if (rawValue.includes(".")) {
118
- const parts = rawValue.split(".");
119
- if (parts[1] && parts[1].length > 2) {
120
- rawValue = parts[0] + "." + parts[1].slice(0, 2);
121
- }
122
- }
123
- form.setValue("amount", rawValue);
124
- };
125
-
126
- const handlePlatformFeeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
127
- let rawValue = e.target.value;
128
- rawValue = rawValue.replace(/[^0-9.]/g, "");
129
- if (rawValue.split(".").length > 2) rawValue = rawValue.slice(0, -1);
130
- if (rawValue.includes(".")) {
131
- const parts = rawValue.split(".");
132
- if (parts[1] && parts[1].length > 2) {
133
- rawValue = parts[0] + "." + parts[1].slice(0, 2);
134
- }
135
- }
136
- form.setValue("platformFee", rawValue);
137
- };
138
-
139
- const handleSubmit = form.handleSubmit(async (payload) => {
140
- try {
141
- setIsSubmitting(true);
142
-
143
- /**
144
- * Create the final payload for the update escrow mutation
145
- *
146
- * @param payload - The payload from the form
147
- * @returns The final payload for the update escrow mutation
148
- */
149
- const finalPayload: UpdateSingleReleaseEscrowPayload = {
150
- contractId: selectedEscrow?.contractId || "",
151
- signer: walletAddress || "",
152
- escrow: {
153
- engagementId: payload.engagementId,
154
- title: payload.title,
155
- description: payload.description,
156
- platformFee:
157
- typeof payload.platformFee === "string"
158
- ? Number(payload.platformFee)
159
- : payload.platformFee,
160
- amount:
161
- typeof payload.amount === "string"
162
- ? Number(payload.amount)
163
- : payload.amount,
164
- receiverMemo: payload.receiverMemo
165
- ? Number(payload.receiverMemo)
166
- : undefined,
167
- trustline: {
168
- address: payload.trustline.address,
169
- decimals: 10000000,
170
- },
171
- roles: payload.roles as any,
172
- milestones: payload.milestones,
173
- },
174
- };
175
-
176
- /**
177
- * Call the update escrow mutation
178
- *
179
- * @param payload - The final payload for the update escrow mutation
180
- * @param type - The type of the escrow
181
- * @param address - The address of the escrow
182
- */
183
- (await updateEscrow.mutateAsync({
184
- payload: finalPayload,
185
- type: "single-release",
186
- address: walletAddress || "",
187
- })) as UpdateSingleReleaseEscrowResponse;
188
-
189
- if (!selectedEscrow) return;
190
-
191
- const nextSelectedEscrow: GetEscrowsFromIndexerResponse = {
192
- ...selectedEscrow,
193
- ...finalPayload.escrow,
194
- trustline: {
195
- name:
196
- selectedEscrow.trustline?.name ||
197
- (selectedEscrow.trustline?.address as string) ||
198
- "",
199
- address: finalPayload.escrow.trustline.address,
200
- decimals: finalPayload.escrow.trustline.decimals,
201
- },
202
- };
203
-
204
- setSelectedEscrow(nextSelectedEscrow);
205
- toast.success("Escrow updated successfully");
206
- } catch (error) {
207
- toast.error(handleError(error as ErrorResponse).message);
208
- } finally {
209
- setIsSubmitting(false);
210
- }
211
- });
212
-
213
- return {
214
- form,
215
- isSubmitting,
216
- milestones,
217
- isAnyMilestoneEmpty,
218
- handleSubmit,
219
- handleAddMilestone,
220
- handleRemoveMilestone,
221
- handleAmountChange,
222
- handlePlatformFeeChange,
223
- };
224
- }
1
+ import * as React from "react";
2
+ import { useForm } from "react-hook-form";
3
+ import { zodResolver } from "@hookform/resolvers/zod";
4
+ import { useUpdateEscrowSchema } from "./schema";
5
+ import { z } from "zod";
6
+ import {
7
+ UpdateSingleReleaseEscrowPayload,
8
+ UpdateSingleReleaseEscrowResponse,
9
+ } from "@trustless-work/escrow/types";
10
+ import { toast } from "sonner";
11
+ import { useEscrowContext } from "@/components/tw-blocks/providers/EscrowProvider";
12
+ import { useWalletContext } from "@/components/tw-blocks/wallet-kit/WalletProvider";
13
+ import { useEscrowsMutations } from "@/components/tw-blocks/tanstack/useEscrowsMutations";
14
+ import {
15
+ ErrorResponse,
16
+ handleError,
17
+ } from "@/components/tw-blocks/handle-errors/handle";
18
+ import { GetEscrowsFromIndexerResponse } from "@trustless-work/escrow/types";
19
+
20
+ export function useUpdateEscrow() {
21
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
22
+
23
+ const { getSingleReleaseFormSchema } = useUpdateEscrowSchema();
24
+ const formSchema = getSingleReleaseFormSchema();
25
+
26
+ const { walletAddress } = useWalletContext();
27
+ const { selectedEscrow, setSelectedEscrow } = useEscrowContext();
28
+ const { updateEscrow } = useEscrowsMutations();
29
+
30
+ const form = useForm<z.infer<typeof formSchema>>({
31
+ resolver: zodResolver(formSchema),
32
+ defaultValues: {
33
+ engagementId: selectedEscrow?.engagementId || "",
34
+ title: selectedEscrow?.title || "",
35
+ description: selectedEscrow?.description || "",
36
+ platformFee: selectedEscrow?.platformFee as unknown as
37
+ | number
38
+ | string
39
+ | undefined,
40
+ amount: selectedEscrow?.amount as unknown as number | string | undefined,
41
+ receiverMemo: selectedEscrow?.receiverMemo
42
+ ? String(selectedEscrow.receiverMemo)
43
+ : "",
44
+ trustline: {
45
+ address: selectedEscrow?.trustline?.address || "",
46
+ decimals: 10000000,
47
+ },
48
+ roles: {
49
+ approver: selectedEscrow?.roles?.approver || "",
50
+ serviceProvider: selectedEscrow?.roles?.serviceProvider || "",
51
+ platformAddress: selectedEscrow?.roles?.platformAddress || "",
52
+ receiver: selectedEscrow?.roles?.receiver || "",
53
+ releaseSigner: selectedEscrow?.roles?.releaseSigner || "",
54
+ disputeResolver: selectedEscrow?.roles?.disputeResolver || "",
55
+ },
56
+ milestones: (selectedEscrow?.milestones || []).map((m: any) => ({
57
+ description: m?.description || "",
58
+ })),
59
+ },
60
+ mode: "onChange",
61
+ });
62
+
63
+ React.useEffect(() => {
64
+ if (!selectedEscrow) return;
65
+ form.reset({
66
+ engagementId: selectedEscrow?.engagementId || "",
67
+ title: selectedEscrow?.title || "",
68
+ description: selectedEscrow?.description || "",
69
+ platformFee:
70
+ (selectedEscrow?.platformFee as unknown as
71
+ | number
72
+ | string
73
+ | undefined) || "",
74
+ amount:
75
+ (selectedEscrow?.amount as unknown as number | string | undefined) ||
76
+ "",
77
+ receiverMemo: selectedEscrow?.receiverMemo
78
+ ? String(selectedEscrow.receiverMemo)
79
+ : "",
80
+ trustline: {
81
+ address: selectedEscrow?.trustline?.address || "",
82
+ decimals: 10000000,
83
+ },
84
+ roles: {
85
+ approver: selectedEscrow?.roles?.approver || "",
86
+ serviceProvider: selectedEscrow?.roles?.serviceProvider || "",
87
+ platformAddress: selectedEscrow?.roles?.platformAddress || "",
88
+ receiver: selectedEscrow?.roles?.receiver || "",
89
+ releaseSigner: selectedEscrow?.roles?.releaseSigner || "",
90
+ disputeResolver: selectedEscrow?.roles?.disputeResolver || "",
91
+ },
92
+ milestones: (selectedEscrow?.milestones || []).map((m: any) => ({
93
+ description: m?.description || "",
94
+ })),
95
+ });
96
+ }, [selectedEscrow, form]);
97
+
98
+ const milestones = form.watch("milestones");
99
+ const isAnyMilestoneEmpty = milestones.some((m) => m.description === "");
100
+
101
+ const handleAddMilestone = () => {
102
+ const current = form.getValues("milestones");
103
+ const updated = [...current, { description: "" }];
104
+ form.setValue("milestones", updated);
105
+ };
106
+
107
+ const handleRemoveMilestone = (index: number) => {
108
+ const current = form.getValues("milestones");
109
+ const updated = current.filter((_, i) => i !== index);
110
+ form.setValue("milestones", updated);
111
+ };
112
+
113
+ const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => {
114
+ let rawValue = e.target.value;
115
+ rawValue = rawValue.replace(/[^0-9.]/g, "");
116
+ if (rawValue.split(".").length > 2) rawValue = rawValue.slice(0, -1);
117
+ if (rawValue.includes(".")) {
118
+ const parts = rawValue.split(".");
119
+ if (parts[1] && parts[1].length > 2) {
120
+ rawValue = parts[0] + "." + parts[1].slice(0, 2);
121
+ }
122
+ }
123
+ form.setValue("amount", rawValue);
124
+ };
125
+
126
+ const handlePlatformFeeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
127
+ let rawValue = e.target.value;
128
+ rawValue = rawValue.replace(/[^0-9.]/g, "");
129
+ if (rawValue.split(".").length > 2) rawValue = rawValue.slice(0, -1);
130
+ if (rawValue.includes(".")) {
131
+ const parts = rawValue.split(".");
132
+ if (parts[1] && parts[1].length > 2) {
133
+ rawValue = parts[0] + "." + parts[1].slice(0, 2);
134
+ }
135
+ }
136
+ form.setValue("platformFee", rawValue);
137
+ };
138
+
139
+ const handleSubmit = form.handleSubmit(async (payload) => {
140
+ try {
141
+ setIsSubmitting(true);
142
+
143
+ /**
144
+ * Create the final payload for the update escrow mutation
145
+ *
146
+ * @param payload - The payload from the form
147
+ * @returns The final payload for the update escrow mutation
148
+ */
149
+ const finalPayload: UpdateSingleReleaseEscrowPayload = {
150
+ contractId: selectedEscrow?.contractId || "",
151
+ signer: walletAddress || "",
152
+ escrow: {
153
+ engagementId: payload.engagementId,
154
+ title: payload.title,
155
+ description: payload.description,
156
+ platformFee:
157
+ typeof payload.platformFee === "string"
158
+ ? Number(payload.platformFee)
159
+ : payload.platformFee,
160
+ amount:
161
+ typeof payload.amount === "string"
162
+ ? Number(payload.amount)
163
+ : payload.amount,
164
+ receiverMemo: payload.receiverMemo
165
+ ? Number(payload.receiverMemo)
166
+ : undefined,
167
+ trustline: {
168
+ address: payload.trustline.address,
169
+ decimals: 10000000,
170
+ },
171
+ roles: payload.roles as any,
172
+ milestones: payload.milestones,
173
+ },
174
+ };
175
+
176
+ /**
177
+ * Call the update escrow mutation
178
+ *
179
+ * @param payload - The final payload for the update escrow mutation
180
+ * @param type - The type of the escrow
181
+ * @param address - The address of the escrow
182
+ */
183
+ (await updateEscrow.mutateAsync({
184
+ payload: finalPayload,
185
+ type: "single-release",
186
+ address: walletAddress || "",
187
+ })) as UpdateSingleReleaseEscrowResponse;
188
+
189
+ if (!selectedEscrow) return;
190
+
191
+ const nextSelectedEscrow: GetEscrowsFromIndexerResponse = {
192
+ ...selectedEscrow,
193
+ ...finalPayload.escrow,
194
+ trustline: {
195
+ name:
196
+ selectedEscrow.trustline?.name ||
197
+ (selectedEscrow.trustline?.address as string) ||
198
+ "",
199
+ address: finalPayload.escrow.trustline.address,
200
+ decimals: finalPayload.escrow.trustline.decimals,
201
+ },
202
+ };
203
+
204
+ setSelectedEscrow(nextSelectedEscrow);
205
+ toast.success("Escrow updated successfully");
206
+ } catch (error) {
207
+ toast.error(handleError(error as ErrorResponse).message);
208
+ } finally {
209
+ setIsSubmitting(false);
210
+ }
211
+ });
212
+
213
+ return {
214
+ form,
215
+ isSubmitting,
216
+ milestones,
217
+ isAnyMilestoneEmpty,
218
+ handleSubmit,
219
+ handleAddMilestone,
220
+ handleRemoveMilestone,
221
+ handleAmountChange,
222
+ handlePlatformFeeChange,
223
+ };
224
+ }
@@ -36,7 +36,9 @@ export function ReactQueryClientProvider({
36
36
  return (
37
37
  <QueryClientProvider client={queryClient}>
38
38
  {children}
39
- <ReactQueryDevtools initialIsOpen={false} />
39
+ {process.env.NODE_ENV !== "production" ? (
40
+ <ReactQueryDevtools initialIsOpen={false} />
41
+ ) : null}
40
42
  </QueryClientProvider>
41
43
  );
42
44
  }