Skip to content

The open alternative to Salesforce, designed for AI.

7.4 KB
Raw Blame
import { type ConfigVariableOptions } from '@/settings/admin-panel/config-variables/types/ConfigVariableOptions';
import { Select } from '@/ui/input/components/Select';
import { SelectControl } from '@/ui/input/components/SelectControl';
import { TextArea } from '@/ui/input/components/TextArea';
import { TextInput } from '@/ui/input/components/TextInput';
import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { styled } from '@linaria/react';
import { type ConfigVariableValue } from 'twenty-shared/types';
import { CustomError, isDefined } from 'twenty-shared/utils';
import { doNotTranslate } from 'twenty-shared/i18n';
import { Dropdown } from 'twenty-ui/components/navigation';
import { CodeEditor } from 'twenty-ui/components/code-editor';
import { themeCssVariables } from 'twenty-ui/theme';
import { ConfigVariableType } from '~/generated-admin/graphql';
import { useLingui } from '@lingui/react/macro';

const StyledJsonEditorContainer = styled.div`
  display: flex;
  flex-direction: column;
  width: 100%;
`;

const StyledJsonEditorLabel = styled.span`
  color: ${themeCssVariables.font.color.light};
  display: block;
  font-size: ${themeCssVariables.font.size.xs};
  font-weight: ${themeCssVariables.font.weight.semiBold};
  margin-bottom: ${themeCssVariables.spacing[1]};
`;

type ConfigVariableDatabaseInputProps = {
  label: string;
  value: ConfigVariableValue;
  onChange: (value: ConfigVariableValue) => void;
  type: ConfigVariableType;
  options?: ConfigVariableOptions;
  disabled?: boolean;
  placeholder?: string;
};

export const ConfigVariableDatabaseInput = ({
  label,
  value,
  onChange,
  type,
  options,
  disabled,
  placeholder,
}: ConfigVariableDatabaseInputProps) => {
  const { t } = useLingui();
  const selectOptions =
    options && Array.isArray(options)
      ? options.map((option) => ({
          value: String(option),
          label: String(option),
        }))
      : [];

  const booleanOptions = [
    { value: 'true', label: doNotTranslate('true') },
    { value: 'false', label: doNotTranslate('false') },
  ];

  const isValueSelected = (optionValue: string) => {
    if (!Array.isArray(value)) return false;
    return value.includes(optionValue);
  };

  const handleMultiSelectChange = (optionValue: string) => {
    if (!Array.isArray(value)) return;

    let newValues = [...value];
    if (isValueSelected(optionValue)) {
      newValues = newValues.filter((val) => val !== optionValue);
    } else {
      newValues.push(optionValue);
    }
    onChange(newValues);
  };

  const jsonArrayTextAreaId = `${label}-json-array`;

  switch (type) {
    case ConfigVariableType.BOOLEAN:
      return (
        <Select
          label={label}
          value={String(value ?? '')}
          onChange={(newValue: string) => onChange(newValue === 'true')}
          disabled={disabled}
          options={booleanOptions}
          dropdownId="config-variable-boolean-select"
          fullWidth
        />
      );

    case ConfigVariableType.NUMBER:
      return (
        <TextInput
          label={label}
          value={value !== null && value !== undefined ? String(value) : ''}
          onChange={(text) => {
            const num = Number(text);
            onChange(isNaN(num) ? text : num);
          }}
          disabled={disabled}
          placeholder={placeholder}
          type="number"
          fullWidth
        />
      );

    case ConfigVariableType.ARRAY:
      return (
        <>
          {options && Array.isArray(options) ? (
            <DropdownRoot
              dropdownId="config-variable-array-dropdown"
              type="picker"
              multiple
            >
              <Dropdown.Trigger
                render={<div />}
                nativeButton={false}
                disabled={disabled}
              >
                <SelectControl
                  selectedOption={{
                    value: '',
                    label:
                      Array.isArray(value) && value.length > 0
                        ? value.join(', ')
                        : t`Select options`,
                  }}
                  isDisabled={disabled}
                  hasRightElement={false}
                  selectSizeVariant="default"
                />
              </Dropdown.Trigger>
              <DropdownContent side="bottom" align="start" sideOffset={8}>
                <Dropdown.Section>
                  {selectOptions.map((option) => (
                    <Dropdown.OptionItem
                      key={option.value}
                      selected={isValueSelected(option.value)}
                      onSelect={() => handleMultiSelectChange(option.value)}
                    >
                      {option.label}
                    </Dropdown.OptionItem>
                  ))}
                </Dropdown.Section>
              </DropdownContent>
            </DropdownRoot>
          ) : (
            <TextArea
              textAreaId={jsonArrayTextAreaId}
              label={label}
              maxRows={5}
              value={
                Array.isArray(value)
                  ? JSON.stringify(value)
                  : String(value ?? '')
              }
              onChange={(text) => {
                try {
                  const arr = JSON.parse(text);
                  onChange(Array.isArray(arr) ? arr : value);
                } catch {
                  onChange(text);
                }
              }}
              disabled={disabled}
              placeholder={placeholder || t`Enter JSON array`}
            />
          )}
        </>
      );

    case ConfigVariableType.ENUM:
      return (
        <Select
          label={label}
          value={String(value ?? '')}
          onChange={(newValue: string) => onChange(newValue)}
          disabled={disabled}
          options={selectOptions}
          dropdownId="config-variable-enum-select"
          fullWidth
        />
      );

    case ConfigVariableType.STRING:
      return (
        <TextInput
          label={label}
          value={
            typeof value === 'string'
              ? value
              : value !== null && value !== undefined
                ? JSON.stringify(value)
                : ''
          }
          onChange={(text) => onChange(text)}
          disabled={disabled}
          placeholder={placeholder || t`Enter value`}
          fullWidth
        />
      );

    case ConfigVariableType.JSON:
      return (
        <StyledJsonEditorContainer>
          <StyledJsonEditorLabel>{label}</StyledJsonEditorLabel>
          <CodeEditor
            value={
              typeof value === 'string'
                ? value
                : value !== null && value !== undefined
                  ? JSON.stringify(value, null, 2)
                  : ''
            }
            language="json"
            height="200px"
            options={{
              readOnly: disabled === true,
            }}
            onChange={(text) => {
              if (!isDefined(text)) {
                return;
              }

              try {
                onChange(JSON.parse(text) as Record<string, unknown>);
              } catch {
                onChange(text as unknown as ConfigVariableValue);
              }
            }}
          />
        </StyledJsonEditorContainer>
      );

    default:
      throw new CustomError(
        doNotTranslate(`Unsupported type: ${type}`),
        'UNSUPPORTED_TYPE',
      );
  }
};