Skip to content

Commit

Permalink
refreshing timeago
Browse files Browse the repository at this point in the history
  • Loading branch information
voliva committed Sep 9, 2022
1 parent 219b0f8 commit 74f2eca
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/App/PlayerDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useStateObservable } from "@react-rxjs/core";
import TimeAgo from "javascript-time-ago";
import en from "javascript-time-ago/locale/en";
import { useEffect, useState } from "react";
import { Pip } from "../components/Pip";
import {
apiKey$,
Expand Down Expand Up @@ -31,10 +32,7 @@ function PlayerDetails() {
</button>
</div>
<div>
<div>
Last update:{" "}
{playerDetails ? timeAgo.format(playerDetails.timestamp) : "N/A"}
</div>
<RefreshingTimeAgo date={playerDetails?.timestamp} />
<div style={{ fontSize: "1.2rem" }}>
<Pip active={true} />
{playerDetails ? playerDetails.pips : "N/A"}
Expand All @@ -44,4 +42,21 @@ function PlayerDetails() {
);
}

function RefreshingTimeAgo({ date }: { date?: Date }) {
const [formattedTime, setFormattedTime] = useState(
date ? timeAgo.format(date) : "N/A"
);

useEffect(() => {
const token = setInterval(() => {
setFormattedTime(date ? timeAgo.format(date) : "N/A");
}, 60_000);
return () => {
clearInterval(token);
};
}, [date]);

return <div>Last update: {formattedTime}</div>;
}

export default PlayerDetails;

0 comments on commit 74f2eca

Please sign in to comment.