Files
docmost/apps/client/src/hooks/use-time-ago.tsx
T
Philip Okugbe 4f3577f009 feat: enhance comments (#1980)
* feat: non-inline comments support

* enhance comments

* fix types
2026-03-02 01:42:25 +00:00

33 lines
825 B
TypeScript

import { timeAgo } from "@/lib/time.ts";
import { useMemo, useSyncExternalStore } from "react";
let tick = 0;
let intervalId: ReturnType<typeof setInterval> | null = null;
const listeners = new Set<() => void>();
function subscribe(callback: () => void) {
listeners.add(callback);
if (listeners.size === 1) {
intervalId = setInterval(() => {
tick++;
listeners.forEach((cb) => cb());
}, 60_000);
}
return () => {
listeners.delete(callback);
if (listeners.size === 0 && intervalId) {
clearInterval(intervalId);
intervalId = null;
}
};
}
function getSnapshot() {
return tick;
}
export function useTimeAgo(date: Date | string) {
const currentTick = useSyncExternalStore(subscribe, getSnapshot);
return useMemo(() => timeAgo(new Date(date)), [date, currentTick]);
}