LibreChat/client/src/hooks/useTimeTick.ts
Marco Beretta fc2ae89aa6
⌚ feat: Show Message Timestamps on Hover (#13709)
* ✨ feat: Show Message Timestamps on Hover

Reveal a message's time inline next to the author name on hover. Recent messages (under 24h) show a relative time ("10 minutes ago") with the absolute date on hover; older messages show the absolute date directly.

A shared MessageTimestamp component is used by both MessageRender and ContentRender, with createdAt added to their memo comparators so the timestamp appears once it's available.

Resolves #5199

* 🎨 fix: Gate message timestamp reveal on hover capability, not width

Use a (hover: hover) media query instead of the md: breakpoint so the timestamp stays visible on touch and other non-hover devices (e.g. tablets wider than md), while still revealing on hover/focus where a pointer supports it.

* 🎨 fix: Show message timestamps across all renderers and keep them live

Extend the hover timestamp to the Assistants (MessageParts), shared-link, search, and parallel/multi-response renderers so every prompt and response shows when it was sent. The parent message's createdAt is threaded down to parallel column headers (SiblingHeader).

Add a shared, ref-counted minute ticker (useTimeTick) so relative labels like "2 minutes ago" stay current while a conversation is left open instead of freezing at first render.
2026-06-14 09:39:52 -04:00

33 lines
1,020 B
TypeScript

import { useSyncExternalStore } from 'react';
const listeners = new Set<() => void>();
let tick = 0;
let intervalId: ReturnType<typeof setInterval> | null = null;
const subscribe = (onStoreChange: () => void): (() => void) => {
listeners.add(onStoreChange);
if (intervalId === null) {
intervalId = setInterval(() => {
tick += 1;
listeners.forEach((listener) => listener());
}, 60_000);
}
return () => {
listeners.delete(onStoreChange);
if (listeners.size === 0 && intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
};
};
const getSnapshot = (): number => tick;
/**
* Subscribes to a shared, ref-counted ticker that fires once a minute, so components
* displaying relative time stay current while a view is left open. A single interval
* is shared across all subscribers and is cleared when the last one unsubscribes.
*/
export default function useTimeTick(): number {
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
}