Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

1.2 KB
Raw Blame
import React from "react"
import { classSet, formatDate, timeSince } from "@fider/services"

interface MomentText {
  locale: string
  date: Date | string
  className?: string
  format?: "relative" | "full" | "short" | "date"
}

export const Moment = (props: MomentText) => {
  if (!props.date) {
    return <span />
  }

  const format = props.format || "relative"

  const now = new Date()
  const date = props.date instanceof Date ? props.date : new Date(props.date)
  const diff = (now.getTime() - date.getTime()) / (60 * 60 * 24 * 1000)
  const display =
    diff >= 365 && format === "relative"
      ? formatDate(props.locale, props.date, "short")
      : format === "relative"
        ? timeSince(props.locale, now, date)
        : format === "date"
          ? formatDate(props.locale, props.date, "date")
          : formatDate(props.locale, props.date, format)

  const tooltip = props.format === "short" ? formatDate(props.locale, props.date, "full") : undefined

  const className = classSet({
    ...(props.className ? { [props.className]: true } : {}),
    date: true,
  })

  return (
    <span className={className} data-tooltip={tooltip}>
      {display}
    </span>
  )
}