|
| 1 | +import { Form } from "@remix-run/react"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { Button } from "~/components/primitives/Buttons"; |
| 4 | +import { Checkbox } from "~/components/primitives/Checkbox"; |
| 5 | +import { FormError } from "~/components/primitives/FormError"; |
| 6 | +import { Header2 } from "~/components/primitives/Headers"; |
| 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 | + |
| 12 | +export const CONCURRENCY_QUOTA_INTENT = "set-concurrency-quota"; |
| 13 | +export const CONCURRENCY_QUOTA_SAVED_VALUE = "concurrency-quota"; |
| 14 | + |
| 15 | +type FieldErrors = Record<string, string[] | undefined> | null; |
| 16 | + |
| 17 | +type Props = { |
| 18 | + currentQuota: number; |
| 19 | + purchased: number; |
| 20 | + errors: FieldErrors; |
| 21 | + formError: string | null; |
| 22 | + savedJustNow: boolean; |
| 23 | + isSubmitting: boolean; |
| 24 | +}; |
| 25 | + |
| 26 | +export function ConcurrencyQuotaSection({ |
| 27 | + currentQuota, |
| 28 | + purchased, |
| 29 | + errors, |
| 30 | + formError, |
| 31 | + savedJustNow, |
| 32 | + isSubmitting, |
| 33 | +}: Props) { |
| 34 | + const hasFieldErrors = !!errors && Object.keys(errors).length > 0; |
| 35 | + const fieldError = (field: string) => |
| 36 | + errors && field in errors ? errors[field]?.[0] : undefined; |
| 37 | + |
| 38 | + const [isEditing, setIsEditing] = useState(hasFieldErrors || !!formError); |
| 39 | + const [usePlanDefault, setUsePlanDefault] = useState(false); |
| 40 | + const [value, setValue] = useState(String(currentQuota)); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (hasFieldErrors || formError) setIsEditing(true); |
| 44 | + }, [hasFieldErrors, formError]); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (savedJustNow && !hasFieldErrors && !formError) setIsEditing(false); |
| 48 | + }, [savedJustNow, hasFieldErrors, formError]); |
| 49 | + |
| 50 | + const cancelEdit = () => { |
| 51 | + setValue(String(currentQuota)); |
| 52 | + setUsePlanDefault(false); |
| 53 | + setIsEditing(false); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <section className="flex flex-col gap-3 rounded-md border border-charcoal-700 bg-charcoal-800 p-4"> |
| 58 | + <div className="flex items-center justify-between"> |
| 59 | + <Header2>Concurrency quota</Header2> |
| 60 | + {!isEditing && ( |
| 61 | + <Button |
| 62 | + variant="tertiary/small" |
| 63 | + onClick={() => setIsEditing(true)} |
| 64 | + disabled={isSubmitting} |
| 65 | + > |
| 66 | + Edit |
| 67 | + </Button> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + |
| 71 | + <Paragraph variant="small"> |
| 72 | + Cap on how much extra concurrency this org can purchase. Increases |
| 73 | + unlock self-serve purchase up to the new cap; the org still has to |
| 74 | + complete the purchase from the billing flow. |
| 75 | + </Paragraph> |
| 76 | + |
| 77 | + {savedJustNow && ( |
| 78 | + <div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2"> |
| 79 | + <Paragraph variant="small" className="text-green-500"> |
| 80 | + Saved. |
| 81 | + </Paragraph> |
| 82 | + </div> |
| 83 | + )} |
| 84 | + |
| 85 | + {formError && ( |
| 86 | + <div className="rounded-md border border-red-600/40 bg-red-600/10 px-3 py-2"> |
| 87 | + <Paragraph variant="small" className="text-red-500"> |
| 88 | + {formError} |
| 89 | + </Paragraph> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + {!isEditing ? ( |
| 94 | + <Property.Table> |
| 95 | + <Property.Item> |
| 96 | + <Property.Label>Max extra concurrency this org can purchase on top of their plan</Property.Label> |
| 97 | + <Property.Value>{currentQuota.toLocaleString()}</Property.Value> |
| 98 | + </Property.Item> |
| 99 | + <Property.Item> |
| 100 | + <Property.Label>Already purchased</Property.Label> |
| 101 | + <Property.Value>{purchased.toLocaleString()}</Property.Value> |
| 102 | + </Property.Item> |
| 103 | + </Property.Table> |
| 104 | + ) : ( |
| 105 | + <Form method="post" className="flex flex-col gap-3 pt-2"> |
| 106 | + <input type="hidden" name="intent" value={CONCURRENCY_QUOTA_INTENT} /> |
| 107 | + |
| 108 | + <div className="flex flex-col gap-1"> |
| 109 | + <Label>Max extra concurrency this org can purchase on top of their plan</Label> |
| 110 | + <Input |
| 111 | + name="extraConcurrencyQuota" |
| 112 | + type="number" |
| 113 | + min={0} |
| 114 | + value={usePlanDefault ? "" : value} |
| 115 | + onChange={(e) => setValue(e.target.value)} |
| 116 | + disabled={usePlanDefault} |
| 117 | + required={!usePlanDefault} |
| 118 | + /> |
| 119 | + <FormError>{fieldError("extraConcurrencyQuota")}</FormError> |
| 120 | + </div> |
| 121 | + |
| 122 | + <div className="flex items-center gap-2"> |
| 123 | + <Checkbox |
| 124 | + id="usePlanDefault" |
| 125 | + name="usePlanDefault" |
| 126 | + value="true" |
| 127 | + checked={usePlanDefault} |
| 128 | + onChange={(e) => setUsePlanDefault(e.target.checked)} |
| 129 | + /> |
| 130 | + <Label htmlFor="usePlanDefault"> |
| 131 | + Use plan default (clears any per-org override) |
| 132 | + </Label> |
| 133 | + </div> |
| 134 | + |
| 135 | + <div className="flex items-center gap-2"> |
| 136 | + <Button |
| 137 | + type="submit" |
| 138 | + variant="primary/medium" |
| 139 | + disabled={isSubmitting || (!usePlanDefault && !value.trim())} |
| 140 | + > |
| 141 | + Save |
| 142 | + </Button> |
| 143 | + <Button |
| 144 | + type="button" |
| 145 | + variant="tertiary/medium" |
| 146 | + onClick={cancelEdit} |
| 147 | + disabled={isSubmitting} |
| 148 | + > |
| 149 | + Cancel |
| 150 | + </Button> |
| 151 | + </div> |
| 152 | + </Form> |
| 153 | + )} |
| 154 | + </section> |
| 155 | + ); |
| 156 | +} |
0 commit comments