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:
@@ -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>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user