aboutsummaryrefslogtreecommitdiffstats
path: root/public/prism/prism-haskell.min.js
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-02-10 16:35:08 +0100
committerArmand Philippot <git@armandphilippot.com>2022-02-10 16:35:08 +0100
commitf2be002df3b13254a5b549dd1589089545c53f02 (patch)
tree1d057bb3941769df6af99066cecbd5d563676ea3 /public/prism/prism-haskell.min.js
parent74df20972a7e9155fadb1d18aad260c0cd9959a9 (diff)
chore: improve contact form behavior
* The status was not visile in top of the form, so I moved it under the submit button. * It was possible to send an empty form. * The input type for email should be email instead of text.
Diffstat (limited to 'public/prism/prism-haskell.min.js')
0 files changed, 0 insertions, 0 deletions
>97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
import {
  type ForwardRefRenderFunction,
  type TimeHTMLAttributes,
  forwardRef,
} from 'react';
import { useIntl } from 'react-intl';
import { settings } from '../../../../utils/config';

type GetDateOptionsConfig = {
  hasDay: boolean;
  hasMonth: boolean;
  hasWeekDay: boolean;
  hasYear: boolean;
};

const getDateOptions = ({
  hasDay,
  hasMonth,
  hasWeekDay,
  hasYear,
}: GetDateOptionsConfig): Intl.DateTimeFormatOptions => {
  const day: Intl.DateTimeFormatOptions['day'] = 'numeric';
  const month: Intl.DateTimeFormatOptions['month'] = 'long';
  const weekDay: Intl.DateTimeFormatOptions['weekday'] = 'long';
  const year: Intl.DateTimeFormatOptions['year'] = 'numeric';
  const options: [
    keyof Intl.DateTimeFormatOptions,
    Intl.DateTimeFormatOptions[keyof Intl.DateTimeFormatOptions],
  ][] = [];

  if (hasDay) options.push(['day', day]);
  if (hasMonth) options.push(['month', month]);
  if (hasWeekDay) options.push(['weekday', weekDay]);
  if (hasYear) options.push(['year', year]);

  return Object.fromEntries(options);
};

export type TimeProps = Omit<
  TimeHTMLAttributes<HTMLTimeElement>,
  'children' | 'dateTime'
> & {
  /**
   * A valid date string.
   */
  date: string;
  /**
   * Should we hide the day number?
   *
   * @default false
   */
  hideDay?: boolean;
  /**
   * Should we hide the month?
   *
   * @default false
   */
  hideMonth?: boolean;
  /**
   * Should we hide the year?
   *
   * @default false
   */
  hideYear?: boolean;
  /**
   * The current locale.
   *
   * @default settings.locales.defaultLocale
   */
  locale?: string;
  /**
   * Should we display the time in addition to the date?
   *
   * @default false
   */
  showTime?: boolean;
  /**
   * Should we display the week day?
   *
   * @default false
   */
  showWeekDay?: boolean;
};

const TimeWithRef: ForwardRefRenderFunction<HTMLTimeElement, TimeProps> = (
  {
    date,
    hideDay = false,
    hideMonth = false,
    hideYear = false,
    locale = settings.locales.defaultLocale,
    showTime = false,
    showWeekDay = false,
    ...props
  },
  ref
) => {
  const intl = useIntl();
  const dateOptions = getDateOptions({
    hasDay: !hideDay,
    hasMonth: !hideMonth,
    hasWeekDay: showWeekDay,
    hasYear: !hideYear,
  });
  const fullDate = new Date(date);
  const dateTime = fullDate.toISOString();
  const readableDate = fullDate.toLocaleDateString(locale, dateOptions);
  const formattedTime = fullDate.toLocaleTimeString(locale, {
    hour: 'numeric',
    minute: 'numeric',
  });
  const readableTime =
    locale === 'fr' ? formattedTime.replace(':', 'h') : formattedTime;

  return (
    <time {...props} dateTime={dateTime} ref={ref}>
      {showTime
        ? intl.formatMessage(
            {
              defaultMessage: '{date} at {time}',
              description: 'Time: readable date and time',
              id: '8q5PXx',
            },
            { date: readableDate, time: readableTime }
          )
        : readableDate}
    </time>
  );
};

/**
 * Time component.
 *
 * Render a date with an optional time in a `<time>` element.
 */
export const Time = forwardRef(TimeWithRef);