{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-time-ago",
  "title": "useTimeAgo",
  "description": "A reactive relative-time formatter (\"۲ روز پیش\" / \"2 days ago\") with built-in Persian and English message tables.",
  "registryDependencies": [
    "@persianlabsui/persian-date"
  ],
  "files": [
    {
      "path": "registry/base/hooks/use-time-ago.ts",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport {\n  formatDate,\n  toPersianDigits,\n  type CalendarType,\n} from \"@/lib/persian-date\"\n\nexport type TimeAgoFormatter<T = number> = (value: T, isPast: boolean) => string\n\nexport type TimeAgoUnitName =\n  \"second\" | \"minute\" | \"hour\" | \"day\" | \"week\" | \"month\" | \"year\"\n\nexport interface TimeAgoMessages {\n  justNow: string\n  past: string | TimeAgoFormatter<string>\n  future: string | TimeAgoFormatter<string>\n  invalid: string\n  second: string | TimeAgoFormatter<number>\n  minute: string | TimeAgoFormatter<number>\n  hour: string | TimeAgoFormatter<number>\n  day: string | TimeAgoFormatter<number>\n  week: string | TimeAgoFormatter<number>\n  month: string | TimeAgoFormatter<number>\n  year: string | TimeAgoFormatter<number>\n}\n\nexport interface TimeAgoUnit {\n  max: number\n  value: number\n  name: TimeAgoUnitName\n}\n\nexport interface FormatTimeAgoOptions {\n  /** Beyond this many milliseconds (or this unit's `max`), fall back to a full date. @default undefined */\n  max?: TimeAgoUnitName | number\n  /** Formatter used for the full-date fallback. @default uses formatDate with \"yyyy/MM/dd\" */\n  fullDateFormatter?: (date: Date) => string\n  /** @default faMessages (or enMessages, see the `locale` option) */\n  messages?: TimeAgoMessages\n  /** @default \"fa\" */\n  locale?: \"fa\" | \"en\"\n  /** Calendar used by the default fullDateFormatter. @default \"shamsi\" */\n  calendarType?: CalendarType\n  /** Show seconds instead of collapsing sub-minute diffs into \"just now\". @default false */\n  showSecond?: boolean\n  /** @default \"round\" */\n  rounding?: \"round\" | \"ceil\" | \"floor\" | number\n  /** Override the unit table (thresholds in milliseconds). */\n  units?: TimeAgoUnit[]\n}\n\nexport interface UseTimeAgoOptions extends FormatTimeAgoOptions {\n  /** Re-render interval in milliseconds. Set 0 to compute once and never update. @default 30000 */\n  updateInterval?: number\n}\n\nconst DEFAULT_UNITS: TimeAgoUnit[] = [\n  { max: 60000, value: 1000, name: \"second\" },\n  { max: 2760000, value: 60000, name: \"minute\" },\n  { max: 72000000, value: 3600000, name: \"hour\" },\n  { max: 518400000, value: 86400000, name: \"day\" },\n  { max: 2419200000, value: 604800000, name: \"week\" },\n  { max: 28512000000, value: 2592000000, name: \"month\" },\n  { max: Number.POSITIVE_INFINITY, value: 31536000000, name: \"year\" },\n]\n\nexport const faMessages: TimeAgoMessages = {\n  justNow: \"همین الان\",\n  past: (n) => (/[\\d۰-۹]/.test(n) ? `${n} پیش` : n),\n  future: (n) => (/[\\d۰-۹]/.test(n) ? `${n} دیگر` : n),\n  invalid: \"\",\n  second: (n) => `${toPersianDigits(String(n))} ثانیه`,\n  minute: (n) => `${toPersianDigits(String(n))} دقیقه`,\n  hour: (n) => `${toPersianDigits(String(n))} ساعت`,\n  day: (n, past) =>\n    n === 1 ? (past ? \"دیروز\" : \"فردا\") : `${toPersianDigits(String(n))} روز`,\n  week: (n, past) =>\n    n === 1\n      ? past\n        ? \"هفته پیش\"\n        : \"هفته دیگر\"\n      : `${toPersianDigits(String(n))} هفته`,\n  month: (n, past) =>\n    n === 1\n      ? past\n        ? \"ماه پیش\"\n        : \"ماه دیگر\"\n      : `${toPersianDigits(String(n))} ماه`,\n  year: (n, past) =>\n    n === 1\n      ? past\n        ? \"سال پیش\"\n        : \"سال دیگر\"\n      : `${toPersianDigits(String(n))} سال`,\n}\n\nexport const enMessages: TimeAgoMessages = {\n  justNow: \"just now\",\n  past: (n) => (/\\d/.test(n) ? `${n} ago` : n),\n  future: (n) => (/\\d/.test(n) ? `in ${n}` : n),\n  invalid: \"\",\n  second: (n) => `${n} second${n > 1 ? \"s\" : \"\"}`,\n  minute: (n) => `${n} minute${n > 1 ? \"s\" : \"\"}`,\n  hour: (n) => `${n} hour${n > 1 ? \"s\" : \"\"}`,\n  day: (n, past) =>\n    n === 1 ? (past ? \"yesterday\" : \"tomorrow\") : `${n} day${n > 1 ? \"s\" : \"\"}`,\n  week: (n, past) =>\n    n === 1\n      ? past\n        ? \"last week\"\n        : \"next week\"\n      : `${n} week${n > 1 ? \"s\" : \"\"}`,\n  month: (n, past) =>\n    n === 1\n      ? past\n        ? \"last month\"\n        : \"next month\"\n      : `${n} month${n > 1 ? \"s\" : \"\"}`,\n  year: (n, past) =>\n    n === 1\n      ? past\n        ? \"last year\"\n        : \"next year\"\n      : `${n} year${n > 1 ? \"s\" : \"\"}`,\n}\n\nfunction defaultFullDateFormatter(calendarType: CalendarType) {\n  return (date: Date) => formatDate(date, \"yyyy/MM/dd\", { calendarType })\n}\n\nfunction applyFormat(\n  messages: TimeAgoMessages,\n  name: TimeAgoUnitName | \"past\" | \"future\",\n  value: number | string,\n  isPast: boolean\n): string {\n  const formatter = messages[name]\n  if (typeof formatter === \"function\") {\n    return (formatter as TimeAgoFormatter<never>)(value as never, isPast)\n  }\n  return formatter.replace(\"{0}\", String(value))\n}\n\n/**\n * Formats the difference between `from` and `now` as a relative time string,\n * e.g. \"۲ روز پیش\" (2 days ago) or \"2 days ago\". Pure function -- the reactive\n * counterpart is `useTimeAgo` below.\n */\nexport function formatTimeAgo(\n  from: Date,\n  options: FormatTimeAgoOptions = {},\n  now: Date | number = Date.now()\n): string {\n  const {\n    max,\n    messages = options.locale === \"en\" ? enMessages : faMessages,\n    fullDateFormatter = defaultFullDateFormatter(\n      options.calendarType ?? \"shamsi\"\n    ),\n    units = DEFAULT_UNITS,\n    showSecond = false,\n    rounding = \"round\",\n  } = options\n\n  const roundFn =\n    typeof rounding === \"number\"\n      ? (n: number) => Number(n.toFixed(rounding))\n      : Math[rounding]\n\n  const diff = Number(now) - from.getTime()\n  const absDiff = Math.abs(diff)\n\n  function getValue(unit: TimeAgoUnit) {\n    return roundFn(absDiff / unit.value)\n  }\n\n  function format(unit: TimeAgoUnit) {\n    const value = getValue(unit)\n    const isPast = diff > 0\n    const str = applyFormat(messages, unit.name, value, isPast)\n    return applyFormat(messages, isPast ? \"past\" : \"future\", str, isPast)\n  }\n\n  if (absDiff < 60000 && !showSecond) {\n    return messages.justNow\n  }\n\n  if (typeof max === \"number\" && absDiff > max) {\n    return fullDateFormatter(from)\n  }\n\n  if (typeof max === \"string\") {\n    const unitMax = units.find((unit) => unit.name === max)?.max\n    if (unitMax && absDiff > unitMax) {\n      return fullDateFormatter(from)\n    }\n  }\n\n  for (const [index, unit] of units.entries()) {\n    const value = getValue(unit)\n    const previous = units[index - 1]\n    if (value <= 0 && previous) return format(previous)\n    if (absDiff < unit.max) return format(unit)\n  }\n\n  return messages.invalid\n}\n\n/**\n * Reactive relative-time formatter (\"۲ روز پیش\" / \"2 days ago\"). Re-renders on\n * `updateInterval` (30s by default) so the string stays current without a\n * manual refresh.\n *\n * Returns `null` until the hook has run in the browser -- `formatTimeAgo`\n * reads `Date.now()` internally, which differs between server and client, so\n * rendering it during SSR would produce a hydration mismatch. Same\n * convention as `useCountdown`/`useDate`.\n */\nexport function useTimeAgo(\n  time: Date | number | string,\n  options: UseTimeAgoOptions = {}\n): string | null {\n  const { updateInterval = 30000, ...formatOptions } = options\n  const [mounted, setMounted] = React.useState(false)\n  const [, forceRender] = React.useReducer((tick: number) => tick + 1, 0)\n\n  React.useEffect(() => {\n    const mount = () => setMounted(true)\n    mount()\n\n    if (updateInterval <= 0) return\n    const id = setInterval(forceRender, updateInterval)\n    return () => clearInterval(id)\n  }, [updateInterval])\n\n  if (!mounted) return null\n\n  // Recomputed on every render, including the interval-driven ones above --\n  // \"now\" must be read fresh each time, so this can't be memoized on `time`/options alone.\n  return formatTimeAgo(new Date(time), formatOptions)\n}\n",
      "type": "registry:hook"
    }
  ],
  "type": "registry:hook"
}