Skip to content

The open alternative to Salesforce, designed for AI.

Show starter limits on the empty Limits tab instead of redirecting (#28550)

Félix Malfait5e3004060df7
diff --git a/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsContent.tsx b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsContent.tsx
index 22a9e1760f4..b09fa2c4968 100644
--- a/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsContent.tsx
+++ b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsContent.tsx
@@ -1,9 +1,9 @@
import { useLingui } from '@lingui/react/macro';
-import { Navigate } from 'react-router-dom';
-import { SettingsPath } from 'twenty-shared/types';
-import { getSettingsPath, isDefined } from 'twenty-shared/utils';
+import { isDefined } from 'twenty-shared/utils';
+import { SettingsBillingLimitsEmptyState } from '@/settings/billing/components/SettingsBillingLimitsEmptyState';
import { SettingsBillingLimitsTable } from '@/settings/billing/components/SettingsBillingLimitsTable';
+import { useUsageQuotaDefinitions } from '@/settings/billing/hooks/useUsageQuotaDefinitions';
import { useUsageQuotasWithConsumption } from '@/settings/billing/hooks/useUsageQuotasWithConsumption';
import { hasKnownUsageLimitSpender } from '@/settings/billing/utils/hasKnownUsageLimitSpender';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
@@ -13,10 +13,12 @@ export const SettingsBillingLimitsContent = () => {
const { t } = useLingui();
const { usageQuotasWithConsumption, loading, error } =
useUsageQuotasWithConsumption();
+ const { usageQuotaDefinitions, loading: definitionsLoading } =
+ useUsageQuotaDefinitions();
const quotas = usageQuotasWithConsumption.filter(hasKnownUsageLimitSpender);
- if (loading) {
+ if (loading || definitionsLoading) {
return null;
}
@@ -35,7 +37,9 @@ export const SettingsBillingLimitsContent = () => {
if (quotas.length === 0) {
return (
- <Navigate to={getSettingsPath(SettingsPath.BillingNewLimit)} replace />
+ <SettingsPageContainer>
+ <SettingsBillingLimitsEmptyState definitions={usageQuotaDefinitions} />
+ </SettingsPageContainer>
);
}
diff --git a/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsEmptyState.tsx b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsEmptyState.tsx
new file mode 100644
index 00000000000..6420b8593b2
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitsEmptyState.tsx
@@ -0,0 +1,64 @@
+import { styled } from '@linaria/react';
+import { useLingui } from '@lingui/react/macro';
+import { SettingsPath } from 'twenty-shared/types';
+import { getSettingsPath, isDefined } from 'twenty-shared/utils';
+import { Section } from 'twenty-ui/components/layout';
+import { IconPlus } from 'twenty-ui/icon';
+import { themeCssVariables, useTheme } from 'twenty-ui/theme';
+
+import { USAGE_LIMIT_PRESET_QUERY_PARAM } from '@/settings/billing/constants/UsageLimitPresetQueryParam';
+import { USAGE_LIMIT_PRESETS } from '@/settings/billing/constants/UsageLimitPresets';
+import { isUsageLimitPresetAvailable } from '@/settings/billing/utils/isUsageLimitPresetAvailable';
+import { SettingsCard } from '@/settings/components/SettingsCard';
+import { type UsageQuotaDefinitionsQuery } from '~/generated-metadata/graphql';
+
+const StyledCards = styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: ${themeCssVariables.spacing[2]};
+`;
+
+type SettingsBillingLimitsEmptyStateProps = {
+ definitions?: UsageQuotaDefinitionsQuery['usageQuotaDefinitions'];
+};
+
+export const SettingsBillingLimitsEmptyState = ({
+ definitions,
+}: SettingsBillingLimitsEmptyStateProps) => {
+ const { t } = useLingui();
+ const theme = useTheme();
+
+ const presetEntries = isDefined(definitions)
+ ? Object.entries(USAGE_LIMIT_PRESETS).filter(([, preset]) =>
+ isUsageLimitPresetAvailable({ definitions, preset }),
+ )
+ : [];
+
+ return (
+ <Section.Root>
+ <Section.Header
+ title={t`Limits`}
+ description={t`Cap what your workspace can spend. Start from a common limit or build your own.`}
+ />
+ <StyledCards>
+ {presetEntries.map(([presetId, preset]) => (
+ <SettingsCard
+ key={presetId}
+ to={getSettingsPath(SettingsPath.BillingNewLimit, undefined, {
+ [USAGE_LIMIT_PRESET_QUERY_PARAM]: presetId,
+ })}
+ Icon={<preset.Icon size={theme.icon.size.md} />}
+ title={t(preset.title)}
+ description={t(preset.description)}
+ />
+ ))}
+ <SettingsCard
+ to={getSettingsPath(SettingsPath.BillingNewLimit)}
+ Icon={<IconPlus size={theme.icon.size.md} />}
+ title={t`Start from scratch`}
+ description={t`Choose any usage, who it applies to and how often it resets.`}
+ />
+ </StyledCards>
+ </Section.Root>
+ );
+};
diff --git a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresetQueryParam.ts b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresetQueryParam.ts
new file mode 100644
index 00000000000..9e513d2bc50
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresetQueryParam.ts
@@ -0,0 +1 @@
+export const USAGE_LIMIT_PRESET_QUERY_PARAM = 'preset';
diff --git a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresets.ts b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresets.ts
new file mode 100644
index 00000000000..c72386eb8ca
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitPresets.ts
@@ -0,0 +1,48 @@
+import { msg } from '@lingui/core/macro';
+import { IconMail, IconSettingsAutomation, IconSparkles } from 'twenty-ui/icon';
+
+import { type UsageLimitPreset } from '@/settings/billing/types/UsageLimitPreset';
+import {
+ UsageOperationType,
+ UsageResourceType,
+ UsageUnit,
+} from '~/generated-metadata/graphql';
+
+export const USAGE_LIMIT_PRESETS = {
+ aiCredits: {
+ Icon: IconSparkles,
+ title: msg`AI credits`,
+ description: msg`Cap what the workspace spends on AI each month.`,
+ values: {
+ resourceType: UsageResourceType.AI,
+ operationType: UsageOperationType.ALL,
+ spenderType: 'workspace',
+ unit: UsageUnit.CREDIT,
+ periodUnit: 'month',
+ },
+ },
+ workflowCredits: {
+ Icon: IconSettingsAutomation,
+ title: msg`Workflow credits`,
+ description: msg`Cap what your workflows spend each month.`,
+ values: {
+ resourceType: UsageResourceType.WORKFLOW,
+ operationType: UsageOperationType.WORKFLOW_EXECUTION,
+ spenderType: 'workspace',
+ unit: UsageUnit.CREDIT,
+ periodUnit: 'month',
+ },
+ },
+ monthlyEmails: {
+ Icon: IconMail,
+ title: msg`Emails sent`,
+ description: msg`Cap how many emails the workspace sends each month.`,
+ values: {
+ resourceType: UsageResourceType.EMAIL,
+ operationType: UsageOperationType.EMAIL_SEND,
+ spenderType: 'workspace',
+ unit: UsageUnit.INVOCATION,
+ periodUnit: 'month',
+ },
+ },
+} as const satisfies Record<string, UsageLimitPreset>;
diff --git a/packages/twenty-front/src/modules/settings/billing/types/UsageLimitPreset.ts b/packages/twenty-front/src/modules/settings/billing/types/UsageLimitPreset.ts
new file mode 100644
index 00000000000..eaffa21171f
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/types/UsageLimitPreset.ts
@@ -0,0 +1,23 @@
+import { type MessageDescriptor } from '@lingui/core';
+import { type IconComponent } from 'twenty-ui/icon';
+
+import { type UsageLimitPeriodUnit } from '@/settings/billing/types/UsageLimitPeriodUnit';
+import { type UsageLimitSpenderType } from '@/settings/billing/types/UsageLimitSpenderType';
+import {
+ type UsageOperationType,
+ type UsageResourceType,
+ type UsageUnit,
+} from '~/generated-metadata/graphql';
+
+export type UsageLimitPreset = {
+ Icon: IconComponent;
+ title: MessageDescriptor;
+ description: MessageDescriptor;
+ values: {
+ resourceType: UsageResourceType;
+ operationType: UsageOperationType;
+ spenderType: UsageLimitSpenderType;
+ unit: UsageUnit;
+ periodUnit: UsageLimitPeriodUnit;
+ };
+};
diff --git a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildUsageLimitFormValuesFromPreset.test.ts b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildUsageLimitFormValuesFromPreset.test.ts
new file mode 100644
index 00000000000..972d40b679d
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildUsageLimitFormValuesFromPreset.test.ts
@@ -0,0 +1,91 @@
+import { EMPTY_USAGE_LIMIT_FORM_VALUES } from '@/settings/billing/constants/EmptyUsageLimitFormValues';
+import { buildUsageLimitFormValuesFromPreset } from '@/settings/billing/utils/buildUsageLimitFormValuesFromPreset';
+import {
+ UsageOperationType,
+ type UsageQuotaDefinitionsQuery,
+ UsageResourceType,
+ UsageUnit,
+} from '~/generated-metadata/graphql';
+
+type UsageQuotaDefinitions =
+ UsageQuotaDefinitionsQuery['usageQuotaDefinitions'];
+type OperatorOnlyScopes =
+ UsageQuotaDefinitions['definitions'][number]['operatorOnlyScopes'];
+
+const AI_DEFINITION = {
+ resourceType: UsageResourceType.AI,
+ allowedOperations: [
+ {
+ operationType: UsageOperationType.ALL,
+ allowedUnits: [UsageUnit.CREDIT],
+ },
+ {
+ operationType: UsageOperationType.AI_CHAT_TOKEN,
+ allowedUnits: [UsageUnit.CREDIT, UsageUnit.TOKEN],
+ },
+ ],
+ allowedSpenderTypes: ['workspace', 'userWorkspace'],
+ operatorOnlyScopes: [],
+};
+
+const buildDefinitions = (
+ operatorOnlyScopes: OperatorOnlyScopes = [],
+): UsageQuotaDefinitions => ({
+ definitions: [{ ...AI_DEFINITION, operatorOnlyScopes }],
+ isIntraWorkspaceLimitEntitled: true,
+ hasAllowancePeriod: false,
+});
+
+describe('buildUsageLimitFormValuesFromPreset', () => {
+ it('prefills the form from a known preset', () => {
+ expect(
+ buildUsageLimitFormValuesFromPreset({
+ definitions: buildDefinitions(),
+ presetId: 'aiCredits',
+ }),
+ ).toEqual({
+ ...EMPTY_USAGE_LIMIT_FORM_VALUES,
+ resourceType: UsageResourceType.AI,
+ operationType: UsageOperationType.ALL,
+ spenderType: 'workspace',
+ unit: UsageUnit.CREDIT,
+ periodUnit: 'month',
+ });
+ });
+
+ it('starts empty without a preset or with an unknown one', () => {
+ for (const presetId of [null, 'unknown', 'toString']) {
+ expect(
+ buildUsageLimitFormValuesFromPreset({
+ definitions: buildDefinitions(),
+ presetId,
+ }),
+ ).toEqual(EMPTY_USAGE_LIMIT_FORM_VALUES);
+ }
+ });
+
+ it('starts empty when the preset usage is not offered', () => {
+ expect(
+ buildUsageLimitFormValuesFromPreset({
+ definitions: buildDefinitions(),
+ presetId: 'workflowCredits',
+ }),
+ ).toEqual(EMPTY_USAGE_LIMIT_FORM_VALUES);
+ });
+
+ it('starts empty when the preset period is reserved to the operator', () => {
+ expect(
+ buildUsageLimitFormValuesFromPreset({
+ definitions: buildDefinitions([
+ {
+ operationType: UsageOperationType.ALL,
+ spenderType: 'workspace',
+ unit: UsageUnit.CREDIT,
+ periodUnit: 'month',
+ },
+ ]),
+ presetId: 'aiCredits',
+ }),
+ ).toEqual(EMPTY_USAGE_LIMIT_FORM_VALUES);
+ });
+});
diff --git a/packages/twenty-front/src/modules/settings/billing/utils/buildUsageLimitFormValuesFromPreset.ts b/packages/twenty-front/src/modules/settings/billing/utils/buildUsageLimitFormValuesFromPreset.ts
new file mode 100644
index 00000000000..1a478eec10f
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/utils/buildUsageLimitFormValuesFromPreset.ts
@@ -0,0 +1,26 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { EMPTY_USAGE_LIMIT_FORM_VALUES } from '@/settings/billing/constants/EmptyUsageLimitFormValues';
+import { USAGE_LIMIT_PRESETS } from '@/settings/billing/constants/UsageLimitPresets';
+import { type UsageLimitFormValues } from '@/settings/billing/types/UsageLimitFormValues';
+import { isKeyOfRecord } from '@/settings/billing/utils/isKeyOfRecord';
+import { isUsageLimitPresetAvailable } from '@/settings/billing/utils/isUsageLimitPresetAvailable';
+import { type UsageQuotaDefinitionsQuery } from '~/generated-metadata/graphql';
+
+export const buildUsageLimitFormValuesFromPreset = ({
+ definitions,
+ presetId,
+}: {
+ definitions: UsageQuotaDefinitionsQuery['usageQuotaDefinitions'];
+ presetId: string | null;
+}): UsageLimitFormValues => {
+ if (!isDefined(presetId) || !isKeyOfRecord(USAGE_LIMIT_PRESETS, presetId)) {
+ return EMPTY_USAGE_LIMIT_FORM_VALUES;
+ }
+
+ const preset = USAGE_LIMIT_PRESETS[presetId];
+
+ return isUsageLimitPresetAvailable({ definitions, preset })
+ ? { ...EMPTY_USAGE_LIMIT_FORM_VALUES, ...preset.values }
+ : EMPTY_USAGE_LIMIT_FORM_VALUES;
+};
diff --git a/packages/twenty-front/src/modules/settings/billing/utils/isUsageLimitPresetAvailable.ts b/packages/twenty-front/src/modules/settings/billing/utils/isUsageLimitPresetAvailable.ts
new file mode 100644
index 00000000000..2498a35eb42
--- /dev/null
+++ b/packages/twenty-front/src/modules/settings/billing/utils/isUsageLimitPresetAvailable.ts
@@ -0,0 +1,24 @@
+import { EMPTY_USAGE_LIMIT_FORM_VALUES } from '@/settings/billing/constants/EmptyUsageLimitFormValues';
+import { type UsageLimitPreset } from '@/settings/billing/types/UsageLimitPreset';
+import { getUsageLimitFormOptions } from '@/settings/billing/utils/getUsageLimitFormOptions';
+import { type UsageQuotaDefinitionsQuery } from '~/generated-metadata/graphql';
+
+export const isUsageLimitPresetAvailable = ({
+ definitions,
+ preset,
+}: {
+ definitions: UsageQuotaDefinitionsQuery['usageQuotaDefinitions'];
+ preset: UsageLimitPreset;
+}): boolean => {
+ const options = getUsageLimitFormOptions({
+ definitions,
+ values: { ...EMPTY_USAGE_LIMIT_FORM_VALUES, ...preset.values },
+ });
+
+ return (
+ options.operationTypes.includes(preset.values.operationType) &&
+ options.spenderTypes.includes(preset.values.spenderType) &&
+ options.units.includes(preset.values.unit) &&
+ options.periodUnits.includes(preset.values.periodUnit)
+ );
+};
diff --git a/packages/twenty-front/src/pages/settings/billing/SettingsBillingNewLimit.tsx b/packages/twenty-front/src/pages/settings/billing/SettingsBillingNewLimit.tsx
index 7fb7a1a1b50..5f66acd8e18 100644
--- a/packages/twenty-front/src/pages/settings/billing/SettingsBillingNewLimit.tsx
+++ b/packages/twenty-front/src/pages/settings/billing/SettingsBillingNewLimit.tsx
@@ -1,17 +1,20 @@
import { CombinedGraphQLErrors } from '@apollo/client/errors';
import { Trans, useLingui } from '@lingui/react/macro';
import { useState } from 'react';
+import { useSearchParams } from 'react-router-dom';
import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath, isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { SettingsBillingLimitForm } from '@/settings/billing/components/SettingsBillingLimitForm';
import { EMPTY_USAGE_LIMIT_FORM_VALUES } from '@/settings/billing/constants/EmptyUsageLimitFormValues';
+import { USAGE_LIMIT_PRESET_QUERY_PARAM } from '@/settings/billing/constants/UsageLimitPresetQueryParam';
import { useCreateUsageLimit } from '@/settings/billing/hooks/useCreateUsageLimit';
import { useUsageQuotaDefinitions } from '@/settings/billing/hooks/useUsageQuotaDefinitions';
import { useUsageQuotaScopeConsumption } from '@/settings/billing/hooks/useUsageQuotaScopeConsumption';
import { type UsageLimitFormValues } from '@/settings/billing/types/UsageLimitFormValues';
import { buildCreateUsageLimitInput } from '@/settings/billing/utils/buildCreateUsageLimitInput';
+import { buildUsageLimitFormValuesFromPreset } from '@/settings/billing/utils/buildUsageLimitFormValuesFromPreset';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { SettingsPageLayout } from '@/settings/components/layout/SettingsPageLayout';
@@ -25,10 +28,21 @@ export const SettingsBillingNewLimit = () => {
useUsageQuotaDefinitions();
const { createUsageLimit, loading: isSaving } = useCreateUsageLimit();
- const [values, setValues] = useState<UsageLimitFormValues>(
- EMPTY_USAGE_LIMIT_FORM_VALUES,
+ const [searchParams] = useSearchParams();
+ const [editedValues, setEditedValues] = useState<UsageLimitFormValues | null>(
+ null,
);
+ // Definitions load after mount, so the preset is resolved on read until the first edit.
+ const values =
+ editedValues ??
+ (isDefined(usageQuotaDefinitions)
+ ? buildUsageLimitFormValuesFromPreset({
+ definitions: usageQuotaDefinitions,
+ presetId: searchParams.get(USAGE_LIMIT_PRESET_QUERY_PARAM),
+ })
+ : EMPTY_USAGE_LIMIT_FORM_VALUES);
+
const input = buildCreateUsageLimitInput(values);
const { scopeConsumption } = useUsageQuotaScopeConsumption(values);
@@ -87,7 +101,7 @@ export const SettingsBillingNewLimit = () => {
definitions={usageQuotaDefinitions}
values={values}
scopeConsumption={scopeConsumption}
- onChange={setValues}
+ onChange={setEditedValues}
/>
)}
</SettingsPageContainer>