Skip to content

The open alternative to Salesforce, designed for AI.

2.1 KB
Raw Blame
import { formatInTimeZone } from 'date-fns-tz';

import { DateFormat } from '@/localization/constants/DateFormat';
import { DATE_TIME_SETTINGS_PREVIEW_DATE } from '@/localization/constants/DateTimeSettingsPreviewDate';
import { detectDateFormat } from '@/localization/utils/detection/detectDateFormat';
import { detectTimeZone } from '@/localization/utils/detection/detectTimeZone';
import { Select } from '@/ui/input/components/Select';
import { useLingui } from '@lingui/react/macro';

type DateTimeSettingsDateFormatSelectProps = {
  value: DateFormat;
  onChange: (nextValue: DateFormat) => void;
  timeZone: string;
};

export const DateTimeSettingsDateFormatSelect = ({
  onChange,
  timeZone,
  value,
}: DateTimeSettingsDateFormatSelectProps) => {
  const { t } = useLingui();

  const systemTimeZone = detectTimeZone();

  const usedTimeZone = timeZone === 'system' ? systemTimeZone : timeZone;

  const systemDateFormat = DateFormat[detectDateFormat()];

  const systemDateFormatLabel = formatInTimeZone(
    DATE_TIME_SETTINGS_PREVIEW_DATE,
    usedTimeZone,
    systemDateFormat,
  );

  return (
    <Select
      dropdownId="datetime-settings-date-format"
      dropdownWidth={320}
      label={t`Date format`}
      fullWidth
      value={value}
      pinnedOption={{
        label: t`System settings`,
        value: DateFormat.SYSTEM,
        contextualText: systemDateFormatLabel,
      }}
      options={[
        {
          label: formatInTimeZone(
            DATE_TIME_SETTINGS_PREVIEW_DATE,
            usedTimeZone,
            DateFormat.MONTH_FIRST,
          ),
          value: DateFormat.MONTH_FIRST,
        },
        {
          label: formatInTimeZone(
            DATE_TIME_SETTINGS_PREVIEW_DATE,
            usedTimeZone,
            DateFormat.DAY_FIRST,
          ),
          value: DateFormat.DAY_FIRST,
        },
        {
          label: formatInTimeZone(
            DATE_TIME_SETTINGS_PREVIEW_DATE,
            usedTimeZone,
            DateFormat.YEAR_FIRST,
          ),
          value: DateFormat.YEAR_FIRST,
        },
      ]}
      onChange={onChange}
    />
  );
};