Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.3 KB
Raw Blame
import { t } from "ttag";

import { SettingsSection } from "metabase/settings-components";
import {
  NumberInput,
  SegmentedControl,
  Stack,
  Text,
  TextInput,
} from "metabase/ui";
import type { MetabotLimitPeriod, MetabotLimitType } from "metabase-types/api";

import S from "./GeneralLimitsSettingsSection.module.css";
import {
  useAdminSettingWithDebouncedInput,
  useInstanceLimitDebouncedInput,
} from "./hooks";
import {
  getInstanceLimitInputLabel,
  getMaxUsageInputUnit,
  getQuotaMessageInputDescription,
  limitTypeOptions,
  resetPeriodOptions,
  sanitizeUsageLimitValue,
} from "./utils";

export function GeneralLimitsSettingsSection() {
  const { instanceLimit, handleInstanceLimitInputChange } =
    useInstanceLimitDebouncedInput();
  const { handleInputChange: handleLimitTypeChange, inputValue: limitType } =
    useAdminSettingWithDebouncedInput<MetabotLimitType>(
      "metabot-limit-unit",
      "tokens",
    );
  const {
    inputValue: limitPeriod,
    handleInputChange: handleLimitPeriodChange,
  } = useAdminSettingWithDebouncedInput<MetabotLimitPeriod>(
    "metabot-limit-reset-rate",
    "monthly",
  );
  const {
    inputValue: quotaMessage,
    handleInputChange: handleQuotaMessageChange,
  } = useAdminSettingWithDebouncedInput<string | null>(
    "metabot-quota-reached-message",
  );

  return (
    <SettingsSection title={t`Settings and general limits`}>
      <Stack gap="xxl" align="flex-start">
        <Stack gap="sm" align="flex-start">
          <Text fw="bold">{t`How do you want to limit AI usage?`}</Text>
          <SegmentedControl
            data={limitTypeOptions}
            value={limitType}
            onChange={handleLimitTypeChange}
          />
        </Stack>
        <Stack gap="sm" align="flex-start">
          <Text fw="bold">{t`When should usage limits reset?`}</Text>
          <SegmentedControl
            data={resetPeriodOptions}
            value={limitPeriod}
            onChange={handleLimitPeriodChange}
          />
        </Stack>
        <NumberInput
          label={getInstanceLimitInputLabel(
            limitType || "tokens",
            limitPeriod || "monthly",
          )}
          description={t`This is the maximum amount all users should be able to use in total.`}
          placeholder={t`Unlimited`}
          classNames={{
            wrapper: S.InstanceLimitInputWrapper,
            description: S.InputDescription,
            section: S.InputUnitSection,
          }}
          rightSection={getMaxUsageInputUnit(limitType, instanceLimit)}
          rightSectionWidth="auto"
          rightSectionPointerEvents="none"
          min={0}
          decimalScale={0}
          value={instanceLimit != null ? instanceLimit : ""}
          onChange={(value) =>
            handleInstanceLimitInputChange(
              sanitizeUsageLimitValue(String(value)),
            )
          }
        />
        <TextInput
          label={t`Quota-reached message`}
          description={getQuotaMessageInputDescription(limitPeriod)}
          placeholder={t`You have reached your AI usage limit for the current period. Please contact your administrator.`}
          w="100%"
          classNames={{ description: S.InputDescription }}
          value={quotaMessage || ""}
          onChange={(e) => handleQuotaMessageChange(e.target.value)}
        />
      </Stack>
    </SettingsSection>
  );
}