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

more optimization/error handling

This commit is contained in:
2022-04-22 09:18:53 -04:00
parent 831139c132
commit 8a97b706be
12 changed files with 93 additions and 72 deletions
+5 -2
View File
@@ -3,17 +3,20 @@ import { formatDateTZ, formatDateISO, formatTimeAgo, FlexibleDate } from "../../
export type RelativeTimeProps = {
date: FlexibleDate;
prefix?: string; // optional "Updated", "Published", "Created", etc.
staticFormat?: string; // full date (without timestamp)
className?: string;
};
const RelativeTime = ({ date, className }: RelativeTimeProps) => {
const RelativeTime = ({ date, prefix, staticFormat = "PP", 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")}`}
{prefix && `${prefix} `}
{hasMounted ? formatTimeAgo(date) : `on ${formatDateTZ(date, staticFormat)}`}
</time>
);
};