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.7 KB
Raw Blame
import { CheckCircleIcon, XCircleIcon, InfoIcon } from '@phosphor-icons/react'
import type { ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from '~/ui/Link'

import { cn } from '~/utils/generic'

import { buttonClasses } from './Button'
import Spin from './icons/Spin'
import { Text } from './Text'

type StatusType = 'success' | 'error' | 'info'

interface ActionLink {
  label: string
  to: string
  primary?: boolean
}

interface ActionButton {
  label: string
  onClick: () => void
  primary?: boolean
}

type Action = ActionLink | ActionButton

const isActionLink = (action: Action): action is ActionLink => 'to' in action

interface StatusPageProps {
  loading?: boolean
  type?: StatusType
  title?: string
  description?: string
  icon?: ReactNode
  actions?: Action[]
}

const iconMap: Record<StatusType, ReactNode> = {
  success: (
    <CheckCircleIcon
      className='size-6 text-emerald-600 dark:text-emerald-400'
      weight='duotone'
      aria-hidden='true'
    />
  ),
  error: (
    <XCircleIcon
      className='size-6 text-red-600 dark:text-red-400'
      weight='duotone'
      aria-hidden='true'
    />
  ),
  info: (
    <InfoIcon
      className='size-6 text-amber-600 dark:text-amber-400'
      weight='duotone'
      aria-hidden='true'
    />
  ),
}

const iconBgMap: Record<StatusType, string> = {
  success: 'bg-emerald-100/70 dark:bg-emerald-500/10',
  error: 'bg-red-100/70 dark:bg-red-500/10',
  info: 'bg-amber-100/70 dark:bg-amber-500/10',
}

const StatusPage = ({
  loading,
  type = 'success',
  title,
  description,
  icon,
  actions,
}: StatusPageProps) => {
  const { t } = useTranslation('common')

  if (loading) {
    return (
      <div className='flex min-h-page items-center justify-center bg-gray-50 dark:bg-slate-950'>
        <output className='flex flex-col items-center' aria-live='polite'>
          <Spin />
          <span className='sr-only'>{t('common.loading')}</span>
        </output>
      </div>
    )
  }

  const displayIcon = icon ?? iconMap[type]

  return (
    <div className='flex min-h-page items-center justify-center bg-gray-50 px-4 dark:bg-slate-950'>
      <div className='mx-auto w-full max-w-md text-center'>
        <div
          className={cn(
            'mx-auto mb-5 flex size-12 items-center justify-center rounded-2xl',
            iconBgMap[type],
          )}
        >
          {displayIcon}
        </div>

        {title ? (
          <Text
            as='h1'
            size='xl'
            weight='semibold'
            className='tracking-tight text-balance'
          >
            {title}
          </Text>
        ) : null}

        {description ? (
          <Text
            as='p'
            size='base'
            colour='secondary'
            className='mt-2 text-pretty'
          >
            {description}
          </Text>
        ) : null}

        {actions && actions.length > 0 ? (
          <div className='mt-6 flex flex-wrap items-center justify-center gap-2.5'>
            {actions.map((action, index) => {
              const className = buttonClasses({
                variant: action.primary ? 'primary' : 'secondary',
                size: 'lg',
              })

              return isActionLink(action) ? (
                <Link key={action.to} to={action.to} className={className}>
                  {action.label}
                </Link>
              ) : (
                <button
                  key={index}
                  type='button'
                  onClick={action.onClick}
                  className={className}
                >
                  {action.label}
                </button>
              )
            })}
          </div>
        ) : null}
      </div>
    </div>
  )
}

export default StatusPage