The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
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';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
export const SettingsBillingNewLimit = () => {
const { t } = useLingui();
const navigate = useNavigateSettings();
const { enqueueToast } = useToast();
const { usageQuotaDefinitions, loading: definitionsLoading } =
useUsageQuotaDefinitions();
const { createUsageLimit, loading: isSaving } = useCreateUsageLimit();
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);
const handleSave = async () => {
if (!isDefined(input)) {
return;
}
try {
await createUsageLimit({ variables: { input } });
enqueueToast({ status: 'success', children: t`Limit created.` });
navigate(SettingsPath.BillingLimits);
} catch (error) {
const serverMessage =
error instanceof CombinedGraphQLErrors
? error.errors[0]?.message
: undefined;
enqueueToast({
status: 'error',
children: serverMessage ?? t`Failed to create the limit.`,
});
}
};
return (
<SettingsPageLayout
title={t`New limit`}
links={[
{
children: <Trans>Workspace</Trans>,
href: getSettingsPath(SettingsPath.General),
},
{
children: <Trans>Billing</Trans>,
href: getSettingsPath(SettingsPath.Billing),
},
{
children: <Trans>Limits</Trans>,
href: getSettingsPath(SettingsPath.BillingLimits),
},
{ children: <Trans>New limit</Trans> },
]}
actionButton={
<SaveAndCancelButtons
onSave={handleSave}
onCancel={() => navigate(SettingsPath.BillingLimits)}
isSaveDisabled={!isDefined(input)}
isLoading={isSaving}
/>
}
>
<SettingsPageContainer>
{definitionsLoading || !isDefined(usageQuotaDefinitions) ? null : (
<SettingsBillingLimitForm
definitions={usageQuotaDefinitions}
values={values}
scopeConsumption={scopeConsumption}
onChange={setEditedValues}
/>
)}
</SettingsPageContainer>
</SettingsPageLayout>
);
};