Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

3.6 KB
Raw Blame
import {
  Popover,
  PopoverButton,
  PopoverPanel,
  Transition,
} from '@headlessui/react'
import cx from 'clsx'
import _map from 'lodash/map'
import { CalendarDotsIcon, CaretDownIcon } from '@phosphor-icons/react'
import { Fragment, useMemo } from 'react'
import { useTranslation } from 'react-i18next'

import { buttonClasses } from '~/ui/Button'

interface PeriodSelectorProps {
  activePeriod: string
  setActivePeriod: (period: string) => void
  isLoading: boolean
}

export const PeriodSelector = ({
  activePeriod,
  setActivePeriod,
  isLoading,
}: PeriodSelectorProps) => {
  const { t } = useTranslation('common')

  const periods = useMemo(() => {
    return [
      {
        label: t('project.thisHour'),
        period: '1h',
      },
      {
        label: t('project.last24h'),
        period: '1d',
      },
      {
        label: t('project.lastXDays', { amount: 7 }),
        period: '7d',
      },
      {
        label: t('project.lastXWeeks', { amount: 4 }),
        period: '4w',
      },
      {
        label: t('project.lastXMonths', { amount: 3 }),
        period: '3M',
      },
      {
        label: t('project.lastXMonths', { amount: 12 }),
        period: '12M',
      },
      {
        label: t('project.lastXMonths', { amount: 24 }),
        period: '24M',
      },
      {
        label: t('project.all'),
        period: 'all',
      },
    ]
  }, [t])

  const period = periods.find((item) => item.period === activePeriod)?.label

  return (
    <Popover className='relative'>
      {({ open }) => (
        <>
          <PopoverButton
            className={buttonClasses({ variant: 'icon', className: 'w-full' })}
          >
            <CalendarDotsIcon className='mr-1 size-5' />
            <span>{period}</span>
            <CaretDownIcon
              className={cx(
                'ml-2 size-5 transform-gpu text-gray-700 transition ease-in-out dark:text-gray-50',
                {
                  'rotate-180': open,
                },
              )}
              aria-hidden='true'
            />
          </PopoverButton>
          <Transition
            as={Fragment}
            enter='transition-[opacity,transform] ease-out duration-150'
            enterFrom='opacity-0 scale-[0.98] translate-y-1'
            enterTo='opacity-100 scale-100 translate-y-0'
            leave='transition-[opacity,transform] ease-out duration-100'
            leaveFrom='opacity-100 scale-100 translate-y-0'
            leaveTo='opacity-0 scale-[0.98] translate-y-1'
          >
            {/* w-56 */}
            <PopoverPanel className='absolute right-0 z-20 mt-2 w-max max-w-sm px-4 sm:px-0 lg:max-w-3xl'>
              <div className='overflow-hidden rounded-lg bg-gray-50 p-1 ring-1 ring-gray-200 dark:bg-slate-900 dark:ring-slate-800'>
                <ul className='w-full text-left'>
                  {_map(periods, (item) => (
                    <PopoverButton
                      as='li'
                      key={item.label}
                      onClick={() => setActivePeriod(item.period)}
                      className={cx(
                        'block cursor-pointer rounded-md px-4 py-2 text-sm text-gray-700 transition-colors hover:bg-gray-200 dark:text-gray-50 dark:hover:bg-slate-800',
                        {
                          'cursor-wait': isLoading,
                        },
                      )}
                    >
                      {item.label}
                    </PopoverButton>
                  ))}
                </ul>
              </div>
            </PopoverPanel>
          </Transition>
        </>
      )}
    </Popover>
  )
}