|
| 1 | +import { Form } from "@remix-run/react"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { Button } from "~/components/primitives/Buttons"; |
| 4 | +import { FormError } from "~/components/primitives/FormError"; |
| 5 | +import { Header2 } from "~/components/primitives/Headers"; |
| 6 | +import { Hint } from "~/components/primitives/Hint"; |
| 7 | +import { Input } from "~/components/primitives/Input"; |
| 8 | +import { Label } from "~/components/primitives/Label"; |
| 9 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 10 | +import * as Property from "~/components/primitives/PropertyTable"; |
| 11 | +import type { WebhookLimits, WebhookLimitsConfig } from "~/v3/webhookLimits"; |
| 12 | + |
| 13 | +export const WEBHOOK_LIMITS_INTENT = "set-webhook-limits"; |
| 14 | +export const WEBHOOK_LIMITS_SAVED_VALUE = "webhook-limits"; |
| 15 | + |
| 16 | +type FieldErrors = Record<string, string[] | undefined> | null; |
| 17 | + |
| 18 | +const FIELDS: Array<{ key: keyof WebhookLimits; label: string; hint: string }> = [ |
| 19 | + { |
| 20 | + key: "maxWaitersPerEnvironment", |
| 21 | + label: "Waiters per environment", |
| 22 | + hint: "Live webhook waiters one environment can hold across all its endpoints.", |
| 23 | + }, |
| 24 | + { |
| 25 | + key: "maxWaitersPerEndpoint", |
| 26 | + label: "Waiters per endpoint", |
| 27 | + hint: "Live waiters on one endpoint, which is also the most one delivery can resume.", |
| 28 | + }, |
| 29 | + { |
| 30 | + key: "concurrency", |
| 31 | + label: "Processing concurrency", |
| 32 | + hint: "Webhook jobs one environment can have in flight at once.", |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +type Props = { |
| 37 | + limits: WebhookLimits; |
| 38 | + overrides: WebhookLimitsConfig; |
| 39 | + errors: FieldErrors; |
| 40 | + savedJustNow: boolean; |
| 41 | + isSubmitting: boolean; |
| 42 | +}; |
| 43 | + |
| 44 | +export function WebhookLimitsSection({ |
| 45 | + limits, |
| 46 | + overrides, |
| 47 | + errors, |
| 48 | + savedJustNow, |
| 49 | + isSubmitting, |
| 50 | +}: Props) { |
| 51 | + const hasFieldErrors = !!errors && Object.keys(errors).length > 0; |
| 52 | + const fieldError = (field: string) => |
| 53 | + errors && field in errors ? errors[field]?.[0] : undefined; |
| 54 | + |
| 55 | + const [isEditing, setIsEditing] = useState(hasFieldErrors); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + // oxlint-disable-next-line react/set-state-in-effect -- This effect intentionally synchronizes local state after an external or lifecycle change. |
| 59 | + if (hasFieldErrors) setIsEditing(true); |
| 60 | + }, [hasFieldErrors]); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + // oxlint-disable-next-line react/set-state-in-effect -- This effect intentionally synchronizes local state after an external or lifecycle change. |
| 64 | + if (savedJustNow && !hasFieldErrors) setIsEditing(false); |
| 65 | + }, [savedJustNow, hasFieldErrors]); |
| 66 | + |
| 67 | + return ( |
| 68 | + <section className="flex flex-col gap-3 rounded-md border border-grid-bright bg-background-bright p-4"> |
| 69 | + <div className="flex items-center justify-between"> |
| 70 | + <Header2>Webhook limits</Header2> |
| 71 | + {!isEditing && ( |
| 72 | + <Button |
| 73 | + variant="tertiary/small" |
| 74 | + onClick={() => setIsEditing(true)} |
| 75 | + disabled={isSubmitting} |
| 76 | + > |
| 77 | + Edit |
| 78 | + </Button> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + <Paragraph variant="small" className="text-text-dimmed"> |
| 82 | + Set by the org's plan. A blank field uses the system default. |
| 83 | + </Paragraph> |
| 84 | + |
| 85 | + {savedJustNow && ( |
| 86 | + <div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2"> |
| 87 | + <Paragraph variant="small" className="text-green-500"> |
| 88 | + Saved. |
| 89 | + </Paragraph> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + {!isEditing ? ( |
| 94 | + <Property.Table> |
| 95 | + {FIELDS.map((field) => ( |
| 96 | + <Property.Item key={field.key}> |
| 97 | + <Property.Label>{field.label}</Property.Label> |
| 98 | + <Property.Value> |
| 99 | + {limits[field.key].toLocaleString()} |
| 100 | + <span className="text-text-dimmed"> |
| 101 | + {overrides[field.key] !== undefined ? " (org)" : " (default)"} |
| 102 | + </span> |
| 103 | + </Property.Value> |
| 104 | + </Property.Item> |
| 105 | + ))} |
| 106 | + </Property.Table> |
| 107 | + ) : ( |
| 108 | + <Form method="post" className="flex flex-col gap-3 pt-2"> |
| 109 | + <input type="hidden" name="intent" value={WEBHOOK_LIMITS_INTENT} /> |
| 110 | + {FIELDS.map((field) => ( |
| 111 | + <div key={field.key} className="flex flex-col gap-1"> |
| 112 | + <Label>{field.label}</Label> |
| 113 | + <Input |
| 114 | + name={field.key} |
| 115 | + type="number" |
| 116 | + min={1} |
| 117 | + defaultValue={overrides[field.key]?.toString() ?? ""} |
| 118 | + placeholder={`Default: ${limits[field.key].toLocaleString()}`} |
| 119 | + /> |
| 120 | + <Hint>{field.hint}</Hint> |
| 121 | + <FormError>{fieldError(field.key)}</FormError> |
| 122 | + </div> |
| 123 | + ))} |
| 124 | + <div className="flex items-center gap-2"> |
| 125 | + <Button type="submit" variant="primary/medium" disabled={isSubmitting}> |
| 126 | + Save |
| 127 | + </Button> |
| 128 | + <Button |
| 129 | + type="button" |
| 130 | + variant="tertiary/medium" |
| 131 | + onClick={() => setIsEditing(false)} |
| 132 | + disabled={isSubmitting} |
| 133 | + > |
| 134 | + Cancel |
| 135 | + </Button> |
| 136 | + </div> |
| 137 | + </Form> |
| 138 | + )} |
| 139 | + </section> |
| 140 | + ); |
| 141 | +} |
0 commit comments