@trustless-work/blocks 1.1.4 → 1.1.6

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.
@@ -0,0 +1,66 @@
1
+ "use client";
2
+
3
+ import { Button } from "__UI_BASE__/button";
4
+ import { Input } from "__UI_BASE__/input";
5
+ import {
6
+ Form,
7
+ FormField,
8
+ FormItem,
9
+ FormLabel,
10
+ FormControl,
11
+ FormMessage,
12
+ } from "__UI_BASE__/form";
13
+ import {
14
+ Dialog,
15
+ DialogContent,
16
+ DialogHeader,
17
+ DialogTitle,
18
+ DialogTrigger,
19
+ } from "__UI_BASE__/dialog";
20
+ import { useLoadEscrow } from "./useLoadEscrow";
21
+ import * as React from "react";
22
+
23
+ export function LoadEscrowDialog() {
24
+ const [isOpen, setIsOpen] = React.useState(false);
25
+ const { form, isSubmitting, onSubmit } = useLoadEscrow();
26
+
27
+ return (
28
+ <Dialog open={isOpen} onOpenChange={setIsOpen}>
29
+ <DialogTrigger asChild>
30
+ <Button type="button" className="cursor-pointer w-full">
31
+ Load Escrow
32
+ </Button>
33
+ </DialogTrigger>
34
+ <DialogContent className="!w-full sm:!max-w-xl max-h-[95vh] overflow-y-auto">
35
+ <DialogHeader>
36
+ <DialogTitle>Load Escrow</DialogTitle>
37
+ </DialogHeader>
38
+ <Form {...form}>
39
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
40
+ <FormField
41
+ control={form.control}
42
+ name="contractIds.0.value"
43
+ render={({ field }) => (
44
+ <FormItem>
45
+ <FormLabel>Contract / Escrow ID</FormLabel>
46
+ <FormControl>
47
+ <Input placeholder="CAZ6UQX7..." {...field} />
48
+ </FormControl>
49
+ <FormMessage />
50
+ </FormItem>
51
+ )}
52
+ />
53
+
54
+ <Button
55
+ type="submit"
56
+ className="w-full cursor-pointer"
57
+ disabled={isSubmitting}
58
+ >
59
+ {isSubmitting ? "Loading..." : "Load Escrow"}
60
+ </Button>
61
+ </form>
62
+ </Form>
63
+ </DialogContent>
64
+ </Dialog>
65
+ );
66
+ }
@@ -0,0 +1,48 @@
1
+ "use client";
2
+
3
+ import { Button } from "__UI_BASE__/button";
4
+ import { Input } from "__UI_BASE__/input";
5
+ import {
6
+ Form,
7
+ FormField,
8
+ FormItem,
9
+ FormLabel,
10
+ FormControl,
11
+ FormMessage,
12
+ } from "__UI_BASE__/form";
13
+ import { useLoadEscrow } from "./useLoadEscrow";
14
+
15
+ export function LoadEscrowForm() {
16
+ const { form, isSubmitting, onSubmit } = useLoadEscrow();
17
+
18
+ return (
19
+ <Form {...form}>
20
+ <form
21
+ onSubmit={form.handleSubmit(onSubmit)}
22
+ className="flex flex-col space-y-6 w-full"
23
+ >
24
+ <FormField
25
+ control={form.control}
26
+ name="contractIds.0.value"
27
+ render={({ field }) => (
28
+ <FormItem>
29
+ <FormLabel>Contract / Escrow ID</FormLabel>
30
+ <FormControl>
31
+ <Input placeholder="CAZ6UQX7..." {...field} />
32
+ </FormControl>
33
+ <FormMessage />
34
+ </FormItem>
35
+ )}
36
+ />
37
+
38
+ <Button
39
+ type="submit"
40
+ className="w-full cursor-pointer"
41
+ disabled={isSubmitting}
42
+ >
43
+ {isSubmitting ? "Loading..." : "Load Escrow"}
44
+ </Button>
45
+ </form>
46
+ </Form>
47
+ );
48
+ }
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+
3
+ const STELLAR_CONTRACT_ID_REGEX = /^C[A-Z2-7]{55}$/;
4
+
5
+ export const formSchema = z.object({
6
+ contractIds: z
7
+ .array(
8
+ z.object({
9
+ value: z
10
+ .string()
11
+ .min(1, "Contract ID is required")
12
+ .regex(STELLAR_CONTRACT_ID_REGEX, "Invalid Stellar Contract ID"),
13
+ })
14
+ )
15
+ .min(1, "At least one contract ID is required"),
16
+ validateOnChain: z.boolean(),
17
+ });
@@ -0,0 +1,58 @@
1
+ import { useState } from "react";
2
+ import { useForm } from "react-hook-form";
3
+ import { zodResolver } from "@hookform/resolvers/zod";
4
+ import { z } from "zod";
5
+ import { toast } from "sonner";
6
+ import {
7
+ ErrorResponse,
8
+ handleError,
9
+ } from "@/components/tw-blocks/handle-errors/handle";
10
+ import { useGetEscrowFromIndexerByContractIds } from "@trustless-work/escrow/hooks";
11
+ import { GetEscrowsFromIndexerResponse } from "@trustless-work/escrow/types";
12
+ import { useEscrowContext } from "@/components/tw-blocks/providers/EscrowProvider";
13
+ import { formSchema } from "./schema";
14
+
15
+ export const useLoadEscrow = () => {
16
+ const { setSelectedEscrow } = useEscrowContext();
17
+ const [isSubmitting, setIsSubmitting] = useState(false);
18
+ const { getEscrowByContractIds } = useGetEscrowFromIndexerByContractIds();
19
+
20
+ const form = useForm<z.infer<typeof formSchema>>({
21
+ resolver: zodResolver(formSchema),
22
+ defaultValues: {
23
+ contractIds: [{ value: "" }],
24
+ validateOnChain: true,
25
+ },
26
+ mode: "onChange",
27
+ });
28
+
29
+ const onSubmit = async (payload: z.infer<typeof formSchema>) => {
30
+ setIsSubmitting(true);
31
+
32
+ try {
33
+ const data = (await getEscrowByContractIds({
34
+ contractIds: payload.contractIds.map((item) => item.value),
35
+ validateOnChain: true,
36
+ })) as unknown as GetEscrowsFromIndexerResponse[];
37
+
38
+ const escrowData = data[0];
39
+
40
+ if (!escrowData) {
41
+ throw new Error("No escrow data received");
42
+ }
43
+
44
+ setSelectedEscrow(escrowData);
45
+
46
+ toast.success(
47
+ "Escrow data fetched successfully. Now you can use the selectedEscrow state"
48
+ );
49
+ } catch (error) {
50
+ toast.error(handleError(error as ErrorResponse).message);
51
+ } finally {
52
+ setIsSubmitting(false);
53
+ form.reset();
54
+ }
55
+ };
56
+
57
+ return { form, isSubmitting, onSubmit };
58
+ };
@@ -0,0 +1,50 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import {
3
+ GetEscrowFromIndexerByContractIdsParams,
4
+ useGetEscrowFromIndexerByContractIds,
5
+ } from "@trustless-work/escrow";
6
+ import { GetEscrowsFromIndexerResponse as Escrow } from "@trustless-work/escrow/types";
7
+
8
+ /**
9
+ * Use the query to get the escrows by contract ids
10
+ *
11
+ * @param params - The parameters for the query
12
+ * @returns The query result
13
+ */
14
+ export const useEscrowsByContractIdsQuery = ({
15
+ contractIds,
16
+ validateOnChain = true,
17
+ }: GetEscrowFromIndexerByContractIdsParams) => {
18
+ // Get the escrow by contract ids
19
+ const { getEscrowByContractIds } = useGetEscrowFromIndexerByContractIds();
20
+
21
+ return useQuery({
22
+ queryKey: ["escrow", contractIds, validateOnChain],
23
+ queryFn: async (): Promise<Escrow[]> => {
24
+ if (!contractIds) {
25
+ throw new Error(
26
+ "Contract IDs are required to fetch escrows by contract IDs"
27
+ );
28
+ }
29
+
30
+ /**
31
+ * Call the query to get escrows by contract ids from the Trustless Work Indexer
32
+ *
33
+ * @param params - The parameters for the query
34
+ * @returns The query result
35
+ */
36
+ const escrows = await getEscrowByContractIds({
37
+ contractIds,
38
+ validateOnChain,
39
+ });
40
+
41
+ if (!escrows) {
42
+ throw new Error("Failed to fetch escrows");
43
+ }
44
+
45
+ return escrows;
46
+ },
47
+ enabled: !!contractIds,
48
+ staleTime: 1000 * 60 * 5,
49
+ });
50
+ };