|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMemo, useState } from "react"; |
| 4 | + |
| 5 | +import { Loader2 } from "lucide-react"; |
| 6 | +import z from "zod"; |
| 7 | + |
| 8 | +import { Card, CardContent } from "@/components/ui/card"; |
| 9 | +import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"; |
| 10 | +import { |
| 11 | + InputGroup, |
| 12 | + InputGroupAddon, |
| 13 | + InputGroupInput, |
| 14 | +} from "@/components/ui/input-group"; |
| 15 | +import { useDebounce } from "@/hooks/use-debounce"; |
| 16 | +import { api } from "@/trpc/client"; |
| 17 | + |
| 18 | +function normalizeInput(value: string): string { |
| 19 | + const trimmed = value.trim(); |
| 20 | + if (!trimmed || trimmed.includes("://")) return trimmed; |
| 21 | + return `https://${trimmed}`; |
| 22 | +} |
| 23 | + |
| 24 | +function getPath(resourceUrl: string): string { |
| 25 | + try { |
| 26 | + const url = new URL(resourceUrl); |
| 27 | + return decodeURIComponent(`${url.pathname}${url.search}`); |
| 28 | + } catch { |
| 29 | + return resourceUrl; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export function TryDiscovery() { |
| 34 | + const [url, setUrl] = useState(""); |
| 35 | + const debouncedUrl = useDebounce(url, 500); |
| 36 | + const normalizedUrl = useMemo( |
| 37 | + () => normalizeInput(debouncedUrl), |
| 38 | + [debouncedUrl] |
| 39 | + ); |
| 40 | + const isValidUrl = z.url().safeParse(normalizedUrl).success; |
| 41 | + const origin = isValidUrl ? new URL(normalizedUrl).origin : null; |
| 42 | + |
| 43 | + const discovery = api.public.resources.checkDiscovery.useQuery( |
| 44 | + { origin: origin!, bustCache: false }, |
| 45 | + { |
| 46 | + enabled: origin !== null && debouncedUrl === url, |
| 47 | + retry: false, |
| 48 | + staleTime: 30_000, |
| 49 | + } |
| 50 | + ); |
| 51 | + |
| 52 | + const resources = discovery.data?.found ? discovery.data.resources : []; |
| 53 | + const isLoading = |
| 54 | + isValidUrl && debouncedUrl === url && discovery.isPending && url.length > 0; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="flex flex-col gap-4" data-not-typeset> |
| 58 | + <Card> |
| 59 | + <CardContent> |
| 60 | + <Field> |
| 61 | + <FieldLabel htmlFor="discovery-url"> |
| 62 | + Origin or endpoint URL |
| 63 | + </FieldLabel> |
| 64 | + <InputGroup> |
| 65 | + <InputGroupInput |
| 66 | + id="discovery-url" |
| 67 | + type="text" |
| 68 | + placeholder="https://yourdomain.com" |
| 69 | + value={url} |
| 70 | + onChange={(event) => { |
| 71 | + setUrl(event.target.value); |
| 72 | + }} |
| 73 | + /> |
| 74 | + {isLoading ? ( |
| 75 | + <InputGroupAddon align="inline-end"> |
| 76 | + <Loader2 className="size-4 animate-spin text-muted-foreground" /> |
| 77 | + </InputGroupAddon> |
| 78 | + ) : null} |
| 79 | + </InputGroup> |
| 80 | + <FieldDescription> |
| 81 | + Runs discovery against the origin and lists the routes x402scan |
| 82 | + resolves. Nothing is registered. |
| 83 | + </FieldDescription> |
| 84 | + </Field> |
| 85 | + </CardContent> |
| 86 | + </Card> |
| 87 | + |
| 88 | + {isValidUrl && discovery.isSuccess && resources.length > 0 ? ( |
| 89 | + <Card> |
| 90 | + <CardContent> |
| 91 | + <p className="mb-2 type-label"> |
| 92 | + Found {resources.length} endpoint |
| 93 | + {resources.length === 1 ? "" : "s"} |
| 94 | + </p> |
| 95 | + <ul className="space-y-1 text-muted-foreground"> |
| 96 | + {resources.slice(0, 10).map((resource) => ( |
| 97 | + <li key={`${resource.method ?? "GET"}-${resource.url}`}> |
| 98 | + <code className="type-compact-code"> |
| 99 | + <strong className="text-foreground"> |
| 100 | + {resource.method ?? "GET"} |
| 101 | + </strong>{" "} |
| 102 | + {getPath(resource.url)} |
| 103 | + </code> |
| 104 | + </li> |
| 105 | + ))} |
| 106 | + {resources.length > 10 ? ( |
| 107 | + <li className="text-muted-foreground/60"> |
| 108 | + ...and {resources.length - 10} more |
| 109 | + </li> |
| 110 | + ) : null} |
| 111 | + </ul> |
| 112 | + </CardContent> |
| 113 | + </Card> |
| 114 | + ) : null} |
| 115 | + |
| 116 | + {isValidUrl && |
| 117 | + discovery.isSuccess && |
| 118 | + (!discovery.data?.found || resources.length === 0) ? ( |
| 119 | + <p className="type-supporting-body text-muted-foreground"> |
| 120 | + {discovery.data?.found |
| 121 | + ? "No discoverable endpoints found at this origin." |
| 122 | + : (discovery.data?.error ?? |
| 123 | + "No discoverable endpoints found at this origin.")} |
| 124 | + </p> |
| 125 | + ) : null} |
| 126 | + |
| 127 | + {isValidUrl && discovery.isError ? ( |
| 128 | + <p className="type-supporting-body text-destructive"> |
| 129 | + Discovery failed. Check the URL and try again. |
| 130 | + </p> |
| 131 | + ) : null} |
| 132 | + </div> |
| 133 | + ); |
| 134 | +} |
0 commit comments