From ae8bd105a06e50299758ecad1801aba4796e9c96 Mon Sep 17 00:00:00 2001 From: Wongspatt Pasadhika <94737324+uno-p-5@users.noreply.github.com> Date: Wed, 24 Apr 2024 22:00:09 -0700 Subject: [PATCH] pls dont break prod (v2) --- components/search/Blurb.tsx | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/components/search/Blurb.tsx b/components/search/Blurb.tsx index 12b8eef..2ebf10d 100644 --- a/components/search/Blurb.tsx +++ b/components/search/Blurb.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from "react"; import { CourseObject, FilterValues } from "./Search"; -import { formatDistanceToNow } from "date-fns"; +import { intervalToDuration } from "date-fns"; interface BlurbProps { filterData: ( @@ -20,10 +20,7 @@ const Blurb = (props: BlurbProps) => { useEffect(() => { const updateRelativeTime = () => { if (lastUpdated) { - const formattedTimeAgo = formatDistanceToNow( - new Date(lastUpdated), - ); - setTimeAgo(`${formattedTimeAgo} ago`); + setTimeAgo(formatTimeSince(lastUpdated)); } }; @@ -33,6 +30,19 @@ const Blurb = (props: BlurbProps) => { return () => clearInterval(intervalId); }, [lastUpdated]); + function formatTimeSince(dateTimestamp) { + const duration = intervalToDuration({ start: new Date(dateTimestamp), end: new Date() }); + const parts = []; + if (duration.years) parts.push(duration.years + " year" + (duration.years !== 1 ? "s" : "")); + if (duration.months) parts.push(duration.months + " month" + (duration.months !== 1 ? "s" : "")); + if (duration.days) parts.push(duration.days + " day" + (duration.days !== 1 ? "s" : "")); + if (duration.hours) parts.push(duration.hours + " hour" + (duration.hours !== 1 ? "s" : "")); + if (duration.minutes) parts.push(duration.minutes + " minute" + (duration.minutes !== 1 ? "s" : "")); + if (duration.seconds) parts.push(duration.seconds + " second" + (duration.seconds !== 1 ? "s" : "")); + + return parts.join(", ") + " ago"; + } + return ( <>