Skip to content

Commit

Permalink
refactor: cleanup blurb
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Apr 25, 2024
1 parent 237e0de commit 33f3393
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions components/search/Blurb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ const Blurb = (props: BlurbProps) => {
const updatedDate = new Date(date);
const diff = now.getTime() - updatedDate.getTime();

const seconds = Math.floor((diff / 1000) % 60);
const minutes = Math.floor((diff / 1000 / 60) % 60);
const hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
const days = Math.floor(diff / (1000 * 60 * 60 * 24) % 365);
const years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
const days = Math.floor((diff / (1000 * 60 * 60 * 24)) % 365);

let result = "";
if (years > 0) result += `${years} years, `;
if (days > 0 || years > 0) result += `${days} days, `;
if (hours > 0 || days > 0 || years > 0) result += `${hours} hours, `;
result += `${minutes} minutes and ${seconds} seconds ago`;

if (days > 0) {
result += `${days} ${days == 1 ? "day" : "days"}`;
} else if (hours > 0) {
result += `${hours} ${hours == 1 ? "hour" : "hours"}`;
} else {
result += `${minutes} ${minutes == 1 ? "minute" : "minutes"}`;
}

return result;
};
Expand All @@ -43,10 +45,10 @@ const Blurb = (props: BlurbProps) => {
}
};

updateRelativeTime(); // Update initially
const intervalId = setInterval(updateRelativeTime, 1000); // Update every second
updateRelativeTime();
const interval = setInterval(updateRelativeTime, 1000);

return () => clearInterval(intervalId); // Cleanup on unmount
return () => clearInterval(interval);
}, [lastUpdated]);

return (
Expand All @@ -66,7 +68,8 @@ const Blurb = (props: BlurbProps) => {
</div>

<div className="flex text-sm font-light text-gray md:justify-end md:text-base">
{"GE-Z's"} data was updated {lastUpdated ? timeAgo : "x"}
{"GE-Z's"} data was updated{" "}
{lastUpdated ? timeAgo : "x"} ago
</div>
</div>

Expand Down

0 comments on commit 33f3393

Please sign in to comment.