1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-04-21 12:05:30 -04:00

use <time> element for all dates

This commit is contained in:
2022-04-16 15:59:28 -04:00
parent 1648464e0e
commit fa968dbcd2
11 changed files with 217 additions and 202 deletions
+21
View File
@@ -0,0 +1,21 @@
import { useHasMounted } from "../../hooks/use-has-mounted";
import { formatDateTZ, formatDateISO, formatTimeAgo, FlexibleDate } from "../../lib/helpers/format-date";
export type RelativeTimeProps = {
date: FlexibleDate;
className?: string;
};
const RelativeTime = ({ date, className }: RelativeTimeProps) => {
// play nice with SSR -- only use relative time on the client, since it'll quickly become outdated on the server and
// cause a react hydration mismatch error.
const hasMounted = useHasMounted();
return (
<time dateTime={formatDateISO(date)} title={formatDateTZ(date)} className={className}>
Updated {hasMounted ? formatTimeAgo(date) : `on ${formatDateTZ(date, "PP")}`}
</time>
);
};
export default RelativeTime;