From ae140b7bc0c7685f7f7d118d2c3e7323b42ffc11 Mon Sep 17 00:00:00 2001 From: Catalin Pit Date: Wed, 24 Jun 2026 16:16:41 +0300 Subject: [PATCH] refactor: improve quota usage logic and streamline rate limit input handling --- .../general/organisation-usage-panel.tsx | 17 ++++++++++++----- .../general/rate-limit-array-input.tsx | 14 +++++++------- .../rate-limit/compute-quota-flags.ts | 4 +--- .../rate-limit/get-quota-alert-kind.ts | 4 +--- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/apps/remix/app/components/general/organisation-usage-panel.tsx b/apps/remix/app/components/general/organisation-usage-panel.tsx index 636839e51..99a69a661 100644 --- a/apps/remix/app/components/general/organisation-usage-panel.tsx +++ b/apps/remix/app/components/general/organisation-usage-panel.tsx @@ -84,12 +84,9 @@ const getUsageCardState = ({ used, limit, footnote }: UsageCardStateOptions): Us }; } - if (isQuotaExceeded(limit, used)) { + if (used > limit) { return { - status: { - label: used > limit ? Exceeded : Limit reached, - variant: 'destructive', - }, + status: { label: Exceeded, variant: 'destructive' }, percent, hasFiniteLimit, progressClassName: '[&>div]:bg-destructive', @@ -97,6 +94,16 @@ const getUsageCardState = ({ used, limit, footnote }: UsageCardStateOptions): Us }; } + if (isQuotaExceeded(limit, used)) { + return { + status: { label: Limit reached, variant: 'orange' }, + percent, + hasFiniteLimit, + progressClassName: '[&>div]:bg-orange-500 dark:[&>div]:bg-orange-400', + subtext: footnote ?? null, + }; + } + if (isQuotaNearing(limit, used)) { return { status: { label: Near limit, variant: 'warning' }, diff --git a/apps/remix/app/components/general/rate-limit-array-input.tsx b/apps/remix/app/components/general/rate-limit-array-input.tsx index a631b7b01..a0197764f 100644 --- a/apps/remix/app/components/general/rate-limit-array-input.tsx +++ b/apps/remix/app/components/general/rate-limit-array-input.tsx @@ -15,11 +15,12 @@ type RateLimitArrayInputProps = { const EMPTY_ENTRY: RateLimitEntryValue = { window: '', max: 0 }; +/** A row counts as "started" once either field has input; fully-empty rows are dropped on commit. */ +const hasEntryInput = (entry: RateLimitEntryValue) => entry.window.trim() !== '' || entry.max > 0; + /** Keep in-progress rows; drop rows that are completely empty. */ const persistEntries = (entries: RateLimitEntryValue[]) => { - return entries - .map((entry) => ({ ...entry, window: entry.window.trim() })) - .filter((entry) => entry.window !== '' || entry.max > 0); + return entries.map((entry) => ({ ...entry, window: entry.window.trim() })).filter(hasEntryInput); }; export const RateLimitArrayInput = ({ value, onChange, disabled }: RateLimitArrayInputProps) => { @@ -31,7 +32,7 @@ export const RateLimitArrayInput = ({ value, onChange, disabled }: RateLimitArra const getWindowError = (entry: RateLimitEntryValue, index: number) => { const window = entry.window.trim(); - if (window === '' && entry.max <= 0) { + if (!hasEntryInput(entry)) { return null; } @@ -51,7 +52,7 @@ export const RateLimitArrayInput = ({ value, onChange, disabled }: RateLimitArra }; const getMaxError = (entry: RateLimitEntryValue) => { - if (entry.window.trim() === '' && entry.max <= 0) { + if (!hasEntryInput(entry)) { return null; } @@ -61,9 +62,8 @@ export const RateLimitArrayInput = ({ value, onChange, disabled }: RateLimitArra const updateEntry = (index: number, patch: Partial) => { if (index >= value.length) { const nextDraftEntry = { ...(draftEntry ?? EMPTY_ENTRY), ...patch }; - const shouldPersistDraft = nextDraftEntry.window.trim() !== '' || nextDraftEntry.max > 0; - if (shouldPersistDraft) { + if (hasEntryInput(nextDraftEntry)) { onChange(persistEntries([...value, nextDraftEntry])); setDraftEntry(null); return; diff --git a/packages/lib/server-only/rate-limit/compute-quota-flags.ts b/packages/lib/server-only/rate-limit/compute-quota-flags.ts index 9e1dfc073..556955942 100644 --- a/packages/lib/server-only/rate-limit/compute-quota-flags.ts +++ b/packages/lib/server-only/rate-limit/compute-quota-flags.ts @@ -1,6 +1,4 @@ -import { isQuotaExceeded, isQuotaNearing, QUOTA_WARNING_THRESHOLD } from '../../universal/quota-usage'; - -export { QUOTA_WARNING_THRESHOLD }; +import { isQuotaExceeded, isQuotaNearing } from '../../universal/quota-usage'; export type QuotaFlags = { isDocumentQuotaExceeded: boolean; diff --git a/packages/lib/server-only/rate-limit/get-quota-alert-kind.ts b/packages/lib/server-only/rate-limit/get-quota-alert-kind.ts index b515e9b54..3082e864e 100644 --- a/packages/lib/server-only/rate-limit/get-quota-alert-kind.ts +++ b/packages/lib/server-only/rate-limit/get-quota-alert-kind.ts @@ -1,6 +1,4 @@ -import { getQuotaWarningCount, QUOTA_WARNING_THRESHOLD } from '../../universal/quota-usage'; - -export { QUOTA_WARNING_THRESHOLD }; +import { getQuotaWarningCount } from '../../universal/quota-usage'; export type QuotaAlertKind = 'quota' | 'quotaNearing';