|
| 1 | +import { type WebhookDeliveryStatus } from "@trigger.dev/database"; |
| 2 | +import { useEffect } from "react"; |
| 3 | +import { useTypedFetcher } from "remix-typedjson"; |
| 4 | +import { DateTime } from "~/components/primitives/DateTime"; |
| 5 | +import { Spinner } from "~/components/primitives/Spinner"; |
| 6 | +import { DeliveryStatusBadge } from "~/components/webhookDeliveries/v1/DeliveryStatus"; |
| 7 | +import { type loader as replaySourceLoader } from "~/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.webhooks.endpoints.$endpointParam.replay-source"; |
| 8 | + |
| 9 | +export function ReplaySourcePicker({ |
| 10 | + replaySourcePath, |
| 11 | + onLoad, |
| 12 | +}: { |
| 13 | + replaySourcePath: string; |
| 14 | + onLoad: (body: string, headers: Record<string, string>) => void; |
| 15 | +}) { |
| 16 | + const listFetcher = useTypedFetcher<typeof replaySourceLoader>(); |
| 17 | + const payloadFetcher = useTypedFetcher<typeof replaySourceLoader>(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (listFetcher.state === "idle" && listFetcher.data === undefined) { |
| 21 | + listFetcher.load(replaySourcePath); |
| 22 | + } |
| 23 | + }, [listFetcher, replaySourcePath]); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const data = payloadFetcher.data; |
| 27 | + if (data?.kind === "payload") { |
| 28 | + onLoad(data.body, data.headers); |
| 29 | + } |
| 30 | + }, [payloadFetcher.data, onLoad]); |
| 31 | + |
| 32 | + const listLoading = |
| 33 | + listFetcher.state === "loading" || |
| 34 | + (listFetcher.data === undefined && listFetcher.state !== "idle"); |
| 35 | + const list = listFetcher.data?.kind === "list" ? listFetcher.data.deliveries : []; |
| 36 | + const loadingDeliveryId = |
| 37 | + payloadFetcher.state !== "idle" |
| 38 | + ? (new URLSearchParams(payloadFetcher.formAction?.split("?")[1] ?? "").get("deliveryId") ?? |
| 39 | + undefined) |
| 40 | + : undefined; |
| 41 | + |
| 42 | + function selectDelivery(friendlyId: string) { |
| 43 | + payloadFetcher.load(`${replaySourcePath}?deliveryId=${encodeURIComponent(friendlyId)}`); |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="flex h-full flex-col overflow-hidden"> |
| 48 | + <div className="flex items-center gap-2 border-b border-grid-dimmed px-3 py-2"> |
| 49 | + <span className="text-xs font-medium text-text-dimmed"> |
| 50 | + Load a past delivery's payload into the composer |
| 51 | + </span> |
| 52 | + {listLoading ? <Spinner className="size-3.5" /> : null} |
| 53 | + </div> |
| 54 | + <div className="flex-1 overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600"> |
| 55 | + {!listLoading && list.length === 0 ? ( |
| 56 | + <p className="px-3 py-8 text-center text-sm text-text-dimmed"> |
| 57 | + No past deliveries to replay yet. |
| 58 | + </p> |
| 59 | + ) : ( |
| 60 | + list.map((delivery) => ( |
| 61 | + <button |
| 62 | + key={delivery.friendlyId} |
| 63 | + type="button" |
| 64 | + onClick={() => selectDelivery(delivery.friendlyId)} |
| 65 | + disabled={payloadFetcher.state !== "idle"} |
| 66 | + className="flex w-full items-center justify-between gap-2 border-b border-grid-dimmed px-3 py-2 text-left hover:bg-charcoal-800 disabled:opacity-60" |
| 67 | + > |
| 68 | + <span className="flex min-w-0 items-center gap-1.5"> |
| 69 | + <span className="truncate font-mono text-xs text-text-bright"> |
| 70 | + {delivery.friendlyId} |
| 71 | + </span> |
| 72 | + {delivery.isTest ? ( |
| 73 | + <span className="shrink-0 rounded-sm bg-charcoal-700 px-1 py-0.5 text-xxs font-semibold uppercase tracking-wide text-text-dimmed"> |
| 74 | + Test |
| 75 | + </span> |
| 76 | + ) : null} |
| 77 | + </span> |
| 78 | + <span className="flex shrink-0 items-center gap-2"> |
| 79 | + <span className="text-xxs text-text-dimmed"> |
| 80 | + <DateTime date={new Date(delivery.createdAt)} /> |
| 81 | + </span> |
| 82 | + {loadingDeliveryId === delivery.friendlyId ? ( |
| 83 | + <Spinner className="size-3.5" /> |
| 84 | + ) : ( |
| 85 | + <DeliveryStatusBadge status={delivery.status as WebhookDeliveryStatus} /> |
| 86 | + )} |
| 87 | + </span> |
| 88 | + </button> |
| 89 | + )) |
| 90 | + )} |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments